GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#14)
by Casper
02:45
created

TwilioChannel::send()   B

Complexity

Conditions 6
Paths 19

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 8
Bugs 2 Features 1
Metric Value
c 8
b 2
f 1
dl 0
loc 28
ccs 0
cts 18
cp 0
rs 8.439
cc 6
eloc 15
nc 19
nop 2
crap 42
1
<?php
2
3
namespace NotificationChannels\Twilio;
4
5
use Exception;
6
use Illuminate\Notifications\Notification;
7
use Illuminate\Contracts\Events\Dispatcher;
8
use NotificationChannels\Twilio\Exceptions\CouldNotSendNotification;
9
use Illuminate\Notifications\Events\NotificationFailed;
10
use Services_Twilio as Twilio;
11
12
class TwilioChannel
13
{
14
    /**
15
     * @var Twilio
16
     */
17
    protected $twilio;
18
19
    /**
20
     * @var Dispatcher
21
     */
22
    protected $events;
23
24
    /**
25
     * Default 'from' from config.
26
     * @var string
27
     */
28
    protected $from;
29
30
    /**
31
     * TwilioChannel constructor.
32
     *
33
     * @param Twilio  $twilio
34
     * @param Dispatcher  $events
35
     */
36
    public function __construct(Twilio $twilio, Dispatcher $events, $from)
37
    {
38
        $this->twilio = $twilio;
39
        $this->events = $events;
40
        $this->from = $from;
41
    }
42
43
    /**
44
     * Send the given notification.
45
     *
46
     * @param  mixed  $notifiable
47
     * @param  \Illuminate\Notifications\Notification  $notification
48
     * @return mixed
49
     * @throws CouldNotSendNotification
50
     */
51
    public function send($notifiable, Notification $notification)
52
    {
53
        if (! $to = $notifiable->routeNotificationFor('twilio')) {
54
            if (! $to = $notifiable->phone_number) {
55
                return;
56
            }
57
        }
58
59
        try {
60
            $message = $notification->toTwilio($notifiable);
61
62
            if (is_string($message)) {
63
                $message = new TwilioSmsMessage($message);
64
            }
65
66
            if (! $message instanceof TwilioAbstractMessage) {
67
                throw CouldNotSendNotification::invalidMessageObject($message);
68
            }
69
70
            return $this->sendMessage($message, $to);
71
        } catch (Exception $exception) {
72
            $this->events->fire(
73
                new NotificationFailed($notifiable, $notification, 'twilio', ['message' => $exception->getMessage()])
74
            );
75
        }
76
77
        return;
78
    }
79
80
    /**
81
     * Send message to Twilio
82
     *
83
     * @param  TwilioAbstractMessage  $message
84
     * @param  string  $to
85
     * @return mixed
86
     *
87
     * @throws \NotificationChannels\Twilio\Exceptions\CouldNotSendNotification
88
     */
89
    protected function sendMessage(TwilioAbstractMessage $message, $to)
90
    {
91
        $from = $this->getFrom($message);
92
93
        if ($message instanceof TwilioSmsMessage) {
94
            return $this->twilio->account->messages->sendMessage(
95
                $from,
96
                $to,
97
                trim($message->content)
98
            );
99
        }
100
101
        if ($message instanceof TwilioCallMessage) {
102
            return $this->twilio->account->calls->create(
103
                $from,
104
                $to,
105
                trim($message->content)
106
            );
107
        }
108
109
        throw CouldNotSendNotification::invalidMessageObject($message);
110
    }
111
112
    protected function getFrom($message)
113
    {
114
        if (! $from = $message->from ?: $this->from) {
115
            throw CouldNotSendNotification::missingFrom();
116
        }
117
118
        return $from;
119
    }
120
}
121