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\SendResetPasswordLink; |
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
|
|
|
|
16
|
|
|
final class SendResetPasswordLinkHandler implements MessageHandlerInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Mailer |
20
|
|
|
*/ |
21
|
|
|
private $mailer; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var TranslatorInterface |
25
|
|
|
*/ |
26
|
|
|
private $translator; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var UrlGeneratorInterface |
30
|
|
|
*/ |
31
|
|
|
private $router; |
32
|
|
|
|
33
|
|
|
public function __construct(Mailer $mailer, TranslatorInterface $translator, UrlGeneratorInterface $router) |
34
|
|
|
{ |
35
|
|
|
$this->mailer = $mailer; |
36
|
|
|
$this->translator = $translator; |
37
|
|
|
$this->router = $router; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function __invoke(SendResetPasswordLink $sendResetPasswordLink) |
41
|
|
|
{ |
42
|
|
|
/** @var User $user */ |
43
|
|
|
$user = $sendResetPasswordLink->getUser(); |
44
|
|
|
|
45
|
|
|
/** @var TemplatedEmail $email */ |
46
|
|
|
$email = $this->buildEmail($user); |
47
|
|
|
|
48
|
|
|
$this->mailer->send($email); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function getSender(): Address |
52
|
|
|
{ |
53
|
|
|
$host = $this->router->getContext()->getHost(); |
54
|
|
|
|
55
|
|
|
return new Address('no-reply@'.$host, $host); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function getSubject(): string |
59
|
|
|
{ |
60
|
|
|
return $this->translator->trans('resetting.email.subject'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function getConfirmationUrl(User $user): string |
64
|
|
|
{ |
65
|
|
|
return $this->router->generate( |
66
|
|
|
'password_reset_confirm', ['token' => $user->getConfirmationToken()], 0 |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function buildEmail(User $user): TemplatedEmail |
71
|
|
|
{ |
72
|
|
|
return (new TemplatedEmail()) |
73
|
|
|
->from($this->getSender()) |
74
|
|
|
->to($user->getEmail()) |
75
|
|
|
->subject($this->getSubject()) |
76
|
|
|
->textTemplate('emails/reset.txt.twig') |
77
|
|
|
->context([ |
78
|
|
|
'confirmationUrl' => $this->getConfirmationUrl($user), |
79
|
|
|
'username' => $user->getUsername(), |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|