Skip to content

Commit 0cd8604

Browse files
authored
Merge pull request #1 from onlime/121-enabled-config
Add enabled config
2 parents 761d57b + 5daeb27 commit 0cd8604

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

config/twilio-notification-channel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
return [
4+
'enabled' => env('TWILIO_ENABLED', true),
45
'username' => env('TWILIO_USERNAME'), // optional when using auth token
56
'password' => env('TWILIO_PASSWORD'), // optional when using auth token
67
'auth_token' => env('TWILIO_AUTH_TOKEN'), // optional when using username and password

src/TwilioChannel.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public function __construct(Twilio $twilio, Dispatcher $events)
4343
*/
4444
public function send($notifiable, Notification $notification)
4545
{
46+
if (! $this->isEnabled()) {
47+
return;
48+
}
49+
4650
try {
4751
$to = $this->getTo($notifiable, $notification);
4852
$message = $notification->toTwilio($notifiable);
@@ -75,6 +79,20 @@ public function send($notifiable, Notification $notification)
7579
}
7680
}
7781

82+
/**
83+
* Check if twilio is enabled.
84+
*
85+
* @return bool
86+
*/
87+
protected function isEnabled()
88+
{
89+
if (is_null($this->twilio->config)) {
90+
return true;
91+
}
92+
93+
return $this->twilio->config->enabled();
94+
}
95+
7896
/**
7997
* Get the address to send a notification to.
8098
*

src/TwilioConfig.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public function __construct(array $config)
1515
$this->config = $config;
1616
}
1717

18+
public function enabled()
19+
{
20+
return $this->config['enabled'] ?? true;
21+
}
22+
1823
public function usingUsernamePasswordAuth(): bool
1924
{
2025
return $this->getUsername() !== null && $this->getPassword() !== null && $this->getAccountSid() !== null;

tests/Integration/BaseIntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace NotificationChannels\Twilio\Tests\Integration;
66

77
use NotificationChannels\Twilio\TwilioProvider;
8-
use Orchestra\Testbench\TestCase;
8+
use PHPUnit\Framework\TestCase;
99

1010
abstract class BaseIntegrationTest extends TestCase
1111
{

tests/Unit/TwilioChannelTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ public function setUp(): void
3636
$this->channel = new TwilioChannel($this->twilio, $this->dispatcher);
3737
}
3838

39+
/** @test */
40+
public function it_will_not_send_a_message_if_not_enabled()
41+
{
42+
$notifiable = new Notifiable();
43+
$notification = Mockery::mock(Notification::class);
44+
45+
$this->twilio->config = new TwilioConfig([
46+
'enabled' => false,
47+
]);
48+
49+
$this->dispatcher->shouldNotReceive('dispatch');
50+
51+
$result = $this->channel->send($notifiable, $notification);
52+
53+
$this->assertNull($result);
54+
}
55+
3956
/** @test */
4057
public function it_will_not_send_a_message_without_known_receiver()
4158
{

0 commit comments

Comments
 (0)