Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
30 / 30 |
|
92.86% |
13 / 14 |
|
16.67% |
3 / 18 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| |
100.00% |
30 / 30 |
|
92.86% |
13 / 14 |
|
16.67% |
3 / 18 |
|
100.00% |
2 / 2 |
35.36 | |
100.00% |
1 / 1 |
|
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| sendMail | |
100.00% |
26 / 26 |
|
92.31% |
12 / 13 |
|
11.76% |
2 / 17 |
|
100.00% |
1 / 1 |
30.73 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Service; |
| 4 | |
| 5 | use App\Application; |
| 6 | use App\Parameter; |
| 7 | use Psr\Log\LoggerInterface; |
| 8 | use Symfony\Component\Mailer\Exception\TransportExceptionInterface; |
| 9 | use Symfony\Component\Mailer\MailerInterface; |
| 10 | use Symfony\Component\Mime\Email; |
| 11 | use Throwable; |
| 12 | |
| 13 | class Mail |
| 14 | { |
| 15 | protected MailerInterface $mailer; |
| 16 | protected Application $application; |
| 17 | protected Parameter $parameter; |
| 18 | protected LoggerInterface $logger; |
| 19 | |
| 20 | public function __construct( |
| 21 | MailerInterface $mailer, |
| 22 | Application $application, |
| 23 | Parameter $parameter, |
| 24 | LoggerInterface $applicationLogger, |
| 25 | ) { |
| 26 | $this->mailer = $mailer; |
| 27 | $this->application = $application; |
| 28 | $this->parameter = $parameter; |
| 29 | $this->logger = $applicationLogger; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Send an email with the symfony mailer |
| 34 | * if the system doesn't run on production server, all mail are rerouted to the value of parameter |
| 35 | * smtpRedirectMailTo. On your local PC you can set |
| 36 | * 'APP_MAILER_DSN' => 'null://null' in you env.local.php file to bypass the mail. |
| 37 | * |
| 38 | * @param string|string[] $to |
| 39 | * |
| 40 | * @throws Throwable |
| 41 | * @throws TransportExceptionInterface |
| 42 | */ |
| 43 | public function sendMail(string|array $to, string $subject, string $body): void |
| 44 | { |
| 45 | try { |
| 46 | $dest = $to; |
| 47 | $log = ''; |
| 48 | $strTo = is_array($to) ? implode(', ', $to) : $to; |
| 49 | if ('prod' !== $this->application->getServerType() || $this->parameter->smtpForceRedirection) { |
| 50 | // redirect allways a mail to a specific mailbox |
| 51 | $dest = $this->parameter->smtpRedirectMailTo; |
| 52 | $log = 'Message redirigé à ' . $dest . 'au lieu du '; |
| 53 | $body = "This e-mail was rerouted.\n" . |
| 54 | 'Server type: ' . $this->application->getServerType() . "\n" . |
| 55 | 'Web site: ' . $this->application->getUrl() . "\n" . |
| 56 | 'Originally to: ' . $strTo . "\n" . |
| 57 | "-----------------------\n" . $body; |
| 58 | } |
| 59 | $log .= 'message destiné à ' . $strTo; |
| 60 | $this->logger->info($log, ['subject' => $subject, 'body' => $body]); |
| 61 | if (is_string($dest)) { |
| 62 | $dest = [$dest]; |
| 63 | } |
| 64 | $email = (new Email()) |
| 65 | ->to(...$dest) |
| 66 | ->subject($subject) |
| 67 | ->text($body); |
| 68 | |
| 69 | $this->mailer->send($email); |
| 70 | } catch (Throwable $e) { |
| 71 | $this->logger->error( |
| 72 | "Lors de l'envoi du mail l'erreur suivant a été détectée :" . $e->getMessage(), |
| 73 | (array) $e |
| 74 | ); |
| 75 | throw $e; |
| 76 | } |
| 77 | } |
| 78 | } |