Skip to content

Commit c0bb9fe

Browse files
Added type hints
1 parent f15d120 commit c0bb9fe

File tree

8 files changed

+101
-53
lines changed

8 files changed

+101
-53
lines changed

src/Commands/TwilioCallCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function handle()
6464
*
6565
* @return array
6666
*/
67-
protected function getArguments()
67+
protected function getArguments(): array
6868
{
6969
return [
7070
['phone', InputArgument::REQUIRED, 'The phone number that will receive a test message.'],
@@ -76,7 +76,7 @@ protected function getArguments()
7676
*
7777
* @return array
7878
*/
79-
protected function getOptions()
79+
protected function getOptions(): array
8080
{
8181
return [
8282
['url', null, InputOption::VALUE_OPTIONAL, 'Optional url that will be used to fetch xml for call.', null],

src/Commands/TwilioSmsCommand.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ class TwilioSmsCommand extends Command
3636
public function __construct(TwilioInterface $twilio)
3737
{
3838
parent::__construct();
39+
3940
$this->twilio = $twilio;
4041
}
4142

4243
/**
4344
* Execute the console command.
4445
*/
45-
public function fire()
46+
public function handle()
4647
{
4748
$this->line('Sending SMS via Twilio to: '.$this->argument('phone'));
4849

@@ -64,7 +65,7 @@ public function fire()
6465
*
6566
* @return array
6667
*/
67-
protected function getArguments()
68+
protected function getArguments(): array
6869
{
6970
return [
7071
['phone', InputArgument::REQUIRED, 'The phone number that will receive a test message.'],
@@ -76,7 +77,7 @@ protected function getArguments()
7677
*
7778
* @return array
7879
*/
79-
protected function getOptions()
80+
protected function getOptions(): array
8081
{
8182
return [
8283
['text', null, InputOption::VALUE_OPTIONAL, 'Optional message that will be sent.', null],

src/Dummy.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,41 @@
22

33
namespace Aloha\Twilio;
44

5+
use Twilio\Exceptions\ConfigurationException;
6+
use Twilio\Rest\Api;
7+
use Twilio\Rest\Api\V2010;
8+
use Twilio\Rest\Api\V2010\Account\CallInstance;
9+
use Twilio\Rest\Api\V2010\Account\MessageInstance;
10+
use Twilio\Rest\Client;
11+
512
class Dummy implements TwilioInterface
613
{
714
/**
815
* @param string $to
916
* @param string $message
17+
* @param array $mediaUrls
18+
* @param array $params
19+
*
20+
* @throws ConfigurationException
1021
*
11-
* @return \Twilio\Rest\Api\V2010\Account\MessageInstance|void
22+
* @return MessageInstance
1223
*/
13-
public function message($to, $message)
24+
public function message(string $to, string $message, array $mediaUrls = [], array $params = []): MessageInstance
1425
{
26+
return new MessageInstance(new V2010(new Api(new Client('nonsense', 'nonsense'))), [], '');
1527
}
1628

1729
/**
1830
* @param string $to
1931
* @param callable|string $message
32+
* @param array $params
33+
*
34+
* @throws ConfigurationException
2035
*
21-
* @return \Twilio\Rest\Api\V2010\Account\CallInstance|void
36+
* @return CallInstance
2237
*/
23-
public function call($to, $message)
38+
public function call(string $to, $message, array $params = []): CallInstance
2439
{
40+
return new CallInstance(new V2010(new Api(new Client('nonsense', 'nonsense'))), [], '');
2541
}
2642
}

src/LoggingDecorator.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@
33
namespace Aloha\Twilio;
44

55
use Psr\Log\LoggerInterface;
6+
use Twilio\Rest\Api\V2010\Account\CallInstance;
7+
use Twilio\Rest\Api\V2010\Account\MessageInstance;
68

79
class LoggingDecorator implements TwilioInterface
810
{
911
/**
10-
* @var \Psr\Log\LoggerInterface
12+
* @var LoggerInterface
1113
*/
1214
private $logger;
1315

1416
/**
15-
* @var \Aloha\Twilio\TwilioInterface
17+
* @var TwilioInterface
1618
*/
1719
private $wrapped;
1820

1921
/**
20-
* @param \Psr\Log\LoggerInterface $logger
21-
* @param \Aloha\Twilio\TwilioInterface $wrapped
22+
* @param LoggerInterface $logger
23+
* @param TwilioInterface $wrapped
2224
*/
2325
public function __construct(LoggerInterface $logger, TwilioInterface $wrapped)
2426
{
@@ -29,26 +31,29 @@ public function __construct(LoggerInterface $logger, TwilioInterface $wrapped)
2931
/**
3032
* @param string $to
3133
* @param string $message
34+
* @param array $mediaUrls
35+
* @param array $params
3236
*
33-
* @return \Twilio\Rest\Api\V2010\Account\MessageInstance
37+
* @return MessageInstance
3438
*/
35-
public function message($to, $message)
39+
public function message($to, $message, array $mediaUrls = [], array $params = []): MessageInstance
3640
{
3741
$this->logger->info(sprintf('Sending a message ["%s"] to %s', $message, $to));
3842

39-
return call_user_func_array([$this->wrapped, 'message'], func_get_args());
43+
return $this->wrapped->message($to, $message, $mediaUrls, $params);
4044
}
4145

4246
/**
4347
* @param string $to
4448
* @param callable|string $message
49+
* @param array $params
4550
*
46-
* @return \Twilio\Rest\Api\V2010\Account\CallInstance
51+
* @return CallInstance
4752
*/
48-
public function call($to, $message)
53+
public function call(string $to, $message, array $params = []): CallInstance
4954
{
5055
$this->logger->info(sprintf('Calling %s', $to));
5156

52-
return call_user_func_array([$this->wrapped, 'call'], func_get_args());
57+
return $this->wrapped->call($to, $message, $params);
5358
}
5459
}

src/Manager.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace Aloha\Twilio;
44

55
use InvalidArgumentException;
6+
use Twilio\Rest\Api\V2010\Account\CallInstance;
7+
use Twilio\Rest\Api\V2010\Account\MessageInstance;
8+
use Twilio\TwiML\TwiML;
69

710
class Manager implements TwilioInterface
811
{
@@ -20,7 +23,7 @@ class Manager implements TwilioInterface
2023
* @param string $default
2124
* @param array $settings
2225
*/
23-
public function __construct($default, array $settings)
26+
public function __construct(string $default, array $settings)
2427
{
2528
$this->default = $default;
2629
$this->settings = $settings;
@@ -32,17 +35,17 @@ public function __construct($default, array $settings)
3235
*
3336
* @return mixed
3437
*/
35-
public function __call($method, $arguments)
38+
public function __call(string $method, array $arguments)
3639
{
3740
return call_user_func_array([$this->defaultConnection(), $method], $arguments);
3841
}
3942

4043
/**
4144
* @param string $connection
4245
*
43-
* @return \Aloha\Twilio\TwilioInterface
46+
* @return TwilioInterface
4447
*/
45-
public function from($connection)
48+
public function from(string $connection): TwilioInterface
4649
{
4750
if (!isset($this->settings[$connection])) {
4851
throw new InvalidArgumentException("Connection \"{$connection}\" is not configured.");
@@ -56,29 +59,32 @@ public function from($connection)
5659
/**
5760
* @param string $to
5861
* @param string $message
62+
* @param array $mediaUrls
63+
* @param array $params
5964
*
60-
* @return \Twilio\Rest\Api\V2010\Account\MessageInstance
65+
* @return MessageInstance
6166
*/
62-
public function message($to, $message)
67+
public function message(string $to, string $message, array $mediaUrls = [], array $params = []): MessageInstance
6368
{
64-
return call_user_func_array([$this->defaultConnection(), 'message'], func_get_args());
69+
return $this->defaultConnection()->message($to, $message, $mediaUrls, $params);
6570
}
6671

6772
/**
6873
* @param string $to
69-
* @param callable|string $message
74+
* @param callable|string|TwiML $message
75+
* @param array $params
7076
*
71-
* @return \Twilio\Rest\Api\V2010\Account\CallInstance
77+
* @return CallInstance
7278
*/
73-
public function call($to, $message)
79+
public function call(string $to, $message, array $params = []): CallInstance
7480
{
75-
return call_user_func_array([$this->defaultConnection(), 'call'], func_get_args());
81+
return $this->defaultConnection()->call($to, $message, $params);
7682
}
7783

7884
/**
79-
* @return \Aloha\Twilio\TwilioInterface
85+
* @return TwilioInterface
8086
*/
81-
public function defaultConnection()
87+
public function defaultConnection(): TwilioInterface
8288
{
8389
return $this->from($this->default);
8490
}

src/Support/Laravel/Facade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Facade extends BaseFacade
1111
*
1212
* @return string
1313
*/
14-
protected static function getFacadeAccessor()
14+
protected static function getFacadeAccessor(): string
1515
{
1616
return 'twilio';
1717
}

src/Twilio.php

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace 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;
59
use Twilio\Rest\Client;
610
use Twilio\TwiML\TwiML;
711
use 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
}

src/TwilioInterface.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,28 @@
22

33
namespace Aloha\Twilio;
44

5+
use Twilio\Rest\Api\V2010\Account\CallInstance;
6+
use Twilio\Rest\Api\V2010\Account\MessageInstance;
7+
use Twilio\TwiML\TwiML;
8+
59
interface TwilioInterface
610
{
711
/**
812
* @param string $to
913
* @param string $message
14+
* @param array $mediaUrls
15+
* @param array $params
1016
*
11-
* @return \Twilio\Rest\Api\V2010\Account\MessageInstance
17+
* @return MessageInstance
1218
*/
13-
public function message($to, $message);
19+
public function message(string $to, string $message, array $mediaUrls = [], array $params = []): MessageInstance;
1420

1521
/**
1622
* @param string $to
17-
* @param callable|string $message
23+
* @param callable|string|TwiML $message
24+
* @param array $params
1825
*
19-
* @return \Twilio\Rest\Api\V2010\Account\CallInstance
26+
* @return CallInstance
2027
*/
21-
public function call($to, $message);
28+
public function call(string $to, $message, array $params): CallInstance;
2229
}

0 commit comments

Comments
 (0)