|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\MessageHandler; |
|
6
|
|
|
|
|
7
|
|
|
use App\Entity\User; |
|
8
|
|
|
use App\Mailer\Mailer; |
|
9
|
|
|
use App\Message\SendEmailConfirmationLink; |
|
10
|
|
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail; |
|
11
|
|
|
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; |
|
12
|
|
|
use Symfony\Component\Mime\Address; |
|
13
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
14
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
15
|
|
|
use SymfonyCasts\Bundle\VerifyEmail\Model\VerifyEmailSignatureComponents; |
|
16
|
|
|
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface; |
|
17
|
|
|
|
|
18
|
|
|
final class SendEmailConfirmationLinkHandler implements MessageHandlerInterface |
|
19
|
|
|
{ |
|
20
|
|
|
private VerifyEmailHelperInterface $verifyEmailHelper; |
|
21
|
|
|
private Mailer $mailer; |
|
22
|
|
|
private UrlGeneratorInterface $router; |
|
23
|
|
|
private TranslatorInterface $translator; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct( |
|
26
|
|
|
VerifyEmailHelperInterface $helper, |
|
27
|
|
|
Mailer $mailer, |
|
28
|
|
|
UrlGeneratorInterface $router, |
|
29
|
|
|
TranslatorInterface $translator |
|
30
|
|
|
) { |
|
31
|
|
|
$this->verifyEmailHelper = $helper; |
|
32
|
|
|
$this->mailer = $mailer; |
|
33
|
|
|
$this->router = $router; |
|
34
|
|
|
$this->translator = $translator; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function __invoke(SendEmailConfirmationLink $sendEmailConfirmationLink): void |
|
38
|
|
|
{ |
|
39
|
|
|
$user = $sendEmailConfirmationLink->getUser(); |
|
40
|
|
|
$email = $this->buildEmail($user); |
|
41
|
|
|
$this->mailer->send($email); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private function getSender(): Address |
|
45
|
|
|
{ |
|
46
|
|
|
$host = $this->router->getContext()->getHost(); |
|
47
|
|
|
|
|
48
|
|
|
return new Address('no-reply@'.$host, $host); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
private function getSubject(): string |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->translator->trans('confirmation.email.subject'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function getSignatureComponents(User $user): VerifyEmailSignatureComponents |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->verifyEmailHelper->generateSignature( |
|
59
|
|
|
'verify_email', |
|
60
|
|
|
(string) $user->getId(), |
|
61
|
|
|
$user->getEmail() |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
private function createContext(VerifyEmailSignatureComponents $signatureComponents): array |
|
66
|
|
|
{ |
|
67
|
|
|
return [ |
|
68
|
|
|
'signedUrl' => $signatureComponents->getSignedUrl(), |
|
69
|
|
|
'expiresAtMessageKey' => $signatureComponents->getExpirationMessageKey(), |
|
70
|
|
|
'expiresAtMessageData' => $signatureComponents->getExpirationMessageData(), |
|
71
|
|
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private function buildEmail(User $user): TemplatedEmail |
|
75
|
|
|
{ |
|
76
|
|
|
$signatureComponents = $this->getSignatureComponents($user); |
|
77
|
|
|
|
|
78
|
|
|
return (new TemplatedEmail()) |
|
79
|
|
|
->from($this->getSender()) |
|
80
|
|
|
->to($user->getEmail()) |
|
81
|
|
|
->subject($this->getSubject()) |
|
82
|
|
|
->textTemplate('emails/confirmation_email.html.twig') |
|
83
|
|
|
->context($this->createContext($signatureComponents)); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|