SmsChannel::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Notifier\Channel\Sms;
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 SmsChannel implements Channel
14
{
15
    public const NAME = 'sms';
16
17
    /** @var Texter */
18
    private $texter;
19
20
    /** @var string|null */
21
    private $defaultSenderPhoneNumber;
22
23 9
    public function __construct(Texter $texter, string $defaultSenderPhoneNumber = null)
24
    {
25 9
        $this->texter = $texter;
26 9
        $this->defaultSenderPhoneNumber = $defaultSenderPhoneNumber;
27 9
    }
28
29 4
    public function getName(): string
30
    {
31 4
        return self::NAME;
32
    }
33
34 7
    public function send(Notification $notification, Recipient $recipient): void
35
    {
36 7
        if (!$notification instanceof SmsNotification) {
37 1
            return;
38
        }
39
40 6
        if (null === ($recipientPhoneNumber = $recipient->getRecipientContact(self::NAME, $notification))) {
41 1
            return;
42
        }
43
44 5
        $message = $notification->toSmsMessage($recipient);
45 5
        $message->to($recipientPhoneNumber);
46
47 5
        if (null === $message->getFrom() && null !== $this->defaultSenderPhoneNumber) {
48 3
            $message->from($this->defaultSenderPhoneNumber);
49
        }
50
51
        try {
52 5
            $this->texter->send($message);
53 1
        } catch (SendingMessageFailed $error) {
54 1
            throw SendingNotificationFailed::for(self::NAME, $notification, $recipient, $error);
55
        }
56 4
    }
57
}
58