Skip to content

Commit a8efac6

Browse files
committed
send emails using queue
Signed-off-by: sergiu <sergiubota@rospace.com>
1 parent 8aeb0d6 commit a8efac6

5 files changed

Lines changed: 102 additions & 6 deletions

File tree

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
},
2929
"require": {
3030
"php": "~8.2.0 || ~8.3.0",
31+
"ext-sockets": "*",
32+
"clue/socket-raw": "^v1.6.0",
3133
"dotkernel/dot-cache": "^4.3.0",
3234
"dotkernel/dot-cli": "^3.9.0",
3335
"dotkernel/dot-data-fixtures": "^1.4.0",
@@ -79,7 +81,8 @@
7981
"Core\\App\\": "src/Core/src/App/src",
8082
"Core\\Security\\": "src/Core/src/Security/src",
8183
"Core\\Setting\\": "src/Core/src/Setting/src",
82-
"Core\\User\\": "src/Core/src/User/src"
84+
"Core\\User\\": "src/Core/src/User/src",
85+
"Core\\NotificationSystem\\": "src/Core/src/NotificationSystem/src"
8386
}
8487
},
8588
"autoload-dev": {

config/config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class_exists(Mezzio\Tooling\ConfigProvider::class)
7474
Core\Security\ConfigProvider::class,
7575
Core\Setting\ConfigProvider::class,
7676
Core\User\ConfigProvider::class,
77+
Core\NotificationSystem\ConfigProvider::class,
7778

7879
// Load application config in a pre-defined order in such a way that local settings
7980
// overwrite global settings. (Loaded as first to last):
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Core\NotificationSystem;
6+
7+
use Core\NotificationSystem\Service\NotificationService;
8+
use Dot\DependencyInjection\Factory\AttributedServiceFactory;
9+
10+
/**
11+
* @phpstan-type ConfigType array{
12+
* dependencies: DependenciesType,
13+
* }
14+
* @phpstan-type DependenciesType array{
15+
* factories: array<class-string, class-string>,
16+
* }
17+
*/
18+
class ConfigProvider
19+
{
20+
/**
21+
* @return ConfigType
22+
*/
23+
public function __invoke(): array
24+
{
25+
return [
26+
'dependencies' => $this->getDependencies(),
27+
];
28+
}
29+
30+
/**
31+
* @return DependenciesType
32+
*/
33+
public function getDependencies(): array
34+
{
35+
return [
36+
'factories' => [
37+
NotificationService::class => AttributedServiceFactory::class,
38+
],
39+
];
40+
}
41+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Core\NotificationSystem\Service;
6+
7+
use Core\User\Entity\User;
8+
use Dot\DependencyInjection\Attribute\Inject;
9+
use Laminas\Json\Encoder;
10+
use Socket\Raw\Factory;
11+
use Socket\Raw\Socket;
12+
13+
class NotificationService
14+
{
15+
/**
16+
* @param array<non-empty-string, mixed> $config
17+
*/
18+
#[Inject(
19+
'config.notification.server',
20+
)]
21+
public function __construct(
22+
private readonly array $config
23+
) {
24+
}
25+
26+
public function createClient(): Socket
27+
{
28+
$socketRawFactory = new Factory();
29+
return $socketRawFactory->createClient(
30+
$this->config['protocol'] . '://' . $this->config['host'] . ':' . $this->config['port']
31+
);
32+
}
33+
34+
public function send(string $message): void
35+
{
36+
$this->createClient()->write($message . $this->config['eof']);
37+
}
38+
39+
/**
40+
* @param array<non-empty-string, mixed> $data
41+
*/
42+
protected function encodeEmailMessage(array $data): string
43+
{
44+
return Encoder::encode($data);
45+
}
46+
47+
public function sendNewAccountNotification(User $user): void
48+
{
49+
$data['userUuid'] = $user->getUuid()->toString();
50+
$this->send($this->encodeEmailMessage($data));
51+
}
52+
}

src/User/src/Handler/PostUserCreateHandler.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Admin\User\Service\UserServiceInterface;
1313
use Core\App\Message;
1414
use Core\App\Service\MailService;
15+
use Core\NotificationSystem\Service\NotificationService;
1516
use Dot\DependencyInjection\Attribute\Inject;
1617
use Dot\FlashMessenger\FlashMessengerInterface;
1718
use Dot\Log\Logger;
@@ -41,6 +42,7 @@ class PostUserCreateHandler implements RequestHandlerInterface
4142
FlashMessengerInterface::class,
4243
CreateUserForm::class,
4344
MailService::class,
45+
NotificationService::class,
4446
'dot-log.default_logger',
4547
'config',
4648
)]
@@ -51,6 +53,7 @@ public function __construct(
5153
protected FlashMessengerInterface $messenger,
5254
protected CreateUserForm $createUserForm,
5355
protected MailService $mailService,
56+
protected NotificationService $notificationService,
5457
protected Logger $logger,
5558
protected array $config,
5659
) {
@@ -71,11 +74,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
7174
$user = $this->userService->saveUser($data);
7275
$this->messenger->addSuccess(Message::USER_CREATED);
7376
if ($user->hasEmail()) {
74-
$body = $this->template->render('user::welcome', [
75-
'config' => $this->config,
76-
'user' => $user,
77-
]);
78-
$this->mailService->sendWelcomeMail($user, $body);
77+
$this->notificationService->sendNewAccountNotification($user);
7978
}
8079

8180
return new EmptyResponse(StatusCodeInterface::STATUS_CREATED);

0 commit comments

Comments
 (0)