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 (#92)
by
unknown
03:11
created

TwilioChannel::send()   A

Complexity

Conditions 5
Paths 20

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7.5412

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 8
cts 15
cp 0.5333
rs 9.1608
c 0
b 0
f 0
cc 5
nc 20
nop 2
crap 7.5412
1
<?php
2
3
namespace NotificationChannels\Twilio;
4
5
use Exception;
6
use Illuminate\Contracts\Events\Dispatcher;
7
use Illuminate\Notifications\Events\NotificationFailed;
8
use Illuminate\Notifications\Notification;
9
use NotificationChannels\Twilio\Exceptions\CouldNotSendNotification;
10
11
class TwilioChannel
12
{
13
    /**
14
     * @var Twilio
15
     */
16
    protected $twilio;
17
18
    /**
19
     * @var Dispatcher
20
     */
21
    protected $events;
22
23
    /**
24
     * TwilioChannel constructor.
25
     *
26
     * @param Twilio     $twilio
27
     * @param Dispatcher $events
28
     */
29 8
    public function __construct(Twilio $twilio, Dispatcher $events)
30
    {
31 8
        $this->twilio = $twilio;
32 8
        $this->events = $events;
33 8
    }
34
35
    /**
36
     * Send the given notification.
37
     *
38
     * @param  mixed                                  $notifiable
39
     * @param  \Illuminate\Notifications\Notification $notification
40
     * @return mixed
41
     * @throws CouldNotSendNotification
42
     */
43 8
    public function send($notifiable, Notification $notification)
44
    {
45
        try {
46 8
            $to = $this->getTo($notifiable);
47 8
            $message = $notification->toTwilio($notifiable);
0 ignored issues
show
Bug introduced by
The method toTwilio() does not seem to exist on object<Illuminate\Notifications\Notification>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48 8
            $useSender = $this->canReceiveAlphanumericSender($notifiable);
49
50 8
            if (is_string($message)) {
51 1
                $message = new TwilioSmsMessage($message);
52
            }
53
54 8
            if (! $message instanceof TwilioMessage) {
55
                throw CouldNotSendNotification::invalidMessageObject($message);
56
            }
57
58 8
            return $this->twilio->sendMessage($message, $to, $useSender);
59
        } catch (Exception $exception) {
60
            $event = new NotificationFailed($notifiable, $notification, 'twilio', ['message' => $exception->getMessage(), 'exception' => $exception]);
61
            if (function_exists('event')) { // Use event helper when possible to add Lumen support
62
                event($event);
63
            } else {
64
                $this->events->fire($event);
0 ignored issues
show
Bug introduced by
The method fire() does not seem to exist on object<Illuminate\Contracts\Events\Dispatcher>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
            }
66
            
67
            // by throwing exception, NotificationSent event is not triggered and we only trigger NotificationFailed above instead
68
            throw new \Exception('Notification failed '.$exception->getMessage());
69
        }
70
    }
71
72
    /**
73
     * Get the address to send a notification to.
74
     *
75
     * @param mixed $notifiable
76
     * @return mixed
77
     * @throws CouldNotSendNotification
78
     */
79 8
    protected function getTo($notifiable)
80
    {
81 8
        if ($notifiable->routeNotificationFor('twilio')) {
82 1
            return $notifiable->routeNotificationFor('twilio');
83
        }
84 7
        if (isset($notifiable->phone_number)) {
85 7
            return $notifiable->phone_number;
86
        }
87
88
        throw CouldNotSendNotification::invalidReceiver();
89
    }
90
91
    /**
92
     * Get the alphanumeric sender.
93
     *
94
     * @param $notifiable
95
     * @return mixed|null
96
     * @throws CouldNotSendNotification
97
     */
98 8
    protected function canReceiveAlphanumericSender($notifiable)
99
    {
100 8
        return method_exists($notifiable, 'canReceiveAlphanumericSender') &&
101 8
        $notifiable->canReceiveAlphanumericSender();
102
    }
103
}
104