EmailChannel::send()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 9.6333
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Notifier\Channel\Email;
6
7
use Notifier\Channel\Channel;
8
use Notifier\Exception\SendingMessageFailed;
9
use Notifier\Exception\SendingNotificationFailed;
10
use Notifier\Notification\Notification;
11
use Notifier\Recipient\Recipient;
12
13
final class EmailChannel implements Channel
14
{
15
    public const NAME = 'email';
16
17
    /** @var Mailer */
18
    private $mailer;
19
20 8
    public function __construct(Mailer $mailer)
21
    {
22 8
        $this->mailer = $mailer;
23 8
    }
24
25 4
    public function getName(): string
26
    {
27 4
        return self::NAME;
28
    }
29
30 5
    public function send(Notification $notification, Recipient $recipient): void
31
    {
32 5
        if (!$notification instanceof EmailNotification) {
33 1
            return;
34
        }
35
36 4
        if (null === ($recipientEmail = $recipient->getRecipientContact(self::NAME, $notification))) {
37 1
            return;
38
        }
39
40 3
        $message = $notification->toEmailMessage($recipient);
41 3
        $message->to($recipientEmail, $recipient->getRecipientName());
42
43
        try {
44 3
            $this->mailer->send($message);
45 1
        } catch (SendingMessageFailed $error) {
46 1
            throw SendingNotificationFailed::for(self::NAME, $notification, $recipient, $error);
47
        }
48 2
    }
49
}
50