Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
86.67% covered (warning)
86.67%
13 / 15
66.67% covered (warning)
66.67%
6 / 9
66.67% covered (warning)
66.67%
4 / 6
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
EmailSender
86.67% covered (warning)
86.67%
13 / 15
66.67% covered (warning)
66.67%
6 / 9
66.67% covered (warning)
66.67%
4 / 6
75.00% covered (warning)
75.00%
3 / 4
5.93
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 supports
100.00% covered (success)
100.00%
1 / 1
75.00% covered (warning)
75.00%
3 / 4
50.00% covered (danger)
50.00%
1 / 2
100.00% covered (success)
100.00%
1 / 1
1.12
 send
83.33% covered (warning)
83.33%
10 / 12
33.33% covered (danger)
33.33%
1 / 3
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
2.50
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Service\Notification\Sender;
4
5use App\Entity\Notification;
6use App\Enum\NotificationLogStatus;
7use App\Enum\NotificationType;
8use App\Service\Notification\Renderer\NotificationRendererInterface;
9use Symfony\Component\DependencyInjection\Attribute\Autowire;
10use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
11use Symfony\Component\Mailer\MailerInterface;
12use Symfony\Component\Mime\Address;
13use Symfony\Component\Mime\Email;
14
15/**
16 * @author Wilhelm Zwertvaegher
17 */
18final 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}