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 (#30)
by Craig
05:58
created

Twilio   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 97.3%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 8
dl 0
loc 118
ccs 36
cts 37
cp 0.973
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B sendMessage() 0 16 5
A sendSmsMessage() 0 16 3
A makeCall() 0 8 1
A getFrom() 0 8 3
A getAlphanumericSender() 0 6 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 11
    public function __construct(TwilioService $twilioService, TwilioConfig $config)
27
    {
28 11
        $this->twilioService = $twilioService;
29 11
        $this->config = $config;
30 11
    }
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 CouldNotSendNotification
40
     */
41 10
    public function sendMessage(TwilioMessage $message, $to, $useAlphanumericSender = false)
42
    {
43 10
        if ($message instanceof TwilioSmsMessage) {
44 7
            if ($useAlphanumericSender && $sender = $this->getAlphanumericSender()) {
45 2
                $message->from($sender);
46 2
            }
47
48 7
            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 7
    protected function sendSmsMessage(TwilioSmsMessage $message, $to)
66
    {
67
        $params = [
68 7
            'body' => trim($message->content),
69 7
        ];
70
71 7
        if ($serviceSid = $this->config->getServiceSid()) {
72 2
            $params['messagingServiceSid'] = $serviceSid;
73 2
        }
74
75 7
        if (!isset($params['messagingServiceSid'])) {
76 5
            $params['from'] = $this->getFrom($message);
77 4
        }
78
79 6
        return $this->twilioService->messages->create($to, $params);
80
    }
81
82
    /**
83
     * Make a call using the Twilio Service.
84
     *
85
     * @param TwilioCallMessage $message
86
     * @param string            $to
87
     * @return \Twilio\Rest\Api\V2010\Account\CallInstance
88
     */
89 2
    protected function makeCall(TwilioCallMessage $message, $to)
90
    {
91 2
        return $this->twilioService->calls->create(
92 2
            $to,
93 2
            $this->getFrom($message),
94 2
            ['url' => trim($message->content)]
95 2
        );
96
    }
97
98
    /**
99
     * Get the from address from message, or config.
100
     *
101
     * @param TwilioMessage $message
102
     * @return string
103
     * @throws CouldNotSendNotification
104
     */
105 7
    protected function getFrom(TwilioMessage $message)
106
    {
107 7
        if (! $from = $message->getFrom() ?: $this->config->getFrom()) {
108 1
            throw CouldNotSendNotification::missingFrom();
109
        }
110
111 6
        return $from;
112
    }
113
114
    /**
115
     * Get the alphanumeric sender from config, if one exists.
116
     *
117
     * @return string|null
118
     */
119 2
    protected function getAlphanumericSender()
120
    {
121 2
        if ($sender = $this->config->getAlphanumericSender()) {
122 2
            return $sender;
123
        }
124
    }
125
}
126