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::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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