From a8efac6f645dc4af78a73b6207e2e5ea1616ac74 Mon Sep 17 00:00:00 2001 From: sergiu Date: Tue, 1 Jul 2025 19:16:18 +0300 Subject: [PATCH 1/2] send emails using queue Signed-off-by: sergiu --- composer.json | 5 +- config/config.php | 1 + .../NotificationSystem/src/ConfigProvider.php | 41 +++++++++++++++ .../src/Service/NotificationService.php | 52 +++++++++++++++++++ .../src/Handler/PostUserCreateHandler.php | 9 ++-- 5 files changed, 102 insertions(+), 6 deletions(-) create mode 100644 src/Core/src/NotificationSystem/src/ConfigProvider.php create mode 100644 src/Core/src/NotificationSystem/src/Service/NotificationService.php diff --git a/composer.json b/composer.json index cb31b81..0fbfc14 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,8 @@ }, "require": { "php": "~8.2.0 || ~8.3.0", + "ext-sockets": "*", + "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", @@ -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": { diff --git a/config/config.php b/config/config.php index 98da7bd..ff7363e 100644 --- a/config/config.php +++ b/config/config.php @@ -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, // Load application config in a pre-defined order in such a way that local settings // overwrite global settings. (Loaded as first to last): diff --git a/src/Core/src/NotificationSystem/src/ConfigProvider.php b/src/Core/src/NotificationSystem/src/ConfigProvider.php new file mode 100644 index 0000000..3b35180 --- /dev/null +++ b/src/Core/src/NotificationSystem/src/ConfigProvider.php @@ -0,0 +1,41 @@ +, + * } + */ +class ConfigProvider +{ + /** + * @return ConfigType + */ + public function __invoke(): array + { + return [ + 'dependencies' => $this->getDependencies(), + ]; + } + + /** + * @return DependenciesType + */ + public function getDependencies(): array + { + return [ + 'factories' => [ + NotificationService::class => AttributedServiceFactory::class, + ], + ]; + } +} diff --git a/src/Core/src/NotificationSystem/src/Service/NotificationService.php b/src/Core/src/NotificationSystem/src/Service/NotificationService.php new file mode 100644 index 0000000..c9a5602 --- /dev/null +++ b/src/Core/src/NotificationSystem/src/Service/NotificationService.php @@ -0,0 +1,52 @@ + $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 $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)); + } +} diff --git a/src/User/src/Handler/PostUserCreateHandler.php b/src/User/src/Handler/PostUserCreateHandler.php index 4b2c459..632123a 100644 --- a/src/User/src/Handler/PostUserCreateHandler.php +++ b/src/User/src/Handler/PostUserCreateHandler.php @@ -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; @@ -41,6 +42,7 @@ class PostUserCreateHandler implements RequestHandlerInterface FlashMessengerInterface::class, CreateUserForm::class, MailService::class, + NotificationService::class, 'dot-log.default_logger', 'config', )] @@ -51,6 +53,7 @@ public function __construct( protected FlashMessengerInterface $messenger, protected CreateUserForm $createUserForm, protected MailService $mailService, + protected NotificationService $notificationService, protected Logger $logger, protected array $config, ) { @@ -71,11 +74,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface $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); From 29e1629d3a140b4cec118c327cdf6f66e4e2e954 Mon Sep 17 00:00:00 2001 From: sergiu Date: Thu, 3 Jul 2025 10:10:28 +0300 Subject: [PATCH 2/2] send procedural TPC message to ping Signed-off-by: sergiu --- composer.json | 5 +- config/config.php | 1 - .../NotificationSystem/src/ConfigProvider.php | 41 --------------- .../src/Service/NotificationService.php | 52 ------------------- .../src/Handler/PostUserCreateHandler.php | 19 +++++-- 5 files changed, 16 insertions(+), 102 deletions(-) delete mode 100644 src/Core/src/NotificationSystem/src/ConfigProvider.php delete mode 100644 src/Core/src/NotificationSystem/src/Service/NotificationService.php diff --git a/composer.json b/composer.json index 0fbfc14..cb31b81 100644 --- a/composer.json +++ b/composer.json @@ -28,8 +28,6 @@ }, "require": { "php": "~8.2.0 || ~8.3.0", - "ext-sockets": "*", - "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", @@ -81,8 +79,7 @@ "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\\NotificationSystem\\": "src/Core/src/NotificationSystem/src" + "Core\\User\\": "src/Core/src/User/src" } }, "autoload-dev": { diff --git a/config/config.php b/config/config.php index ff7363e..98da7bd 100644 --- a/config/config.php +++ b/config/config.php @@ -74,7 +74,6 @@ class_exists(Mezzio\Tooling\ConfigProvider::class) Core\Security\ConfigProvider::class, Core\Setting\ConfigProvider::class, Core\User\ConfigProvider::class, - Core\NotificationSystem\ConfigProvider::class, // Load application config in a pre-defined order in such a way that local settings // overwrite global settings. (Loaded as first to last): diff --git a/src/Core/src/NotificationSystem/src/ConfigProvider.php b/src/Core/src/NotificationSystem/src/ConfigProvider.php deleted file mode 100644 index 3b35180..0000000 --- a/src/Core/src/NotificationSystem/src/ConfigProvider.php +++ /dev/null @@ -1,41 +0,0 @@ -, - * } - */ -class ConfigProvider -{ - /** - * @return ConfigType - */ - public function __invoke(): array - { - return [ - 'dependencies' => $this->getDependencies(), - ]; - } - - /** - * @return DependenciesType - */ - public function getDependencies(): array - { - return [ - 'factories' => [ - NotificationService::class => AttributedServiceFactory::class, - ], - ]; - } -} diff --git a/src/Core/src/NotificationSystem/src/Service/NotificationService.php b/src/Core/src/NotificationSystem/src/Service/NotificationService.php deleted file mode 100644 index c9a5602..0000000 --- a/src/Core/src/NotificationSystem/src/Service/NotificationService.php +++ /dev/null @@ -1,52 +0,0 @@ - $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 $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)); - } -} diff --git a/src/User/src/Handler/PostUserCreateHandler.php b/src/User/src/Handler/PostUserCreateHandler.php index 632123a..52e1dab 100644 --- a/src/User/src/Handler/PostUserCreateHandler.php +++ b/src/User/src/Handler/PostUserCreateHandler.php @@ -12,7 +12,6 @@ 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; @@ -27,6 +26,11 @@ use Psr\Http\Server\RequestHandlerInterface; use Throwable; +use function fclose; +use function fwrite; +use function json_encode; +use function stream_socket_client; + /** * @phpstan-import-type CreateUserDataType from CreateUserInputFilter */ @@ -42,7 +46,6 @@ class PostUserCreateHandler implements RequestHandlerInterface FlashMessengerInterface::class, CreateUserForm::class, MailService::class, - NotificationService::class, 'dot-log.default_logger', 'config', )] @@ -53,7 +56,6 @@ public function __construct( protected FlashMessengerInterface $messenger, protected CreateUserForm $createUserForm, protected MailService $mailService, - protected NotificationService $notificationService, protected Logger $logger, protected array $config, ) { @@ -74,7 +76,16 @@ public function handle(ServerRequestInterface $request): ResponseInterface $user = $this->userService->saveUser($data); $this->messenger->addSuccess(Message::USER_CREATED); if ($user->hasEmail()) { - $this->notificationService->sendNewAccountNotification($user); + $client = stream_socket_client("tcp://localhost:8556", $errno, $errstr, 30); + + if (! $client) { + echo "Error: $errstr ($errno)\n"; + } else { + $data['userUuid'] = $user->getUuid()->toString(); + fwrite($client, json_encode($data) . "\n"); + + fclose($client); + } } return new EmptyResponse(StatusCodeInterface::STATUS_CREATED);