@@ -36,7 +36,7 @@ public function __construct(TwilioService $twilioService, TwilioConfig $config)
3636 * @param string $to
3737 * @param bool $useAlphanumericSender
3838 * @return mixed
39- * @throws CouldNotSendNotification
39+ * @throws \Twilio\Exceptions\TwilioException
4040 */
4141 public function sendMessage (TwilioMessage $ message , $ to , $ useAlphanumericSender = false )
4242 {
@@ -61,17 +61,23 @@ public function sendMessage(TwilioMessage $message, $to, $useAlphanumericSender
6161 * @param TwilioSmsMessage $message
6262 * @param string $to
6363 * @return \Twilio\Rest\Api\V2010\Account\MessageInstance
64- * @throws CouldNotSendNotification
6564 */
6665 protected function sendSmsMessage (TwilioSmsMessage $ message , $ to )
6766 {
6867 $ params = [
69- 'from ' => $ this ->getFrom ($ message ),
7068 'body ' => trim ($ message ->content ),
7169 ];
7270
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 ();
7581 }
7682
7783 $ this ->fillOptionalParams ($ params , $ message , [
@@ -98,7 +104,7 @@ protected function sendSmsMessage(TwilioSmsMessage $message, $to)
98104 * @param TwilioCallMessage $message
99105 * @param string $to
100106 * @return \Twilio\Rest\Api\V2010\Account\CallInstance
101- * @throws CouldNotSendNotification
107+ * @throws \Twilio\Exceptions\TwilioException
102108 */
103109 protected function makeCall (TwilioCallMessage $ message , $ to )
104110 {
@@ -115,9 +121,13 @@ protected function makeCall(TwilioCallMessage $message, $to)
115121 'fallbackMethod ' ,
116122 ]);
117123
124+ if (! $ from = $ this ->getFrom ($ message )) {
125+ throw CouldNotSendNotification::missingFrom ();
126+ }
127+
118128 return $ this ->twilioService ->calls ->create (
119129 $ to ,
120- $ this -> getFrom ( $ message ) ,
130+ $ from ,
121131 $ params
122132 );
123133 }
@@ -127,15 +137,21 @@ protected function makeCall(TwilioCallMessage $message, $to)
127137 *
128138 * @param TwilioMessage $message
129139 * @return string
130- * @throws CouldNotSendNotification
131140 */
132141 protected function getFrom (TwilioMessage $ message )
133142 {
134- if (! $ from = $ message ->getFrom () ?: $ this ->config ->getFrom ()) {
135- throw CouldNotSendNotification::missingFrom ();
136- }
143+ return $ message ->getFrom () ?: $ this ->config ->getFrom ();
144+ }
137145
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 ();
139155 }
140156
141157 /**
@@ -148,13 +164,15 @@ protected function getAlphanumericSender()
148164 if ($ sender = $ this ->config ->getAlphanumericSender ()) {
149165 return $ sender ;
150166 }
167+
168+ return null ;
151169 }
152170
153171 /**
154172 * @param array $params
155173 * @param TwilioMessage $message
156174 * @param array $optionalParams
157- * @return mixed
175+ * @return Twilio
158176 */
159177 protected function fillOptionalParams (&$ params , $ message , $ optionalParams )
160178 {
@@ -163,5 +181,7 @@ protected function fillOptionalParams(&$params, $message, $optionalParams)
163181 $ params [$ optionalParam ] = $ message ->$ optionalParam ;
164182 }
165183 }
184+
185+ return $ this ;
166186 }
167187}
0 commit comments