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 ( 987681...4554fe )
by Gregorio
08:57 queued 03:48
created

Twilio::sendSmsMessage()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 0
cts 29
cp 0
rs 8.7377
c 0
b 0
f 0
cc 6
nc 12
nop 2
crap 42
1
<?php
2
3
namespace NotificationChannels\Twilio;
4
5
use Twilio\Rest\Client as TwilioService;
6
use NotificationChannels\Twilio\Exceptions\CouldNotSendNotification;
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
    public function __construct(TwilioService $twilioService, TwilioConfig $config)
27
    {
28
        $this->twilioService = $twilioService;
29
        $this->config = $config;
30
    }
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 \Twilio\Exceptions\TwilioException
40
     */
41
    public function sendMessage(TwilioMessage $message, $to, $useAlphanumericSender = false)
42
    {
43
        if ($message instanceof TwilioSmsMessage) {
44
            if ($useAlphanumericSender && $sender = $this->getAlphanumericSender()) {
45
                $message->from($sender);
46
            }
47
48
            return $this->sendSmsMessage($message, $to);
49
        }
50
51
        if ($message instanceof TwilioCallMessage) {
52
            return $this->makeCall($message, $to);
53
        }
54
55
        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
     */
65
    protected function sendSmsMessage(TwilioSmsMessage $message, $to)
66
    {
67
        $params = [
68
            'body' => trim($message->content),
69
        ];
70
71
        if ($messagingServiceSid = $this->getMessagingServiceSid($message)) {
72
            $params['messagingServiceSid'] = $messagingServiceSid;
73
        }
74
75
        if ($from = $this->getFrom($message)) {
76
            $params['from'] = $from;
77
        }
78
79
        if (! $from && ! $messagingServiceSid) {
80
            throw CouldNotSendNotification::missingFrom();
81
        }
82
83
        $this->fillOptionalParams($params, $message, [
84
            'statusCallback',
85
            'statusCallbackMethod',
86
            'applicationSid',
87
            'maxPrice',
88
            'provideFeedback',
89
            'validityPeriod',
90
        ]);
91
92
        if ($message instanceof TwilioMmsMessage) {
93
            $this->fillOptionalParams($params, $message, [
94
                'mediaUrl',
95
            ]);
96
        }
97
98
        return $this->twilioService->messages->create($to, $params);
99
    }
100
101
    /**
102
     * Make a call using the Twilio Service.
103
     *
104
     * @param TwilioCallMessage $message
105
     * @param string $to
106
     * @return \Twilio\Rest\Api\V2010\Account\CallInstance
107
     * @throws \Twilio\Exceptions\TwilioException
108
     */
109
    protected function makeCall(TwilioCallMessage $message, $to)
110
    {
111
        $params = [
112
            'url' => trim($message->content),
113
        ];
114
115
        $this->fillOptionalParams($params, $message, [
116
            'statusCallback',
117
            'statusCallbackMethod',
118
            'method',
119
            'status',
120
            'fallbackUrl',
121
            'fallbackMethod',
122
        ]);
123
124
        if (! $from = $this->getFrom($message)) {
125
            throw CouldNotSendNotification::missingFrom();
126
        }
127
128
        return $this->twilioService->calls->create(
129
            $to,
130
            $from,
131
            $params
132
        );
133
    }
134
135
    /**
136
     * Get the from address from message, or config.
137
     *
138
     * @param TwilioMessage $message
139
     * @return string
140
     */
141
    protected function getFrom(TwilioMessage $message)
142
    {
143
        return $message->getFrom() ?: $this->config->getFrom();
144
    }
145
146
    /**
147
     * Get the messaging service SID from message, or config.
148
     *
149
     * @param TwilioSmsMessage $message
150
     * @return string
151
     */
152
    protected function getMessagingServiceSid(TwilioSmsMessage $message)
153
    {
154
        return $message->getMessagingServiceSid() ?: $this->config->getServiceSid();
155
    }
156
157
    /**
158
     * Get the alphanumeric sender from config, if one exists.
159
     *
160
     * @return string|null
161
     */
162
    protected function getAlphanumericSender()
163
    {
164
        if ($sender = $this->config->getAlphanumericSender()) {
165
            return $sender;
166
        }
167
    }
168
169
    /**
170
     * @param array $params
171
     * @param TwilioMessage $message
172
     * @param array $optionalParams
173
     * @return Twilio
174
     */
175
    protected function fillOptionalParams(&$params, $message, $optionalParams)
176
    {
177
        foreach ($optionalParams as $optionalParam) {
178
            if ($message->$optionalParam) {
179
                $params[$optionalParam] = $message->$optionalParam;
180
            }
181
        }
182
183
        return $this;
184
    }
185
}
186