1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Twilio; |
4
|
|
|
|
5
|
|
|
use Twilio\Rest\Client as TwilioService; |
6
|
|
|
use NotificationChannels\Twilio\Exceptions\CouldNotSendNotification; |
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
|
13 |
|
public function __construct(TwilioService $twilioService, TwilioConfig $config) |
27
|
|
|
{ |
28
|
13 |
|
$this->twilioService = $twilioService; |
29
|
13 |
|
$this->config = $config; |
30
|
13 |
|
} |
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
|
11 |
|
public function sendMessage(TwilioMessage $message, $to, $useAlphanumericSender = false) |
42
|
|
|
{ |
43
|
11 |
|
if ($message instanceof TwilioSmsMessage) { |
44
|
8 |
|
if ($useAlphanumericSender && $sender = $this->getAlphanumericSender()) { |
45
|
2 |
|
$message->from($sender); |
46
|
2 |
|
} |
47
|
|
|
|
48
|
8 |
|
return $this->sendSmsMessage($message, $to); |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
if ($message instanceof TwilioNotifyMessage) { |
52
|
2 |
|
return $this->notifyMessage($message, $to); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
if ($message instanceof TwilioCallMessage) { |
56
|
|
|
return $this->makeCall($message, $to); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
throw CouldNotSendNotification::invalidMessageObject($message); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Send an sms message using the Twilio Service. |
64
|
|
|
* |
65
|
|
|
* @param TwilioSmsMessage $message |
66
|
8 |
|
* @param string $to |
67
|
|
|
* @return \Twilio\Rest\Api\V2010\Account\MessageInstance |
68
|
|
|
* @throws CouldNotSendNotification |
69
|
8 |
|
*/ |
70
|
7 |
|
protected function sendSmsMessage(TwilioSmsMessage $message, $to) |
71
|
7 |
|
{ |
72
|
|
|
$params = [ |
73
|
7 |
|
'from' => $this->getFrom($message), |
74
|
2 |
|
'body' => trim($message->content), |
75
|
2 |
|
]; |
76
|
|
|
|
77
|
7 |
|
if ($service_sid = $this->config->getServiceSid()) { |
78
|
7 |
|
$params['messagingServiceSid'] = $service_sid; |
79
|
7 |
|
} |
80
|
7 |
|
|
81
|
7 |
|
$this->fillOptionalParams($params, $message, [ |
82
|
7 |
|
'statusCallback', |
83
|
7 |
|
'statusCallbackMethod', |
84
|
7 |
|
'applicationSid', |
85
|
|
|
'maxPrice', |
86
|
7 |
|
'provideFeedback', |
87
|
1 |
|
'validityPeriod', |
88
|
1 |
|
]); |
89
|
1 |
|
|
90
|
1 |
|
if ($message instanceof TwilioMmsMessage) { |
91
|
|
|
$this->fillOptionalParams($params, $message, [ |
92
|
7 |
|
'mediaUrl', |
93
|
|
|
]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this->twilioService->messages->create($to, $params); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Send an notify message uing the Twilio service |
101
|
|
|
* |
102
|
|
|
* @param TwilioNotifyMessage $message |
103
|
2 |
|
* @param string $to |
104
|
|
|
* @return \Twilio |
105
|
|
|
*/ |
106
|
2 |
|
protected function notifyMessage(TwilioNotifyMessage $message, $to) |
107
|
2 |
|
{ |
108
|
|
|
$params = [ |
109
|
2 |
|
"toBinding" => '{"binding_type":"sms", "address":"'. $to .'"}', |
110
|
2 |
|
'body' => trim($message->content) |
111
|
2 |
|
]; |
112
|
2 |
|
|
113
|
2 |
|
$service_sid = $this->getServiceSid($message); |
114
|
2 |
|
|
115
|
2 |
|
return $this->twilioService |
116
|
2 |
|
->notify->services($service_sid) |
117
|
|
|
->notifications->create($params); |
118
|
2 |
|
} |
119
|
2 |
|
|
120
|
2 |
|
/** |
121
|
|
|
* Make a call using the Twilio Service. |
122
|
2 |
|
* |
123
|
|
|
* @param TwilioCallMessage $message |
124
|
|
|
* @param string $to |
125
|
|
|
* @return \Twilio\Rest\Api\V2010\Account\CallInstance |
126
|
|
|
* @throws CouldNotSendNotification |
127
|
|
|
*/ |
128
|
|
|
protected function makeCall(TwilioCallMessage $message, $to) |
129
|
|
|
{ |
130
|
|
|
$params = [ |
131
|
|
|
'url' => trim($message->content), |
132
|
10 |
|
]; |
133
|
|
|
|
134
|
10 |
|
$this->fillOptionalParams($params, $message, [ |
135
|
1 |
|
'statusCallback', |
136
|
|
|
'statusCallbackMethod', |
137
|
|
|
'method', |
138
|
9 |
|
'status', |
139
|
|
|
'fallbackUrl', |
140
|
|
|
'fallbackMethod', |
141
|
|
|
]); |
142
|
|
|
|
143
|
|
|
return $this->twilioService->calls->create( |
144
|
|
|
$to, |
145
|
|
|
$this->getFrom($message), |
146
|
2 |
|
$params |
147
|
|
|
); |
148
|
2 |
|
} |
149
|
2 |
|
|
150
|
|
|
/** |
151
|
|
|
* Get the from address from message, or config. |
152
|
|
|
* |
153
|
|
|
* @param TwilioMessage $message |
154
|
|
|
* @return string |
155
|
|
|
* @throws CouldNotSendNotification |
156
|
|
|
*/ |
157
|
|
|
protected function getFrom(TwilioMessage $message) |
158
|
|
|
{ |
159
|
9 |
|
if (! $from = $message->getFrom() ?: $this->config->getFrom()) { |
160
|
|
|
throw CouldNotSendNotification::missingFrom(); |
161
|
9 |
|
} |
162
|
9 |
|
|
163
|
3 |
|
return $from; |
164
|
3 |
|
} |
165
|
9 |
|
|
166
|
9 |
|
/** |
167
|
|
|
* Get the alphanumeric sender from config, if one exists. |
168
|
|
|
* |
169
|
|
|
* @return string|null |
170
|
|
|
*/ |
171
|
|
|
protected function getAlphanumericSender() |
172
|
|
|
{ |
173
|
|
|
if ($sender = $this->config->getAlphanumericSender()) { |
174
|
|
|
return $sender; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* get service sid |
180
|
|
|
* |
181
|
|
|
* @param TwilioMessage $message |
182
|
|
|
* @return string |
183
|
|
|
* @throws CouldNotSendNotification |
184
|
|
|
*/ |
185
|
|
|
protected function getServiceSid(TwilioMessage $message) |
186
|
|
|
{ |
187
|
|
|
if (! $service_sid = $message->getServiceSid()) { |
|
|
|
|
188
|
|
|
if (! $service_sid = $this->config->getServiceSid()) { |
189
|
|
|
throw CouldNotSendNotification::missingServiceSid(); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $service_sid; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param array $params |
198
|
|
|
* @param TwilioMessage $message |
199
|
|
|
* @param array $optionalParams |
200
|
|
|
* @return mixed |
201
|
|
|
*/ |
202
|
|
|
protected function fillOptionalParams(&$params, $message, $optionalParams) |
203
|
|
|
{ |
204
|
|
|
foreach ($optionalParams as $optionalParam) { |
205
|
|
|
if ($message->$optionalParam) { |
206
|
|
|
$params[$optionalParam] = $message->$optionalParam; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: