-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMessageHandler.php
More file actions
79 lines (65 loc) · 2.19 KB
/
MessageHandler.php
File metadata and controls
79 lines (65 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
namespace Queue\App\Message;
use Core\User\Repository\UserRepository;
use Dot\DependencyInjection\Attribute\Inject;
use Dot\Log\Logger;
use Dot\Mail\Exception\MailException;
use Dot\Mail\Service\MailService;
use Exception;
use Mezzio\Template\TemplateRendererInterface;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use function json_decode;
class MessageHandler
{
protected array $args = [];
#[Inject(
MailService::class,
TemplateRendererInterface::class,
UserRepository::class,
'dot-log.queue-log',
'config',
)]
public function __construct(
protected MailService $mailService,
protected TemplateRendererInterface $templateRenderer,
protected UserRepository $userRepository,
protected Logger $logger,
protected array $config,
) {
}
public function __invoke(Message $message): void
{
$payload = json_decode($message->getPayload()['foo'], true);
if ($payload !== null && isset($payload['userUuid'])) {
$this->logger->info("message: " . $payload['userUuid']);
$this->args = $payload;
try {
$this->perform();
} catch (Exception $exception) {
$this->logger->error("message: " . $exception->getMessage());
}
}
}
public function perform(): void
{
$this->sendWelcomeMail();
}
public function sendWelcomeMail(): bool
{
$user = $this->userRepository->find($this->args['userUuid']);
$this->mailService->getMessage()->addTo($user->getEmail(), $user->getName());
$this->mailService->setSubject('Welcome to ' . $this->config['application']['name']);
$body = $this->templateRenderer->render('notification-email::welcome', [
'user' => $user,
'config' => $this->config,
]);
$this->mailService->setBody($body);
try {
return $this->mailService->send()->isValid();
} catch (MailException | TransportExceptionInterface $exception) {
$this->logger->notice($exception->getMessage());
}
return false;
}
}