Completed
Push — master ( 570f1a...576848 )
by Nikola
03:44
created

SmsChannel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
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\Channel\Exception\SendingMessageFailed;
9
use Notifier\Channel\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 5
    public function __construct(Texter $texter)
21
    {
22 5
        $this->texter = $texter;
23 5
    }
24
25 2
    public function getName(): string
26
    {
27 2
        return self::NAME;
28
    }
29
30 4
    public function send(Notification $notification, Recipient $recipient): void
31
    {
32 4
        if (!$notification instanceof SmsNotification) {
33
            return;
34
        }
35
36 4
        if (null === ($recipientPhoneNumber = $recipient->getRecipientContact(self::NAME, $notification))) {
37 1
            return;
38
        }
39
40 3
        $message = $notification->toSmsMessage($recipient);
41 3
        $message->to($recipientPhoneNumber);
42
43
        try {
44 3
            $this->texter->send($message);
45 1
        } catch (SendingMessageFailed $error) {
46 1
            throw SendingNotificationFailed::dueTo($error, self::NAME);
47
        }
48 2
    }
49
}
50