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 | } |
Below are the source code lines that represent each code path as identified by Xdebug. Please note a path is not
necessarily coterminous with a line, a line may contain multiple paths and therefore show up more than once.
Please also be aware that some paths may include implicit rather than explicit branches, e.g. an if statement
always has an else as part of its logical flow even if you didn't write one.
| 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 | } |
| 61 | return self::NAME; |
| 62 | } |
| 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 | } |
| 33 | public function supports(Notification $notification): bool |
| 34 | { |
| 35 | return in_array($notification->getType(), [NotificationType::CONTACT, NotificationType::SUGGESTION, NotificationType::REPORT]); |
| 35 | return in_array($notification->getType(), [NotificationType::CONTACT, NotificationType::SUGGESTION, NotificationType::REPORT]); |
| 35 | return in_array($notification->getType(), [NotificationType::CONTACT, NotificationType::SUGGESTION, NotificationType::REPORT]); |
| 36 | } |
| 33 | public function supports(Notification $notification): bool |
| 34 | { |
| 35 | return in_array($notification->getType(), [NotificationType::CONTACT, NotificationType::SUGGESTION, NotificationType::REPORT]); |
| 35 | return in_array($notification->getType(), [NotificationType::CONTACT, NotificationType::SUGGESTION, NotificationType::REPORT]); |
| 35 | return in_array($notification->getType(), [NotificationType::CONTACT, NotificationType::SUGGESTION, NotificationType::REPORT]); |
| 36 | } |
| 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 | } |