Completed
Push — master ( d834aa...40bc03 )
by antikainen
01:56
created

ZonerSmsGatewayChannel::send()   C

Complexity

Conditions 8
Paths 32

Size

Total Lines 32
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 8.1867

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 32
ccs 12
cts 14
cp 0.8571
rs 5.3846
cc 8
eloc 14
nc 32
nop 2
crap 8.1867
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 10
    public function __construct(ZonerSmsGateway $gateway)
16
    {
17 10
        $this->gateway = $gateway;
18 10
    }
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 10
    public function send($notifiable, Notification $notification)
30
    {
31 10
        $message = $notification->toZonerSmsGateway($notifiable);
32
33 10
        if (is_string($message)) {
34
            $message = new ZonerSmsGatewayMessage($message);
35
        }
36
37
        // Use the receiver from message, if defined:
38 10
        $receiver = $message->receiver;
39
40
        // Otherwise use the receiver given by notifiable routeNotificationForZonerSmsGateway:
41 10
        if (empty($receiver) && method_exists($notifiable, 'routeNotificationForZonerSmsGateway')) {
42 8
            $receiver = $notifiable->routeNotificationForZonerSmsGateway();
43
        }
44
45
        // Otherwise use the receiver given by notifiable generic routing method:
46 10
        if (empty($receiver)) {
47 1
            $receiver = $notifiable->routeNotificationFor(self::class);
48
        }
49
50
        // As the last resort, try to get the phone_number attribute from notifiable:
51 10
        if (empty($receiver) && isset($notifiable->phone_number)) {
52 1
            $receiver = $notifiable->phone_number;
53
        }
54
55 10
        if (empty($receiver)) {
56
            throw CouldNotSendNotification::receiverNotProvided();
57
        }
58
59 10
        return $this->gateway->sendMessage($receiver, trim($message->content), $message->sender);
60
    }
61
}
62