@@ -36,7 +36,7 @@ public function __construct(TwilioService $twilioService, TwilioConfig $config)
36
36
* @param string $to
37
37
* @param bool $useAlphanumericSender
38
38
* @return mixed
39
- * @throws CouldNotSendNotification
39
+ * @throws \Twilio\Exceptions\TwilioException
40
40
*/
41
41
public function sendMessage (TwilioMessage $ message , $ to , $ useAlphanumericSender = false )
42
42
{
@@ -61,17 +61,23 @@ public function sendMessage(TwilioMessage $message, $to, $useAlphanumericSender
61
61
* @param TwilioSmsMessage $message
62
62
* @param string $to
63
63
* @return \Twilio\Rest\Api\V2010\Account\MessageInstance
64
- * @throws CouldNotSendNotification
65
64
*/
66
65
protected function sendSmsMessage (TwilioSmsMessage $ message , $ to )
67
66
{
68
67
$ params = [
69
- 'from ' => $ this ->getFrom ($ message ),
70
68
'body ' => trim ($ message ->content ),
71
69
];
72
70
73
- if ($ serviceSid = $ this ->config ->getServiceSid ()) {
74
- $ params ['messagingServiceSid ' ] = $ serviceSid ;
71
+ if ($ messagingServiceSid = $ this ->getMessagingServiceSid ($ message )) {
72
+ $ params ['messagingServiceSid ' ] = $ messagingServiceSid ;
73
+ }
74
+
75
+ if ($ from = $ this ->getFrom ($ message )) {
76
+ $ params ['from ' ] = $ from ;
77
+ }
78
+
79
+ if (!$ from && !$ messagingServiceSid ) {
80
+ throw CouldNotSendNotification::missingFrom ();
75
81
}
76
82
77
83
$ this ->fillOptionalParams ($ params , $ message , [
@@ -98,7 +104,7 @@ protected function sendSmsMessage(TwilioSmsMessage $message, $to)
98
104
* @param TwilioCallMessage $message
99
105
* @param string $to
100
106
* @return \Twilio\Rest\Api\V2010\Account\CallInstance
101
- * @throws CouldNotSendNotification
107
+ * @throws \Twilio\Exceptions\TwilioException
102
108
*/
103
109
protected function makeCall (TwilioCallMessage $ message , $ to )
104
110
{
@@ -115,9 +121,13 @@ protected function makeCall(TwilioCallMessage $message, $to)
115
121
'fallbackMethod ' ,
116
122
]);
117
123
124
+ if (! $ from = $ this ->getFrom ($ message )) {
125
+ throw CouldNotSendNotification::missingFrom ();
126
+ }
127
+
118
128
return $ this ->twilioService ->calls ->create (
119
129
$ to ,
120
- $ this -> getFrom ( $ message ) ,
130
+ $ from ,
121
131
$ params
122
132
);
123
133
}
@@ -127,15 +137,21 @@ protected function makeCall(TwilioCallMessage $message, $to)
127
137
*
128
138
* @param TwilioMessage $message
129
139
* @return string
130
- * @throws CouldNotSendNotification
131
140
*/
132
141
protected function getFrom (TwilioMessage $ message )
133
142
{
134
- if (! $ from = $ message ->getFrom () ?: $ this ->config ->getFrom ()) {
135
- throw CouldNotSendNotification::missingFrom ();
136
- }
143
+ return $ message ->getFrom () ?: $ this ->config ->getFrom ();
144
+ }
137
145
138
- return $ from ;
146
+ /**
147
+ * Get the messaging service SID from message, or config.
148
+ *
149
+ * @param TwilioSmsMessage $message
150
+ * @return string
151
+ */
152
+ protected function getMessagingServiceSid (TwilioSmsMessage $ message )
153
+ {
154
+ return $ message ->getMessagingServiceSid () ?: $ this ->config ->getServiceSid ();
139
155
}
140
156
141
157
/**
@@ -148,13 +164,15 @@ protected function getAlphanumericSender()
148
164
if ($ sender = $ this ->config ->getAlphanumericSender ()) {
149
165
return $ sender ;
150
166
}
167
+
168
+ return null ;
151
169
}
152
170
153
171
/**
154
172
* @param array $params
155
173
* @param TwilioMessage $message
156
174
* @param array $optionalParams
157
- * @return mixed
175
+ * @return Twilio
158
176
*/
159
177
protected function fillOptionalParams (&$ params , $ message , $ optionalParams )
160
178
{
@@ -163,5 +181,7 @@ protected function fillOptionalParams(&$params, $message, $optionalParams)
163
181
$ params [$ optionalParam ] = $ message ->$ optionalParam ;
164
182
}
165
183
}
184
+
185
+ return $ this ;
166
186
}
167
187
}
0 commit comments