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
Push — master ( e5d320...581fb3 )
by Gregorio
01:59
created

Twilio::sendMessage()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 4
nop 3
crap 5
1
<?php
2
3
namespace NotificationChannels\Twilio;
4
5
use NotificationChannels\Twilio\Exceptions\CouldNotSendNotification;
6
use Twilio\Rest\Client as TwilioService;
7
8
class Twilio
9
{
10
    /**
11
     * @var TwilioService
12
     */
13
    protected $twilioService;
14
15
    /**
16
     * @var TwilioConfig
17
     */
18
    private $config;
19
20
    /**
21
     * Twilio constructor.
22
     *
23
     * @param  TwilioService $twilioService
24
     * @param TwilioConfig   $config
25
     */
26 12
    public function __construct(TwilioService $twilioService, TwilioConfig $config)
27
    {
28 12
        $this->twilioService = $twilioService;
29 12
        $this->config = $config;
30 12
    }
31
32
    /**
33
     * Send a TwilioMessage to the a phone number.
34
     *
35
     * @param  TwilioMessage $message
36
     * @param  string        $to
37
     * @param bool           $useAlphanumericSender
38
     * @return mixed
39
     * @throws CouldNotSendNotification
40
     */
41 10
    public function sendMessage(TwilioMessage $message, $to, $useAlphanumericSender = false)
42
    {
43 10
        if ($message instanceof TwilioSmsMessage) {
44 7
            if ($useAlphanumericSender && $sender = $this->getAlphanumericSender()) {
45 2
                $message->from($sender);
46 2
            }
47
48 7
            return $this->sendSmsMessage($message, $to);
49
        }
50
51 3
        if ($message instanceof TwilioCallMessage) {
52 2
            return $this->makeCall($message, $to);
53
        }
54
55 1
        throw CouldNotSendNotification::invalidMessageObject($message);
56
    }
57
58
    /**
59
     * Send an sms message using the Twilio Service.
60
     *
61
     * @param TwilioSmsMessage $message
62
     * @param string           $to
63
     * @return \Twilio\Rest\Api\V2010\Account\MessageInstance
64
     * @throws CouldNotSendNotification
65
     */
66 7
    protected function sendSmsMessage(TwilioSmsMessage $message, $to)
67
    {
68
        $params = [
69 7
            'from' => $this->getFrom($message),
70 6
            'body' => trim($message->content),
71 6
        ];
72
73 6
        if ($message instanceof TwilioMmsMessage) {
74
            $params['mediaUrl'] = $message->mediaUrl;
75
        }
76
77 6
        if ($serviceSid = $this->config->getServiceSid()) {
78 2
            $params['messagingServiceSid'] = $serviceSid;
79 2
        }
80
81 6
        return $this->twilioService->messages->create($to, $params);
82
    }
83
84
    /**
85
     * Make a call using the Twilio Service.
86
     *
87
     * @param TwilioCallMessage $message
88
     * @param string            $to
89
     * @return \Twilio\Rest\Api\V2010\Account\CallInstance
90
     * @throws CouldNotSendNotification
91
     */
92 2
    protected function makeCall(TwilioCallMessage $message, $to)
93
    {
94 2
        return $this->twilioService->calls->create(
95 2
            $to,
96 2
            $this->getFrom($message),
97 2
            ['url' => trim($message->content)]
98 2
        );
99
    }
100
101
    /**
102
     * Get the from address from message, or config.
103
     *
104
     * @param TwilioMessage $message
105
     * @return string
106
     * @throws CouldNotSendNotification
107
     */
108 9
    protected function getFrom(TwilioMessage $message)
109
    {
110 9
        if (! $from = $message->getFrom() ?: $this->config->getFrom()) {
111 1
            throw CouldNotSendNotification::missingFrom();
112
        }
113
114 8
        return $from;
115
    }
116
117
    /**
118
     * Get the alphanumeric sender from config, if one exists.
119
     *
120
     * @return string|null
121
     */
122 2
    protected function getAlphanumericSender()
123
    {
124 2
        if ($sender = $this->config->getAlphanumericSender()) {
125 2
            return $sender;
126
        }
127
128
        return null;
129
    }
130
}
131