Completed
Push — master ( 953e3f...307f6c )
by Simon
03:54 queued 02:56
created

SipgateChannel   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 70
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A send() 0 11 1
A addRecipient() 0 14 3
A addSmsId() 0 8 2
1
<?php
2
3
namespace Simonkub\Laravel\Notifications\Sipgate;
4
5
use Illuminate\Notifications\Notification;
6
use Simonkub\Laravel\Notifications\Sipgate\Exceptions\CouldNotSendNotification;
7
8
class SipgateChannel
9
{
10
    /**
11
     * @var SipgateClient
12
     */
13
    protected $client;
14
15
    /**
16
     * @var string
17
     */
18
    protected $smsId;
19
20 7
    public function __construct(SipgateClient $client, string $smsId)
21
    {
22 7
        $this->client = $client;
23 7
        $this->smsId = $smsId;
24 7
    }
25
26
    /**
27
     * Send the given notification.
28
     *
29
     * @param  mixed  $notifiable
30
     * @param  Notification  $notification
31
     *
32
     * @throws CouldNotSendNotification
33
     */
34 7
    public function send($notifiable, Notification $notification)
35
    {
36
        /** @var SipgateMessage $message */
37 7
        $message = $notification->toSipgate($notifiable);
38
39 7
        $this->addRecipient($message, $notifiable);
40
41 6
        $this->addSmsId($message);
42
43 6
        $this->client->send($message);
44 6
    }
45
46
    /**
47
     * @param  SipgateMessage  $message
48
     * @param $notifiable
49
     * @throws CouldNotSendNotification
50
     */
51 7
    protected function addRecipient(SipgateMessage $message, $notifiable)
52
    {
53 7
        if ($message->getRecipient()) {
54 1
            return;
55
        }
56
57 6
        if ($recipient = $notifiable->routeNotificationFor('sipgate', $notifiable)) {
58 5
            $message->recipient($recipient);
59
60 5
            return;
61
        }
62
63 1
        throw CouldNotSendNotification::noRecipient();
64
    }
65
66
    /**
67
     * @param  SipgateMessage  $message
68
     */
69 6
    protected function addSmsId(SipgateMessage $message)
70
    {
71 6
        if ($message->getSmsId()) {
72 1
            return;
73
        }
74
75 5
        $message->smsId($this->smsId);
76 5
    }
77
}
78