Completed
Push — master ( af97d0...c93ab2 )
by Nikola
03:56
created

SmsChannel::send()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.1308

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 11
cts 13
cp 0.8462
rs 8.9297
c 0
b 0
f 0
cc 6
nc 6
nop 2
crap 6.1308
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 7
    public function __construct(Texter $texter, string $defaultSenderPhoneNumber = null)
24
    {
25 7
        $this->texter = $texter;
26 7
        $this->defaultSenderPhoneNumber = $defaultSenderPhoneNumber;
27 7
    }
28
29 4
    public function getName(): string
30
    {
31 4
        return self::NAME;
32
    }
33
34 5
    public function send(Notification $notification, Recipient $recipient): void
35
    {
36 5
        if (!$notification instanceof SmsNotification) {
37
            return;
38
        }
39
40 5
        if (null === ($recipientPhoneNumber = $recipient->getRecipientContact(self::NAME, $notification))) {
41 1
            return;
42
        }
43
44 4
        $message = $notification->toSmsMessage($recipient);
45 4
        $message->to($recipientPhoneNumber);
46
47 4
        if (null === $message->getFrom() && null !== $this->defaultSenderPhoneNumber) {
48
            $message->from($this->defaultSenderPhoneNumber);
49
        }
50
51
        try {
52 4
            $this->texter->send($message);
53 1
        } catch (SendingMessageFailed $error) {
54 1
            throw SendingNotificationFailed::for(self::NAME, $notification, $recipient, $error);
55
        }
56 3
    }
57
}
58