22
33namespace Aloha \Twilio ;
44
5+ use Twilio \Exceptions \ConfigurationException ;
6+ use Twilio \Exceptions \TwilioException ;
7+ use Twilio \Rest \Api \V2010 \Account \CallInstance ;
8+ use Twilio \Rest \Api \V2010 \Account \MessageInstance ;
59use Twilio \Rest \Client ;
610use Twilio \TwiML \TwiML ;
711use Twilio \TwiML \VoiceResponse ;
@@ -29,7 +33,7 @@ class Twilio implements TwilioInterface
2933 protected $ sslVerify ;
3034
3135 /**
32- * @var \Twilio\Rest\ Client
36+ * @var Client
3337 */
3438 protected $ twilio ;
3539
@@ -39,7 +43,7 @@ class Twilio implements TwilioInterface
3943 * @param string $sid
4044 * @param bool $sslVerify
4145 */
42- public function __construct ($ sid , $ token , $ from , $ sslVerify = true )
46+ public function __construct (string $ sid , string $ token , string $ from , bool $ sslVerify = true )
4347 {
4448 $ this ->sid = $ sid ;
4549 $ this ->token = $ token ;
@@ -50,14 +54,17 @@ public function __construct($sid, $token, $from, $sslVerify = true)
5054 /**
5155 * @param string $to
5256 * @param string $message
53- * @param null| array $mediaUrls
57+ * @param array $mediaUrls
5458 * @param array $params
5559 *
5660 * @see https://www.twilio.com/docs/api/messaging/send-messages Documentation
5761 *
58- * @return \Twilio\Rest\Api\V2010\Account\MessageInstance
62+ * @throws ConfigurationException
63+ * @throws TwilioException
64+ *
65+ * @return MessageInstance
5966 */
60- public function message ($ to , $ message , $ mediaUrls = null , array $ params = [])
67+ public function message (string $ to , string $ message , array $ mediaUrls = [] , array $ params = []): MessageInstance
6168 {
6269 $ params ['body ' ] = $ message ;
6370
@@ -77,16 +84,20 @@ public function message($to, $message, $mediaUrls = null, array $params = [])
7784 * @param callable|string|TwiML $message
7885 * @param array $params
7986 *
87+ * @throws TwilioException
88+ *
8089 * @see https://www.twilio.com/docs/api/voice/making-calls Documentation
8190 *
82- * @return \Twilio\Rest\Api\V2010\Account\ CallInstance
91+ * @return CallInstance
8392 */
84- public function call ($ to , $ message , array $ params = [])
93+ public function call (string $ to , $ message , array $ params = []): CallInstance
8594 {
95+ if (is_callable ($ message )) {
96+ $ message = $ this ->twiml ($ message );
97+ }
98+
8699 if ($ message instanceof TwiML) {
87- $ params ['twiml ' ] = $ message ->__toString ();
88- } elseif (is_callable ($ message )) {
89- $ params ['twiml ' ] = $ this ->twiml ($ message );
100+ $ params ['twiml ' ] = (string ) $ message ;
90101 } else {
91102 $ params ['url ' ] = $ message ;
92103 }
@@ -99,9 +110,11 @@ public function call($to, $message, array $params = [])
99110 }
100111
101112 /**
102- * @return \Twilio\Rest\Client
113+ * @throws ConfigurationException
114+ *
115+ * @return Client
103116 */
104- public function getTwilio ()
117+ public function getTwilio (): Client
105118 {
106119 if ($ this ->twilio ) {
107120 return $ this ->twilio ;
@@ -113,14 +126,14 @@ public function getTwilio()
113126 /**
114127 * @param callable $callback
115128 *
116- * @return string
129+ * @return TwiML
117130 */
118- private function twiml (callable $ callback )
131+ private function twiml (callable $ callback ): TwiML
119132 {
120133 $ message = new VoiceResponse ();
121134
122135 call_user_func ($ callback , $ message );
123136
124- return ( string ) $ message ;
137+ return $ message ;
125138 }
126139}
0 commit comments