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

ZonerSmsGatewayChannel::send()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.0208

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 27
ccs 11
cts 12
cp 0.9167
rs 8.439
c 2
b 0
f 0
cc 6
eloc 12
nc 16
nop 2
crap 6.0208
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