Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
},
"require": {
"php": "~8.2.0 || ~8.3.0",
"ext-sockets": "*",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is with those socket packages and extensions ?
not a good idea to add it to the default admin ...
there is nothing built in ?

"clue/socket-raw": "^v1.6.0",
"dotkernel/dot-cache": "^4.3.0",
"dotkernel/dot-cli": "^3.9.0",
"dotkernel/dot-data-fixtures": "^1.4.0",
Expand Down Expand Up @@ -79,7 +81,8 @@
"Core\\App\\": "src/Core/src/App/src",
"Core\\Security\\": "src/Core/src/Security/src",
"Core\\Setting\\": "src/Core/src/Setting/src",
"Core\\User\\": "src/Core/src/User/src"
"Core\\User\\": "src/Core/src/User/src",
"Core\\NotificationSystem\\": "src/Core/src/NotificationSystem/src"
}
},
"autoload-dev": {
Expand Down
1 change: 1 addition & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class_exists(Mezzio\Tooling\ConfigProvider::class)
Core\Security\ConfigProvider::class,
Core\Setting\ConfigProvider::class,
Core\User\ConfigProvider::class,
Core\NotificationSystem\ConfigProvider::class,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we load this dynamically ?
Only if there is something about that in local.php ?


// Load application config in a pre-defined order in such a way that local settings
// overwrite global settings. (Loaded as first to last):
Expand Down
41 changes: 41 additions & 0 deletions src/Core/src/NotificationSystem/src/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Core\NotificationSystem;

use Core\NotificationSystem\Service\NotificationService;
use Dot\DependencyInjection\Factory\AttributedServiceFactory;

/**
* @phpstan-type ConfigType array{
* dependencies: DependenciesType,
* }
* @phpstan-type DependenciesType array{
* factories: array<class-string, class-string>,
* }
*/
class ConfigProvider
{
/**
* @return ConfigType
*/
public function __invoke(): array
{
return [
'dependencies' => $this->getDependencies(),
];
}

/**
* @return DependenciesType
*/
public function getDependencies(): array
{
return [
'factories' => [
NotificationService::class => AttributedServiceFactory::class,
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Core\NotificationSystem\Service;

use Core\User\Entity\User;
use Dot\DependencyInjection\Attribute\Inject;
use Laminas\Json\Encoder;
use Socket\Raw\Factory;
use Socket\Raw\Socket;

class NotificationService
{
/**
* @param array<non-empty-string, mixed> $config
*/
#[Inject(
'config.notification.server',
)]
public function __construct(
private readonly array $config
) {
}

public function createClient(): Socket
{
$socketRawFactory = new Factory();
return $socketRawFactory->createClient(
$this->config['protocol'] . '://' . $this->config['host'] . ':' . $this->config['port']
);
}

public function send(string $message): void
{
$this->createClient()->write($message . $this->config['eof']);
}

/**
* @param array<non-empty-string, mixed> $data
*/
protected function encodeEmailMessage(array $data): string
{
return Encoder::encode($data);
}

public function sendNewAccountNotification(User $user): void
{
$data['userUuid'] = $user->getUuid()->toString();
$this->send($this->encodeEmailMessage($data));
}
}
9 changes: 4 additions & 5 deletions src/User/src/Handler/PostUserCreateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Admin\User\Service\UserServiceInterface;
use Core\App\Message;
use Core\App\Service\MailService;
use Core\NotificationSystem\Service\NotificationService;
use Dot\DependencyInjection\Attribute\Inject;
use Dot\FlashMessenger\FlashMessengerInterface;
use Dot\Log\Logger;
Expand Down Expand Up @@ -41,6 +42,7 @@
FlashMessengerInterface::class,
CreateUserForm::class,
MailService::class,
NotificationService::class,
'dot-log.default_logger',
'config',
)]
Expand All @@ -51,6 +53,7 @@
protected FlashMessengerInterface $messenger,
protected CreateUserForm $createUserForm,
protected MailService $mailService,
protected NotificationService $notificationService,
protected Logger $logger,
protected array $config,
) {
Expand All @@ -71,11 +74,7 @@
$user = $this->userService->saveUser($data);
$this->messenger->addSuccess(Message::USER_CREATED);
if ($user->hasEmail()) {
$body = $this->template->render('user::welcome', [
'config' => $this->config,
'user' => $user,
]);
$this->mailService->sendWelcomeMail($user, $body);
$this->notificationService->sendNewAccountNotification($user);
}

return new EmptyResponse(StatusCodeInterface::STATUS_CREATED);
Expand All @@ -87,14 +86,14 @@
]),
StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY
);
} catch (MailException $exception) {

Check notice on line 89 in src/User/src/Handler/PostUserCreateHandler.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Redundant catch clause

Exception 'MailException' is never thrown in the corresponding 'try' block

Check notice on line 89 in src/User/src/Handler/PostUserCreateHandler.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Redundant catch clause

Exception 'MailException' is never thrown in the corresponding 'try' block
$this->logger->err('Send user welcome email', [
'error' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'trace' => $exception->getTraceAsString(),
]);
$this->messenger->addError(Message::mailNotSentTo($user->getEmail()));

Check failure on line 96 in src/User/src/Handler/PostUserCreateHandler.php

View workflow job for this annotation

GitHub Actions / PHPStan 8.2-ubuntu-latest

Cannot call method getEmail() on Core\User\Entity\User|null.

Check failure on line 96 in src/User/src/Handler/PostUserCreateHandler.php

View workflow job for this annotation

GitHub Actions / PHPStan 8.3-ubuntu-latest

Cannot call method getEmail() on Core\User\Entity\User|null.

Check failure on line 96 in src/User/src/Handler/PostUserCreateHandler.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.2, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Cannot call method getEmail() on Core\User\Entity\User|null.

Check failure on line 96 in src/User/src/Handler/PostUserCreateHandler.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.2, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Cannot call method getEmail() on Core\User\Entity\User|null.
return new EmptyResponse(StatusCodeInterface::STATUS_CREATED);
} catch (BadRequestException | ConflictException | NotFoundException $exception) {
return new HtmlResponse(
Expand Down
Loading