Skip to content

Commit 7178163

Browse files
committed
Revert some changes and make config compatible with auth token or username/passward
1 parent a45d5be commit 7178163

File tree

5 files changed

+62
-4
lines changed

5 files changed

+62
-4
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ Add your Twilio Account SID, Auth Token, and From Number (optional) to your `con
4949
// config/services.php
5050
...
5151
'twilio' => [
52-
'username' => env('TWILIO_USERNAME'),
53-
'password' => env('TWILIO_PASSWORD'),
54-
'account_sid' => env('TWILIO_ACCOUNT_SID'), //optional
52+
'username' => env('TWILIO_USERNAME'), // optional when using auth token
53+
'password' => env('TWILIO_PASSWORD'), // optional when using auth token
54+
'auth_token' => env('TWILIO_AUTH_TOKEN'), // optional when using username and password
55+
'account_sid' => env('TWILIO_ACCOUNT_SID'),
5556
'from' => env('TWILIO_FROM'), // optional
5657
],
5758
...

src/Twilio.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ 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
6465
*/
6566
protected function sendSmsMessage(TwilioSmsMessage $message, $to)
6667
{
@@ -82,6 +83,7 @@ protected function sendSmsMessage(TwilioSmsMessage $message, $to)
8283
* @param TwilioCallMessage $message
8384
* @param string $to
8485
* @return \Twilio\Rest\Api\V2010\Account\CallInstance
86+
* @throws CouldNotSendNotification
8587
*/
8688
protected function makeCall(TwilioCallMessage $message, $to)
8789
{
@@ -118,5 +120,7 @@ protected function getAlphanumericSender()
118120
if ($sender = $this->config->getAlphanumericSender()) {
119121
return $sender;
120122
}
123+
124+
return null;
121125
}
122126
}

src/TwilioConfig.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ public function __construct(array $config)
1919
$this->config = $config;
2020
}
2121

22+
/**
23+
* Get the auth token.
24+
*
25+
* @return string
26+
*/
27+
public function getAuthToken()
28+
{
29+
return $this->config['auth_token'];
30+
}
31+
2232
/**
2333
* Get the username.
2434
*

src/TwilioProvider.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,15 @@ public function boot()
2323

2424
$this->app->bind(TwilioService::class, function () {
2525
$config = $this->app['config']['services.twilio'];
26-
return new TwilioService($config['username'], $config['password'], $config['account_sid']);
26+
$account_sid = array_get($config, 'account_sid');
27+
$username = array_get($config, 'username');
28+
if (!empty($username)) {
29+
$password = array_get($config, 'password');
30+
return new TwilioService($username, $password, $account_sid);
31+
} else {
32+
$auth_token = array_get($config, 'auth_token');
33+
return new TwilioService($account_sid, $auth_token);
34+
}
2735
});
2836
}
2937

tests/TwilioProviderTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,41 @@ public function it_gives_an_instantiated_twilio_object_when_the_channel_asks_for
6161

6262
$this->provider->boot();
6363
}
64+
65+
/** @test */
66+
public function it_gives_an_instantiated_twilio_object_when_the_channel_asks_for_it_with_new_config()
67+
{
68+
$configArray = [
69+
'username' => 'username',
70+
'password' => 'password',
71+
'account_sid' => 'sid',
72+
'from' => 'from',
73+
];
74+
75+
$this->app->shouldReceive('offsetGet')
76+
->with('config')
77+
->andReturn([
78+
'services.twilio' => $configArray,
79+
]);
80+
81+
$twilio = Mockery::mock(TwilioService::class);
82+
$config = Mockery::mock(TwilioConfig::class, $configArray);
83+
84+
$this->app->shouldReceive('make')->with(TwilioConfig::class)->andReturn($config);
85+
$this->app->shouldReceive('make')->with(TwilioService::class)->andReturn($twilio);
86+
87+
$this->app->shouldReceive('when')->with(TwilioChannel::class)->once()->andReturn($this->app);
88+
$this->app->shouldReceive('needs')->with(Twilio::class)->once()->andReturn($this->app);
89+
$this->app->shouldReceive('give')->with(Mockery::on(function ($twilio) {
90+
return $twilio() instanceof Twilio;
91+
}))->once();
92+
93+
$this->app->shouldReceive('bind')->with(TwilioService::class, Mockery::on(function ($twilio) {
94+
return $twilio() instanceof TwilioService;
95+
}))->once()->andReturn($this->app);
96+
97+
$this->provider->boot();
98+
}
6499
}
65100

66101
interface App extends Application, ArrayAccess

0 commit comments

Comments
 (0)