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
Push — master ( 8da4ae...00055f )
by Gregorio
09:18
created

Twilio::getFrom()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 1
crap 12
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
    public function __construct(TwilioService $twilioService, TwilioConfig $config)
27
    {
28
        $this->twilioService = $twilioService;
29
        $this->config = $config;
30
    }
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
    public function sendMessage(TwilioMessage $message, $to, $useAlphanumericSender = false)
42
    {
43
        if ($message instanceof TwilioSmsMessage) {
44
            if ($useAlphanumericSender && $sender = $this->getAlphanumericSender()) {
45
                $message->from($sender);
46
            }
47
48
            return $this->sendSmsMessage($message, $to);
49
        }
50
51
        if ($message instanceof TwilioCallMessage) {
52
            return $this->makeCall($message, $to);
53
        }
54
55
        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
     * @throws CouldNotSendNotification
65
     */
66
    protected function sendSmsMessage(TwilioSmsMessage $message, $to)
67
    {
68
        $params = [
69
            'from' => $this->getFrom($message),
70
            'body' => trim($message->content),
71
        ];
72
73
        if ($serviceSid = $this->config->getServiceSid()) {
74
            $params['messagingServiceSid'] = $serviceSid;
75
        }
76
77
        return $this->twilioService->messages->create($to, $params);
78
    }
79
80
    /**
81
     * Make a call using the Twilio Service.
82
     *
83
     * @param TwilioCallMessage $message
84
     * @param string            $to
85
     * @return \Twilio\Rest\Api\V2010\Account\CallInstance
86
     * @throws CouldNotSendNotification
87
     */
88
    protected function makeCall(TwilioCallMessage $message, $to)
89
    {
90
        return $this->twilioService->calls->create(
91
            $to,
92
            $this->getFrom($message),
93
            ['url' => trim($message->content)]
94
        );
95
    }
96
97
    /**
98
     * Get the from address from message, or config.
99
     *
100
     * @param TwilioMessage $message
101
     * @return string
102
     * @throws CouldNotSendNotification
103
     */
104
    protected function getFrom(TwilioMessage $message)
105
    {
106
        if (! $from = $message->getFrom() ?: $this->config->getFrom()) {
107
            throw CouldNotSendNotification::missingFrom();
108
        }
109
110
        return $from;
111
    }
112
113
    /**
114
     * Get the alphanumeric sender from config, if one exists.
115
     *
116
     * @return string|null
117
     */
118
    protected function getAlphanumericSender()
119
    {
120
        if ($sender = $this->config->getAlphanumericSender()) {
121
            return $sender;
122
        }
123
124
        return null;
125
    }
126
}
127