UnisenderChannel   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
B send() 0 22 4
A sendMessage() 0 15 4
A __construct() 0 3 1
1
<?php
2
3
namespace NotificationChannels\Unisender;
4
5
use Illuminate\Notifications\Notification;
6
7
class UnisenderChannel
8
{
9
    /**
10
     * @var UnisenderApi
11
     */
12
    protected $api;
13
14
    /**
15
     * UnisenderChannel constructor.
16
     *
17
     * @param UnisenderApi $api
18
     */
19
    public function __construct(UnisenderApi $api)
20
    {
21
        $this->api = $api;
22
    }
23
24
    /**
25
     * @param mixed        $notifiable
26
     * @param Notification $notification
27
     *
28
     * @return mixed
29
     */
30
    public function send($notifiable, Notification $notification)
31
    {
32
        $to = $notifiable->routeNotificationFor('unisender');
33
34
        if (!$to) {
35
            throw new \InvalidArgumentException('No receivers.');
36
        }
37
38
        if (!method_exists($notification, 'toUnisender')) {
39
            throw new \InvalidArgumentException('Method "toUnisender" does not exists on given notification instance.');
40
        }
41
42
        /** @var UnisenderMessage $message */
43
        $message = call_user_func_array([$notification, 'toUnisender'], [$notifiable]);
44
45
        if (!($message instanceof UnisenderMessage)) {
0 ignored issues
show
introduced by
$message is always a sub-type of NotificationChannels\Unisender\UnisenderMessage.
Loading history...
46
            throw new \InvalidArgumentException('Message is not an instance of UnisenderMessage.');
47
        }
48
49
        $message->to($to);
50
51
        return $this->sendMessage($message);
52
    }
53
54
    protected function sendMessage(UnisenderMessage $message)
55
    {
56
        if (!is_null($message->token)) {
57
            $this->api->setToken($message->token);
58
        }
59
60
        try {
61
            return $this->api->sendSms($message);
62
        } catch (\Exception $e) {
63
            if (!$message->silent) {
64
                throw $e;
65
            }
66
        }
67
68
        return null;
69
    }
70
}
71