Completed
Push — master ( ef0171...3efde9 )
by antikainen
01:47
created

ZonerSmsGatewayChannel   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.33%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 49
ccs 14
cts 15
cp 0.9333
rs 10
c 2
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B send() 0 27 6
1
<?php
2
3
namespace NotificationChannels\ZonerSmsGateway;
4
5
use Illuminate\Notifications\Notification;
6
use NotificationChannels\ZonerSmsGateway\Exceptions\CouldNotSendNotification;
7
8
class ZonerSmsGatewayChannel
9
{
10
    /**
11
     * @var ZonerSmsGateway
12
     */
13
    protected $gateway;
14
15 4
    public function __construct(ZonerSmsGateway $gateway)
16
    {
17 4
        $this->gateway = $gateway;
18 4
    }
19
20
    /**
21
     * Send the given notification.
22
     *
23
     * @param mixed $notifiable
24
     * @param Notification $notification
25
     *
26
     * @return tracking number
27
     * @throws CouldNotSendNotification
28
     */
29 4
    public function send($notifiable, Notification $notification)
30
    {
31 4
        $message = $notification->toZonerSmsGateway($notifiable);
32
33 4
        if (is_string($message)) {
34
            $message = new ZonerSmsGatewayMessage($message);
35
        }
36
37
        // Use the receiver from message, if defined:
38 4
        $receiver = $message->receiver;
39
40
        // Otherwise use the receiver given by notifiable:
41 4
        if (empty($receiver)) {
42 3
            $receiver = $notifiable->routeNotificationFor(ZonerSmsGatewayChannel::class);
43
        }
44
45
        // As the last resort, try to get the phone_number attribute from notifiable:
46 4
        if (empty($receiver) && isset($notifiable->phone_number)) {
47 1
            $receiver = $notifiable->phone_number;
48
        }
49
50 4
        if (empty($receiver)) {
51 2
            throw CouldNotSendNotification::receiverNotProvided();
52
        }
53
54 2
        return $this->gateway->sendMessage($receiver, trim($message->content), $message->sender);
55
    }
56
}
57