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 (#90)
by Atymic
02:14
created

Twilio   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 9
dl 0
loc 188
ccs 48
cts 52
cp 0.9231
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B sendMessage() 0 24 7
B sendSmsMessage() 0 42 7
A makeCall() 0 25 2
A getFrom() 0 4 2
A getMessagingServiceSid() 0 4 2
A getAlphanumericSender() 0 4 1
A fillOptionalParams() 0 10 3
1
<?php
2
3
namespace NotificationChannels\Twilio;
4
5
use NotificationChannels\Twilio\Exceptions\CouldNotSendNotification;
6
use Twilio\Exceptions\TwilioException;
7
use Twilio\Rest\Api\V2010\Account\CallInstance;
8
use Twilio\Rest\Api\V2010\Account\MessageInstance;
9
use Twilio\Rest\Client as TwilioService;
10
11
class Twilio
12
{
13
    /** @var TwilioService */
14
    protected $twilioService;
15
16
    /** @var TwilioConfig */
17
    private $config;
18
19 14
    public function __construct(TwilioService $twilioService, TwilioConfig $config)
20
    {
21 14
        $this->twilioService = $twilioService;
22 14
        $this->config = $config;
23 14
    }
24
25
    /**
26
     * Send a TwilioMessage to the a phone number.
27
     *
28
     * @param TwilioMessage $message
29
     * @param string|null $to
30
     * @param bool $useAlphanumericSender
31
     *
32
     * @return mixed
33
     * @throws TwilioException
34
     * @throws CouldNotSendNotification
35
     */
36 12
    public function sendMessage(TwilioMessage $message, ?string $to, bool $useAlphanumericSender = false)
37
    {
38
        try {
39 12
            if ($message instanceof TwilioSmsMessage) {
40 9
                if ($useAlphanumericSender && $sender = $this->getAlphanumericSender()) {
41 2
                    $message->from($sender);
42
                }
43
44 9
                return $this->sendSmsMessage($message, $to);
45
            }
46
47 3
            if ($message instanceof TwilioCallMessage) {
48 3
                return $this->makeCall($message, $to);
49
            }
50 1
        } catch (TwilioException $e) {
51
            if ($this->config->isIgnoredErrorCode($e->getCode())) {
52
                return null;
53
            }
54
55
            throw $e;
56
        }
57
58 1
        throw CouldNotSendNotification::invalidMessageObject($message);
59
    }
60
61
    /**
62
     * Send an sms message using the Twilio Service.
63
     *
64
     * @param TwilioSmsMessage $message
65
     * @param string|null $to
66
     *
67
     * @return MessageInstance
68
     * @throws CouldNotSendNotification
69
     * @throws TwilioException
70
     */
71 9
    protected function sendSmsMessage(TwilioSmsMessage $message, ?string $to): MessageInstance
72
    {
73 9
        $debugTo = $this->config->getDebugTo();
74
75 9
        if ($debugTo !== null) {
76 1
            $to = $debugTo;
77
        }
78
79
        $params = [
80 9
            'body' => trim($message->content),
81
        ];
82
83 9
        if ($messagingServiceSid = $this->getMessagingServiceSid($message)) {
84 2
            $params['messagingServiceSid'] = $messagingServiceSid;
85
        }
86
87 9
        if ($from = $this->getFrom($message)) {
88 8
            $params['from'] = $from;
89
        }
90
91 9
        if (empty($from) && empty($messagingServiceSid)) {
92 1
            throw CouldNotSendNotification::missingFrom();
93
        }
94
95 8
        $this->fillOptionalParams($params, $message, [
96 8
            'statusCallback',
97
            'statusCallbackMethod',
98
            'applicationSid',
99
            'forceDelivery',
100
            'maxPrice',
101
            'provideFeedback',
102
            'validityPeriod',
103
        ]);
104
105 8
        if ($message instanceof TwilioMmsMessage) {
106 1
            $this->fillOptionalParams($params, $message, [
107 1
                'mediaUrl',
108
            ]);
109
        }
110
111 8
        return $this->twilioService->messages->create($to, $params);
112
    }
113
114
    /**
115
     * Make a call using the Twilio Service.
116
     *
117
     * @param TwilioCallMessage $message
118
     * @param string|null $to
119
     *
120
     * @return CallInstance
121
     * @throws TwilioException
122
     * @throws CouldNotSendNotification
123
     */
124 2
    protected function makeCall(TwilioCallMessage $message, ?string $to): CallInstance
125
    {
126
        $params = [
127 2
            'url' => trim($message->content),
128
        ];
129
130 2
        $this->fillOptionalParams($params, $message, [
131 2
            'statusCallback',
132
            'statusCallbackMethod',
133
            'method',
134
            'status',
135
            'fallbackUrl',
136
            'fallbackMethod',
137
        ]);
138
139 2
        if (! $from = $this->getFrom($message)) {
140
            throw CouldNotSendNotification::missingFrom();
141
        }
142
143 2
        return $this->twilioService->calls->create(
144 2
            $to,
145
            $from,
146
            $params
147
        );
148
    }
149
150
    /**
151
     * Get the from address from message, or config.
152
     *
153
     * @param TwilioMessage $message
154
     * @return string|null
155
     */
156 11
    protected function getFrom(TwilioMessage $message): ?string
157
    {
158 11
        return $message->getFrom() ?: $this->config->getFrom();
159
    }
160
161
    /**
162
     * Get the messaging service SID from message, or config.
163
     *
164
     * @param TwilioSmsMessage $message
165
     * @return string|null
166
     */
167 9
    protected function getMessagingServiceSid(TwilioSmsMessage $message): ?string
168
    {
169 9
        return $message->getMessagingServiceSid() ?: $this->config->getServiceSid();
170
    }
171
172
    /**
173
     * Get the alphanumeric sender from config, if one exists.
174
     *
175
     * @return string|null
176
     */
177 2
    protected function getAlphanumericSender(): ?string
178
    {
179 2
        return $this->config->getAlphanumericSender();
180
    }
181
182
    /**
183
     * @param array $params
184
     * @param TwilioMessage $message
185
     * @param array $optionalParams
186
     * @return Twilio
187
     */
188 10
    protected function fillOptionalParams(&$params, $message, $optionalParams): self
189
    {
190 10
        foreach ($optionalParams as $optionalParam) {
191 10
            if ($message->$optionalParam) {
192 4
                $params[$optionalParam] = $message->$optionalParam;
193
            }
194
        }
195
196 10
        return $this;
197
    }
198
}
199