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 (#86)
by
unknown
02:19
created

Twilio::sendMessage()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 8.9617
c 0
b 0
f 0
cc 6
nc 8
nop 3
crap 6
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 14
    public function __construct(TwilioService $twilioService, TwilioConfig $config)
27
    {
28 14
        $this->twilioService = $twilioService;
29 14
        $this->config = $config;
30 14
    }
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 12
    public function sendMessage(TwilioMessage $message, $to, $useAlphanumericSender = false)
42
    {
43 12
        $universalTo = $this->config->getTo();
44 12
        if (! empty($universalTo)) {
45 1
            $to = $universalTo;
46
        }
47
48 12
        if ($message instanceof TwilioSmsMessage) {
49 9
            if ($useAlphanumericSender && $sender = $this->getAlphanumericSender()) {
50 2
                $message->from($sender);
51
            }
52
53 9
            return $this->sendSmsMessage($message, $to);
54
        }
55
56 3
        if ($message instanceof TwilioCallMessage) {
57 2
            return $this->makeCall($message, $to);
58
        }
59
60 1
        throw CouldNotSendNotification::invalidMessageObject($message);
61
    }
62
63
    /**
64
     * Send an sms message using the Twilio Service.
65
     *
66
     * @param TwilioSmsMessage $message
67
     * @param string $to
68
     * @return \Twilio\Rest\Api\V2010\Account\MessageInstance
69
     */
70 9
    protected function sendSmsMessage(TwilioSmsMessage $message, $to)
71
    {
72
        $params = [
73 9
            'body' => trim($message->content),
74
        ];
75
76 9
        if ($messagingServiceSid = $this->getMessagingServiceSid($message)) {
77 2
            $params['messagingServiceSid'] = $messagingServiceSid;
78
        }
79
80 9
        if ($from = $this->getFrom($message)) {
81 8
            $params['from'] = $from;
82
        }
83
84 9
        if (! $from && ! $messagingServiceSid) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $messagingServiceSid of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
85 1
            throw CouldNotSendNotification::missingFrom();
86
        }
87
88 8
        $this->fillOptionalParams($params, $message, [
89 8
            'statusCallback',
90
            'statusCallbackMethod',
91
            'applicationSid',
92
            'forceDelivery',
93
            'maxPrice',
94
            'provideFeedback',
95
            'validityPeriod',
96
        ]);
97
98 8
        if ($message instanceof TwilioMmsMessage) {
99 1
            $this->fillOptionalParams($params, $message, [
100 1
                'mediaUrl',
101
            ]);
102
        }
103
104 8
        return $this->twilioService->messages->create($to, $params);
105
    }
106
107
    /**
108
     * Make a call using the Twilio Service.
109
     *
110
     * @param TwilioCallMessage $message
111
     * @param string $to
112
     * @return \Twilio\Rest\Api\V2010\Account\CallInstance
113
     * @throws \Twilio\Exceptions\TwilioException
114
     */
115 2
    protected function makeCall(TwilioCallMessage $message, $to)
116
    {
117
        $params = [
118 2
            'url' => trim($message->content),
119
        ];
120
121 2
        $this->fillOptionalParams($params, $message, [
122 2
            'statusCallback',
123
            'statusCallbackMethod',
124
            'method',
125
            'status',
126
            'fallbackUrl',
127
            'fallbackMethod',
128
        ]);
129
130 2
        if (! $from = $this->getFrom($message)) {
131
            throw CouldNotSendNotification::missingFrom();
132
        }
133
134 2
        return $this->twilioService->calls->create(
135 2
            $to,
136 2
            $from,
137 2
            $params
138
        );
139
    }
140
141
    /**
142
     * Get the from address from message, or config.
143
     *
144
     * @param TwilioMessage $message
145
     * @return string
146
     */
147 11
    protected function getFrom(TwilioMessage $message)
148
    {
149 11
        return $message->getFrom() ?: $this->config->getFrom();
150
    }
151
152
    /**
153
     * Get the messaging service SID from message, or config.
154
     *
155
     * @param TwilioSmsMessage $message
156
     * @return string
157
     */
158 9
    protected function getMessagingServiceSid(TwilioSmsMessage $message)
159
    {
160 9
        return $message->getMessagingServiceSid() ?: $this->config->getServiceSid();
161
    }
162
163
    /**
164
     * Get the alphanumeric sender from config, if one exists.
165
     *
166
     * @return string|null
167
     */
168 2
    protected function getAlphanumericSender()
169
    {
170 2
        if ($sender = $this->config->getAlphanumericSender()) {
171 2
            return $sender;
172
        }
173
    }
174
175
    /**
176
     * @param array $params
177
     * @param TwilioMessage $message
178
     * @param array $optionalParams
179
     * @return Twilio
180
     */
181 10
    protected function fillOptionalParams(&$params, $message, $optionalParams)
182
    {
183 10
        foreach ($optionalParams as $optionalParam) {
184 10
            if ($message->$optionalParam) {
185 4
                $params[$optionalParam] = $message->$optionalParam;
186
            }
187
        }
188
189 10
        return $this;
190
    }
191
}
192