|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gbere\SimpleAuth\Service; |
|
6
|
|
|
|
|
7
|
|
|
use Gbere\SimpleAuth\Bridge\Mime\TemplatedEmail; |
|
8
|
|
|
use Gbere\SimpleAuth\Model\UserInterface; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
|
10
|
|
|
use Symfony\Component\Mailer\Exception\TransportExceptionInterface; |
|
11
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
|
12
|
|
|
use Symfony\Component\Mime\Address; |
|
13
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
14
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
15
|
|
|
|
|
16
|
|
|
class Mailer |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var ParameterBagInterface */ |
|
19
|
|
|
private $parameterBag; |
|
20
|
|
|
/** @var MailerInterface */ |
|
21
|
|
|
private $mailer; |
|
22
|
|
|
/** @var RouterInterface */ |
|
23
|
|
|
private $router; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(ParameterBagInterface $parameterBag, MailerInterface $mailer, RouterInterface $router) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->parameterBag = $parameterBag; |
|
28
|
|
|
$this->mailer = $mailer; |
|
29
|
|
|
$this->router = $router; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @throws TransportExceptionInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
public function sendConfirmRegistrationMessage(UserInterface $user): void |
|
36
|
|
|
{ |
|
37
|
|
|
$this->mailer->send((new TemplatedEmail()) |
|
38
|
|
|
->from($this->getSenderEmail()) |
|
39
|
|
|
->to(new Address($user->getEmail(), $user->getName())) |
|
|
|
|
|
|
40
|
|
|
->subject('Confirm registration') |
|
41
|
|
|
->context([ |
|
42
|
|
|
'content' => 'Please click the next link to confirm registration', |
|
43
|
|
|
'action_url' => $this->generateUrl('simple_auth_confirm_registration', ['token' => $user->getConfirmationToken()]), |
|
44
|
|
|
'action_text' => 'Confirm registration', |
|
45
|
|
|
]) |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @throws TransportExceptionInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
public function sendWelcomeMessage(UserInterface $user): void |
|
53
|
|
|
{ |
|
54
|
|
|
$this->mailer->send((new TemplatedEmail()) |
|
55
|
|
|
->from($this->getSenderEmail()) |
|
56
|
|
|
->to(new Address($user->getEmail(), $user->getName())) |
|
|
|
|
|
|
57
|
|
|
->subject('Welcome') |
|
58
|
|
|
->context(['content' => 'Your registration was successfully completed']) |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @throws TransportExceptionInterface |
|
64
|
|
|
*/ |
|
65
|
|
|
public function sendPasswordResetMessage(UserInterface $user): void |
|
66
|
|
|
{ |
|
67
|
|
|
$this->mailer->send((new TemplatedEmail()) |
|
68
|
|
|
->from($this->getSenderEmail()) |
|
69
|
|
|
->to(new Address($user->getEmail(), $user->getName())) |
|
|
|
|
|
|
70
|
|
|
->subject('Password request') |
|
71
|
|
|
->context([ |
|
72
|
|
|
'content' => 'Please, click the next link to reset your password', |
|
73
|
|
|
'action_url' => $this->generateUrl('simple_auth_password_reset', ['token' => $user->getConfirmationToken()]), |
|
74
|
|
|
'action_text' => 'Reset password', |
|
75
|
|
|
]) |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @throws TransportExceptionInterface |
|
81
|
|
|
*/ |
|
82
|
|
|
public function sendPasswordResetNotificationMessage(UserInterface $user): void |
|
83
|
|
|
{ |
|
84
|
|
|
$this->mailer->send((new TemplatedEmail()) |
|
85
|
|
|
->from($this->getSenderEmail()) |
|
86
|
|
|
->to(new Address($user->getEmail(), $user->getName())) |
|
|
|
|
|
|
87
|
|
|
->subject('Password reset notification') |
|
88
|
|
|
->context([ |
|
89
|
|
|
'content' => 'Your password was successfully updated', |
|
90
|
|
|
]) |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function getSenderEmail(): Address |
|
95
|
|
|
{ |
|
96
|
|
|
return new Address( |
|
97
|
|
|
$this->parameterBag->get('simple_auth_sender_email'), |
|
98
|
|
|
$this->parameterBag->get('simple_auth_sender_name') ?? '' |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private function generateUrl(string $name, array $parameters = null): string |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->router->generate($name, $parameters, UrlGeneratorInterface::ABSOLUTE_URL); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|