Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.x] Implement shortcode support #24

Merged
merged 3 commits into from
Sep 30, 2019
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
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;
}