Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
22 / 22 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
Mailer | |
100.00% |
22 / 22 |
|
100.00% |
3 / 3 |
3 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
send | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
sendForgetPassword | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * This file is part of the MADIS - RGPD Management application. |
5 | * |
6 | * @copyright Copyright (c) 2018-2019 Soluris - Solutions Numériques Territoriales Innovantes |
7 | * |
8 | * This program is free software: you can redistribute it and/or modify |
9 | * it under the terms of the GNU Affero General Public License as published by |
10 | * the Free Software Foundation, either version 3 of the License, or |
11 | * (at your option) any later version. |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU Affero General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU Affero General Public License |
19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
20 | */ |
21 | |
22 | declare(strict_types=1); |
23 | |
24 | namespace App\Domain\User\Component; |
25 | |
26 | use App\Domain\User\Model; |
27 | use Symfony\Component\Mailer\Exception\TransportExceptionInterface; |
28 | use Symfony\Component\Mime\Address; |
29 | use Symfony\Component\Mime\Email; |
30 | use Symfony\Contracts\Translation\TranslatorInterface; |
31 | use Twig\Environment; |
32 | |
33 | class Mailer |
34 | { |
35 | /** |
36 | * @var \Symfony\Component\Mailer\Mailer |
37 | */ |
38 | private $mailer; |
39 | |
40 | /** |
41 | * @var TranslatorInterface |
42 | */ |
43 | private $translator; |
44 | |
45 | /** |
46 | * @var Environment |
47 | */ |
48 | private $twig; |
49 | |
50 | /** |
51 | * @var string |
52 | */ |
53 | private $senderEmail; |
54 | |
55 | /** |
56 | * @var string |
57 | */ |
58 | private $senderName; |
59 | |
60 | public function __construct( |
61 | \Symfony\Component\Mailer\MailerInterface $mailer, |
62 | TranslatorInterface $translator, |
63 | Environment $twig, |
64 | string $senderEmail, |
65 | string $senderName, |
66 | ) { |
67 | $this->mailer = $mailer; |
68 | $this->translator = $translator; |
69 | $this->twig = $twig; |
70 | $this->senderEmail = $senderEmail; |
71 | $this->senderName = $senderName; |
72 | } |
73 | |
74 | /** |
75 | * Send an email. |
76 | * |
77 | * @param string $to The receiver of the email |
78 | * @param string $subject The subject of the email |
79 | * @param string $body The content of the email |
80 | * |
81 | * @throws TransportExceptionInterface |
82 | */ |
83 | private function send(string $to, string $subject, string $body): void |
84 | { |
85 | $message = (new Email()) |
86 | ->from(new Address($this->senderEmail, $this->senderName)) |
87 | ->to($to) |
88 | ->subject($subject) |
89 | ->html($body) |
90 | ; |
91 | |
92 | $this->mailer->send($message); |
93 | } |
94 | |
95 | /** |
96 | * Send forget password email in order to reset it. |
97 | * |
98 | * @param Model\User $user The user to reset password |
99 | * |
100 | * @throws \Twig\Error\LoaderError |
101 | * @throws \Twig\Error\RuntimeError |
102 | * @throws \Twig\Error\SyntaxError |
103 | */ |
104 | public function sendForgetPassword(Model\User $user): void |
105 | { |
106 | $this->send( |
107 | $user->getEmail(), |
108 | $this->translator->trans('user.forget_password.subject', [], 'mail'), |
109 | $this->twig->render( |
110 | 'User/Mail/forget_password.html.twig', |
111 | [ |
112 | 'user' => $user, |
113 | ] |
114 | ) |
115 | ); |
116 | } |
117 | } |