3
3
namespace NotificationChannels \Twilio ;
4
4
5
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 ;
6
9
use Twilio \Rest \Client as TwilioService ;
7
10
8
11
class Twilio
9
12
{
10
- /**
11
- * @var TwilioService
12
- */
13
+ /** @var TwilioService */
13
14
protected $ twilioService ;
14
15
15
- /**
16
- * @var TwilioConfig
17
- */
18
- private $ config ;
16
+ /** @var TwilioConfig */
17
+ public $ config ;
19
18
20
- /**
21
- * Twilio constructor.
22
- *
23
- * @param TwilioService $twilioService
24
- * @param TwilioConfig $config
25
- */
26
19
public function __construct (TwilioService $ twilioService , TwilioConfig $ config )
27
20
{
28
21
$ this ->twilioService = $ twilioService ;
@@ -32,13 +25,15 @@ public function __construct(TwilioService $twilioService, TwilioConfig $config)
32
25
/**
33
26
* Send a TwilioMessage to the a phone number.
34
27
*
35
- * @param TwilioMessage $message
36
- * @param string $to
28
+ * @param TwilioMessage $message
29
+ * @param string|null $to
37
30
* @param bool $useAlphanumericSender
31
+ *
38
32
* @return mixed
39
- * @throws \Twilio\Exceptions\TwilioException
33
+ * @throws TwilioException
34
+ * @throws CouldNotSendNotification
40
35
*/
41
- public function sendMessage (TwilioMessage $ message , $ to , $ useAlphanumericSender = false )
36
+ public function sendMessage (TwilioMessage $ message , ? string $ to , bool $ useAlphanumericSender = false )
42
37
{
43
38
if ($ message instanceof TwilioSmsMessage) {
44
39
if ($ useAlphanumericSender && $ sender = $ this ->getAlphanumericSender ()) {
@@ -59,11 +54,20 @@ public function sendMessage(TwilioMessage $message, $to, $useAlphanumericSender
59
54
* Send an sms message using the Twilio Service.
60
55
*
61
56
* @param TwilioSmsMessage $message
62
- * @param string $to
63
- * @return \Twilio\Rest\Api\V2010\Account\MessageInstance
57
+ * @param string|null $to
58
+ *
59
+ * @return MessageInstance
60
+ * @throws CouldNotSendNotification
61
+ * @throws TwilioException
64
62
*/
65
- protected function sendSmsMessage (TwilioSmsMessage $ message , $ to )
63
+ protected function sendSmsMessage (TwilioSmsMessage $ message , ? string $ to ): MessageInstance
66
64
{
65
+ $ debugTo = $ this ->config ->getDebugTo ();
66
+
67
+ if ($ debugTo !== null ) {
68
+ $ to = $ debugTo ;
69
+ }
70
+
67
71
$ params = [
68
72
'body ' => trim ($ message ->content ),
69
73
];
@@ -76,7 +80,7 @@ protected function sendSmsMessage(TwilioSmsMessage $message, $to)
76
80
$ params ['from ' ] = $ from ;
77
81
}
78
82
79
- if (! $ from && ! $ messagingServiceSid ) {
83
+ if (empty ( $ from) && empty ( $ messagingServiceSid) ) {
80
84
throw CouldNotSendNotification::missingFrom ();
81
85
}
82
86
@@ -103,11 +107,13 @@ protected function sendSmsMessage(TwilioSmsMessage $message, $to)
103
107
* Make a call using the Twilio Service.
104
108
*
105
109
* @param TwilioCallMessage $message
106
- * @param string $to
107
- * @return \Twilio\Rest\Api\V2010\Account\CallInstance
108
- * @throws \Twilio\Exceptions\TwilioException
110
+ * @param string|null $to
111
+ *
112
+ * @return CallInstance
113
+ * @throws TwilioException
114
+ * @throws CouldNotSendNotification
109
115
*/
110
- protected function makeCall (TwilioCallMessage $ message , $ to )
116
+ protected function makeCall (TwilioCallMessage $ message , ? string $ to ): CallInstance
111
117
{
112
118
$ params = [
113
119
'url ' => trim ($ message ->content ),
@@ -137,9 +143,9 @@ protected function makeCall(TwilioCallMessage $message, $to)
137
143
* Get the from address from message, or config.
138
144
*
139
145
* @param TwilioMessage $message
140
- * @return string
146
+ * @return string|null
141
147
*/
142
- protected function getFrom (TwilioMessage $ message )
148
+ protected function getFrom (TwilioMessage $ message ): ? string
143
149
{
144
150
return $ message ->getFrom () ?: $ this ->config ->getFrom ();
145
151
}
@@ -148,9 +154,9 @@ protected function getFrom(TwilioMessage $message)
148
154
* Get the messaging service SID from message, or config.
149
155
*
150
156
* @param TwilioSmsMessage $message
151
- * @return string
157
+ * @return string|null
152
158
*/
153
- protected function getMessagingServiceSid (TwilioSmsMessage $ message )
159
+ protected function getMessagingServiceSid (TwilioSmsMessage $ message ): ? string
154
160
{
155
161
return $ message ->getMessagingServiceSid () ?: $ this ->config ->getServiceSid ();
156
162
}
@@ -160,11 +166,9 @@ protected function getMessagingServiceSid(TwilioSmsMessage $message)
160
166
*
161
167
* @return string|null
162
168
*/
163
- protected function getAlphanumericSender ()
169
+ protected function getAlphanumericSender (): ? string
164
170
{
165
- if ($ sender = $ this ->config ->getAlphanumericSender ()) {
166
- return $ sender ;
167
- }
171
+ return $ this ->config ->getAlphanumericSender ();
168
172
}
169
173
170
174
/**
@@ -173,7 +177,7 @@ protected function getAlphanumericSender()
173
177
* @param array $optionalParams
174
178
* @return Twilio
175
179
*/
176
- protected function fillOptionalParams (&$ params , $ message , $ optionalParams )
180
+ protected function fillOptionalParams (&$ params , $ message , $ optionalParams ): self
177
181
{
178
182
foreach ($ optionalParams as $ optionalParam ) {
179
183
if ($ message ->$ optionalParam ) {
0 commit comments