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 (#73)
by Atymic
07:48 queued 06:19
created

Twilio   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 90.57%

Importance

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

8 Methods

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