Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Add enabled config #1

Merged
merged 4 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/twilio-notification-channel.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

return [
'enabled' => env('TWILIO_ENABLED', true),
'username' => env('TWILIO_USERNAME'), // optional when using auth token
'password' => env('TWILIO_PASSWORD'), // optional when using auth token
'auth_token' => env('TWILIO_AUTH_TOKEN'), // optional when using username and password
Expand Down
18 changes: 18 additions & 0 deletions src/TwilioChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public function __construct(Twilio $twilio, Dispatcher $events)
*/
public function send($notifiable, Notification $notification)
{
if (! $this->isEnabled()) {
return;
}

try {
$to = $this->getTo($notifiable, $notification);
$message = $notification->toTwilio($notifiable);
Expand Down Expand Up @@ -75,6 +79,20 @@ public function send($notifiable, Notification $notification)
}
}

/**
* Check if twilio is enabled.
*
* @return bool
*/
protected function isEnabled()
{
if (is_null($this->twilio->config)) {
return true;
}

return $this->twilio->config->enabled();
}

/**
* Get the address to send a notification to.
*
Expand Down
5 changes: 5 additions & 0 deletions src/TwilioConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public function __construct(array $config)
$this->config = $config;
}

public function enabled()
{
return $this->config['enabled'] ?? true;
}

public function usingUsernamePasswordAuth(): bool
{
return $this->getUsername() !== null && $this->getPassword() !== null && $this->getAccountSid() !== null;
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/BaseIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace NotificationChannels\Twilio\Tests\Integration;

use NotificationChannels\Twilio\TwilioProvider;
use Orchestra\Testbench\TestCase;
use PHPUnit\Framework\TestCase;

abstract class BaseIntegrationTest extends TestCase
{
Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/TwilioChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ public function setUp(): void
$this->channel = new TwilioChannel($this->twilio, $this->dispatcher);
}

/** @test */
public function it_will_not_send_a_message_if_not_enabled()
{
$notifiable = new Notifiable();
$notification = Mockery::mock(Notification::class);

$this->twilio->config = new TwilioConfig([
'enabled' => false,
]);

$this->dispatcher->shouldNotReceive('dispatch');

$result = $this->channel->send($notifiable, $notification);

$this->assertNull($result);
}

/** @test */
public function it_will_not_send_a_message_without_known_receiver()
{
Expand Down
Loading