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

Twilio::getMessagingServiceSid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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