Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
86.67% |
13 / 15 |
|
66.67% |
6 / 9 |
|
66.67% |
4 / 6 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| EmailSender | |
86.67% |
13 / 15 |
|
66.67% |
6 / 9 |
|
66.67% |
4 / 6 |
|
75.00% |
3 / 4 |
5.93 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| supports | |
100.00% |
1 / 1 |
|
75.00% |
3 / 4 |
|
50.00% |
1 / 2 |
|
100.00% |
1 / 1 |
1.12 | |||
| send | |
83.33% |
10 / 12 |
|
33.33% |
1 / 3 |
|
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
2.50 | |||
| getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Service\Notification\Sender; |
| 4 | |
| 5 | use App\Entity\Notification; |
| 6 | use App\Enum\NotificationLogStatus; |
| 7 | use App\Enum\NotificationType; |
| 8 | use App\Service\Notification\Renderer\NotificationRendererInterface; |
| 9 | use Symfony\Component\DependencyInjection\Attribute\Autowire; |
| 10 | use Symfony\Component\Mailer\Exception\TransportExceptionInterface; |
| 11 | use Symfony\Component\Mailer\MailerInterface; |
| 12 | use Symfony\Component\Mime\Address; |
| 13 | use Symfony\Component\Mime\Email; |
| 14 | |
| 15 | /** |
| 16 | * @author Wilhelm Zwertvaegher |
| 17 | */ |
| 18 | final readonly class EmailSender implements NotificationSenderInterface |
| 19 | { |
| 20 | private const string NAME = 'email'; |
| 21 | |
| 22 | public function __construct( |
| 23 | #[Autowire('%email.sender%')] |
| 24 | private string $emailSender, |
| 25 | #[Autowire('%app.name%')] |
| 26 | private string $appName, |
| 27 | private MailerInterface $mailer, |
| 28 | private NotificationRendererInterface $renderer, |
| 29 | private NotificationRendererInterface $textRenderer, |
| 30 | ) { |
| 31 | } |
| 32 | |
| 33 | public function supports(Notification $notification): bool |
| 34 | { |
| 35 | return in_array($notification->getType(), [NotificationType::CONTACT, NotificationType::SUGGESTION, NotificationType::REPORT]); |
| 36 | } |
| 37 | |
| 38 | public function send(Notification $notification): NotificationSenderResult |
| 39 | { |
| 40 | $content = $this->renderer->render($notification, $this); |
| 41 | |
| 42 | $email = new Email() |
| 43 | ->from(new Address($this->emailSender, $this->appName)) |
| 44 | ->to($notification->getRecipientEmail()) |
| 45 | ->priority(Email::PRIORITY_HIGH) |
| 46 | ->subject($notification->getSubject()) |
| 47 | ->text($this->textRenderer->render($notification, $this)) |
| 48 | ->html($content); |
| 49 | |
| 50 | try { |
| 51 | $this->mailer->send($email); |
| 52 | |
| 53 | return new NotificationSenderResult(NotificationLogStatus::SENT, 'Email sent'); |
| 54 | } catch (TransportExceptionInterface $exception) { |
| 55 | return new NotificationSenderResult(NotificationLogStatus::ERROR, 'Email could not be sent '.$exception->getMessage()); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | public function getName(): string |
| 60 | { |
| 61 | return self::NAME; |
| 62 | } |
| 63 | } |