Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
87.50% |
14 / 16 |
|
66.67% |
6 / 9 |
|
66.67% |
4 / 6 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| EmailSender | |
87.50% |
14 / 16 |
|
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 | |
84.62% |
11 / 13 |
|
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\Notification\Infrastructure\Sender; |
| 4 | |
| 5 | use App\Notification\Domain\Model\Notification; |
| 6 | use App\Notification\Domain\Model\NotificationDispatchResult; |
| 7 | use App\Notification\Domain\Model\NotificationStatus; |
| 8 | use App\Notification\Domain\Model\NotificationType; |
| 9 | use App\Notification\Infrastructure\Renderer\NotificationRenderer; |
| 10 | use App\Notification\Infrastructure\Renderer\NotificationSubjectGenerator; |
| 11 | use Symfony\Component\Mailer\Exception\TransportExceptionInterface; |
| 12 | use Symfony\Component\Mailer\MailerInterface; |
| 13 | use Symfony\Component\Mime\Email; |
| 14 | |
| 15 | /** |
| 16 | * @author Wilhelm Zwertvaegher |
| 17 | */ |
| 18 | final readonly class EmailSender implements NotificationSender |
| 19 | { |
| 20 | |
| 21 | private const string NAME = 'email'; |
| 22 | |
| 23 | public function __construct( |
| 24 | private MailerInterface $mailer, |
| 25 | private NotificationRenderer $renderer, |
| 26 | private NotificationRenderer $textRenderer, |
| 27 | private NotificationSubjectGenerator $subjectGenerator |
| 28 | ) { |
| 29 | } |
| 30 | |
| 31 | public function supports(Notification $notification): bool |
| 32 | { |
| 33 | return in_array($notification->getType(), [NotificationType::WELCOME]); |
| 34 | } |
| 35 | |
| 36 | public function send(Notification $notification): NotificationSenderResult |
| 37 | { |
| 38 | $content = $this->renderer->render($notification, $this); |
| 39 | |
| 40 | $email = new Email() |
| 41 | // TODO : this should be set as a global parameter in services.yaml |
| 42 | ->from('hello@example.com') |
| 43 | ->to($notification->getIdentityInfo()->getEmail()) |
| 44 | ->priority(Email::PRIORITY_HIGH) |
| 45 | ->subject($this->subjectGenerator->generate($notification)) |
| 46 | ->text($this->textRenderer->render($notification, $this)) |
| 47 | ->html($content); |
| 48 | |
| 49 | try { |
| 50 | $this->mailer->send($email); |
| 51 | return new NotificationSenderResult(NotificationStatus::SENT, 'Email sent'); |
| 52 | } catch (TransportExceptionInterface $exception) { |
| 53 | return new NotificationSenderResult(NotificationStatus::ERROR, 'Email could not be sent ' . $exception->getMessage()); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | public function getName(): string |
| 58 | { |
| 59 | return self::NAME; |
| 60 | } |
| 61 | } |