Skip to content

Commit

Permalink
Merge pull request #24 from dwightwatson/shortcode
Browse files Browse the repository at this point in the history
[2.x] Implement shortcode support
  • Loading branch information
taylorotwell authored Sep 30, 2019
2 parents b964ec3 + c8557e0 commit b6f945a
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 18 deletions.
45 changes: 45 additions & 0 deletions src/Channels/NexmoShortcodeChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Illuminate\Notifications\Channels;

use Illuminate\Notifications\Notification;
use Nexmo\Message\Client as NexmoClient;

class NexmoShortcodeChannel
{
/**
* The Nexmo message client instance.
*
* @var \Nexmo\Message\Client
*/
protected $nexmo;

/**
* Create a new channel instance.
*
* @param \Nexmo\Message\Client $nexmo
* @return void
*/
public function __construct(NexmoClient $nexmo)
{
$this->client = $nexmo;
}

/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return void
*/
public function send($notifiable, Notification $notification)
{
if (! $to = $notifiable->routeNotificationFor('shortcode', $notification)) {
return;
}

$shortcode = array_merge(['to' => $to], $notification->toShortcode($notifiable));

$this->nexmo->sendShortcode($shortcode);
}
}
9 changes: 9 additions & 0 deletions src/NexmoChannelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\ServiceProvider;
use Nexmo\Client as NexmoClient;
use Nexmo\Message\Client as NexmoMessageClient;

class NexmoChannelServiceProvider extends ServiceProvider
{
Expand All @@ -22,6 +23,14 @@ public function register()
$this->app['config']['services.nexmo.sms_from']
);
});

$service->extend('shortcode', function ($app) {
$client = tap($app->make(NexmoMessageClient::class), function ($client) use ($app) {
$client->setClient($app->make(NexmoClient::class));
});

return new Channels\NexmoShortcodeChannel($client);
});
});
}
}
53 changes: 53 additions & 0 deletions tests/Channels/NexmoShortcodeChannelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Illuminate\Tests\Notifications\Channels;

use Illuminate\Notifications\Channels\NexmoShortcodeChannel;
use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\Notification;
use Illuminate\Tests\Notifications\TestCase;
use Mockery as m;
use Nexmo\Message\Client;

class NexmoShortcodeChannelTest extends TestCase
{
public function testShortcodeIsSentViaNexmo()
{
$notification = new NotificationNexmoShortcodeChannelTestNotification;
$notifiable = new NotificationNexmoShortcodeChannelTestNotifiable;

$channel = new NexmoShortcodeChannel(
$nexmo = m::mock(Client::class)
);

$nexmo->shouldReceive('sendShortcode')->with([
'type' => 'alert',
'to' => '5555555555',
'custom' => [
'code' => 'abc123',
],
]);

$channel->send($notifiable, $notification);
}
}

class NotificationNexmoShortcodeChannelTestNotifiable
{
use Notifiable;

public $phone_number = '5555555555';
}

class NotificationNexmoShortcodeChannelTestNotification extends Notification
{
public function toShortcode($notifiable)
{
return [
'type' => 'alert',
'custom' => [
'code' => 'abc123',
],
];
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
<?php

namespace Illuminate\Tests\Notifications;
namespace Illuminate\Tests\Notifications\Channels;

use Illuminate\Notifications\Channels\NexmoSmsChannel;
use Illuminate\Notifications\Messages\NexmoMessage;
use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\Notification;
use Illuminate\Tests\Notifications\TestCase;
use Mockery as m;
use Nexmo\Client;
use PHPUnit\Framework\TestCase;

class NotificationNexmoChannelTest extends TestCase
class NexmoSmsChannelTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}

public function testSmsIsSentViaNexmo()
{
$notification = new NotificationNexmoChannelTestNotification;
$notifiable = new NotificationNexmoChannelTestNotifiable;
$notification = new NotificationNexmoSmsChannelTestNotification;
$notifiable = new NotificationNexmoSmsChannelTestNotifiable;

$channel = new NexmoSmsChannel(
$nexmo = m::mock(Client::class), '4444444444'
Expand All @@ -39,8 +34,8 @@ public function testSmsIsSentViaNexmo()

public function testSmsIsSentViaNexmoWithCustomFrom()
{
$notification = new NotificationNexmoChannelTestCustomFromNotification;
$notifiable = new NotificationNexmoChannelTestNotifiable;
$notification = new NotificationNexmoSmsChannelTestCustomFromNotification;
$notifiable = new NotificationNexmoSmsChannelTestNotifiable;

$channel = new NexmoSmsChannel(
$nexmo = m::mock(Client::class), '4444444444'
Expand All @@ -59,8 +54,8 @@ public function testSmsIsSentViaNexmoWithCustomFrom()

public function testSmsIsSentViaNexmoWithCustomFromAndClientRef()
{
$notification = new NotificationNexmoChannelTestCustomFromAndClientRefNotification;
$notifiable = new NotificationNexmoChannelTestNotifiable;
$notification = new NotificationNexmoSmsChannelTestCustomFromAndClientRefNotification;
$notifiable = new NotificationNexmoSmsChannelTestNotifiable;

$channel = new NexmoSmsChannel(
$nexmo = m::mock(Client::class), '4444444444'
Expand All @@ -78,30 +73,30 @@ public function testSmsIsSentViaNexmoWithCustomFromAndClientRef()
}
}

class NotificationNexmoChannelTestNotifiable
class NotificationNexmoSmsChannelTestNotifiable
{
use Notifiable;

public $phone_number = '5555555555';
}

class NotificationNexmoChannelTestNotification extends Notification
class NotificationNexmoSmsChannelTestNotification extends Notification
{
public function toNexmo($notifiable)
{
return new NexmoMessage('this is my message');
}
}

class NotificationNexmoChannelTestCustomFromNotification extends Notification
class NotificationNexmoSmsChannelTestCustomFromNotification extends Notification
{
public function toNexmo($notifiable)
{
return (new NexmoMessage('this is my message'))->from('5554443333')->unicode();
}
}

class NotificationNexmoChannelTestCustomFromAndClientRefNotification extends Notification
class NotificationNexmoSmsChannelTestCustomFromAndClientRefNotification extends Notification
{
public function toNexmo($notifiable)
{
Expand Down
11 changes: 11 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Illuminate\Tests\Notifications;

use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\TestCase as BaseTestCase;

class TestCase extends BaseTestCase
{
use MockeryPHPUnitIntegration;
}

0 comments on commit b6f945a

Please sign in to comment.