1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gbere\SimpleAuth\Service; |
6
|
|
|
|
7
|
|
|
use Gbere\SimpleAuth\Entity\User; |
8
|
|
|
use Symfony\Bridge\Twig\Mime\NotificationEmail; |
9
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
10
|
|
|
use Symfony\Component\Mailer\Exception\TransportExceptionInterface; |
11
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
12
|
|
|
|
13
|
|
|
class Mailer |
14
|
|
|
{ |
15
|
|
|
/** @var MailerInterface */ |
16
|
|
|
private $mailer; |
17
|
|
|
/** @var ParameterBagInterface */ |
18
|
|
|
private $parameterBag; |
19
|
|
|
|
20
|
|
|
public function __construct(MailerInterface $mailer, ParameterBagInterface $parameterBag) |
21
|
|
|
{ |
22
|
|
|
$this->mailer = $mailer; |
23
|
|
|
$this->parameterBag = $parameterBag; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @throws TransportExceptionInterface |
28
|
|
|
*/ |
29
|
|
|
public function sendConfirmRegistrationMessage(User $user): void |
30
|
|
|
{ |
31
|
|
|
$this->mailer->send((new NotificationEmail()) |
32
|
|
|
->from($this->getSenderEmail()) |
33
|
|
|
->to($user->getEmail()) |
34
|
|
|
->subject('Confirm registration') |
35
|
|
|
->htmlTemplate('emails/confirm-registration.html.twig') |
36
|
|
|
->context(['token' => $user->getConfirmationToken()]) |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @throws TransportExceptionInterface |
42
|
|
|
*/ |
43
|
|
|
public function sendWelcomeMessage(User $user): void |
44
|
|
|
{ |
45
|
|
|
$this->mailer->send((new NotificationEmail()) |
46
|
|
|
->from($this->getSenderEmail()) |
47
|
|
|
->to($user->getEmail()) |
48
|
|
|
->subject('Welcome') |
49
|
|
|
->htmlTemplate('emails/welcome.html.twig') |
50
|
|
|
->context(['token' => $user->getConfirmationToken()]) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @throws TransportExceptionInterface |
56
|
|
|
*/ |
57
|
|
|
public function sendPasswordResetMessage(User $user): void |
58
|
|
|
{ |
59
|
|
|
$this->mailer->send((new NotificationEmail()) |
60
|
|
|
->from($this->getSenderEmail()) |
61
|
|
|
->to($user->getEmail()) |
62
|
|
|
->subject('Password request') |
63
|
|
|
->htmlTemplate('emails/password-reset.html.twig') |
64
|
|
|
->context(['token' => $user->getConfirmationToken()]) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @throws TransportExceptionInterface |
70
|
|
|
*/ |
71
|
|
|
public function sendPasswordResetNotificationMessage(User $user): void |
72
|
|
|
{ |
73
|
|
|
$this->mailer->send((new NotificationEmail()) |
74
|
|
|
->from($this->getSenderEmail()) |
75
|
|
|
->to($user->getEmail()) |
76
|
|
|
->subject('Password reset notification') |
77
|
|
|
->htmlTemplate('emails/password-reset-notification.html.twig') |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function getSenderEmail(): string |
82
|
|
|
{ |
83
|
|
|
return $this->parameterBag->get('email.sender'); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|