Skip to content

Commit

Permalink
Add US shortcode support
Browse files Browse the repository at this point in the history
  • Loading branch information
mheap committed Feb 7, 2019
1 parent 3dc03ca commit 2f79f67
Show file tree
Hide file tree
Showing 9 changed files with 323 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Message/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,37 @@ public function send($message)
return $message;
}

public function sendShortcode($message) {
if(!($message instanceof Shortcode)){
$message = Shortcode::createMessageFromArray($message);
}

$params = $message->getRequestData();

$request = new Request(
$this->getClient()->getRestUrl() . '/sc/us/'.$message->getType().'/json'
,'POST',
'php://temp',
['content-type' => 'application/json']
);

$request->getBody()->write(json_encode($params));
$response = $this->client->send($request);

$body = json_decode($response->getBody(), true);

foreach ($body['messages'] as $m) {
if ($m['status'] != '0') {
$e = new Exception\Request($m['error-text'], $m['status']);
$e->setEntity($message);
throw $e;
}
}

return $body;

}

/**
* @param $query
* @return MessageInterface[]
Expand Down
78 changes: 78 additions & 0 deletions src/Message/Shortcode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* Nexmo Client Library for PHP
*
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
*/

namespace Nexmo\Message;

use Nexmo\Client\Exception\Exception;
use Nexmo\Message\Shortcode\Alert;
use Nexmo\Message\Shortcode\Marketing;
use Nexmo\Message\Shortcode\TwoFactor;

abstract class Shortcode
{

protected $to;
protected $custom;
protected $options;

public function __construct($to, array $custom = [], array $options = []) {
$this->to = $to;
$this->custom = $custom;
$this->options = $options;
}

public function setCustom($custom) {
$this->custom = $custom;
}

public function setOptions($options) {
$this->options = $options;
}

public function getType() {
return $this->type;
}

public function getRequestData() {
// Options, then custom, then to. This is the priority
// we want so that people can't overwrite to with a custom param
return $this->options + $this->custom + [
'to' => $this->to
];
}

public static function createMessageFromArray($data){
if (!isset($data['type'])) {
throw new Exception('No type provided when creating a shortcode message');
}

if (!isset($data['to'])) {
throw new Exception('No to provided when creating a shortcode message');
}

$data['type'] = strtolower($data['type']);

if ($data['type'] === '2fa') {
$m = new TwoFactor($data['to']);
} else if ($data['type'] === 'marketing') {
$m = new Marketing($data['to']);
} else if ($data['type'] === 'alert') {
$m = new Alert($data['to']);
}

if (isset($data['custom'])) {
$m->setCustom($data['custom']);
}

if (isset($data['options'])) {
$m->setOptions($data['options']);
}

return $m;
}
}
15 changes: 15 additions & 0 deletions src/Message/Shortcode/Alert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Nexmo Client Library for PHP
*
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
*/

namespace Nexmo\Message\Shortcode;
use Nexmo\Message\Shortcode;

class Alert extends Shortcode
{
protected $type = 'alert';
}
15 changes: 15 additions & 0 deletions src/Message/Shortcode/Marketing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Nexmo Client Library for PHP
*
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
*/

namespace Nexmo\Message\Shortcode;
use Nexmo\Message\Shortcode;

class Marketing extends Shortcode
{
protected $type = 'marketing';
}
15 changes: 15 additions & 0 deletions src/Message/Shortcode/TwoFactor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Nexmo Client Library for PHP
*
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
*/

namespace Nexmo\Message\Shortcode;
use Nexmo\Message\Shortcode;

class TwoFactor extends Shortcode
{
protected $type = '2fa';
}
82 changes: 82 additions & 0 deletions test/Message/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Nexmo\Client\Exception;
use Nexmo\Message\Client;
use Nexmo\Message\Message;
use Nexmo\Message\Shortcode\TwoFactor;
use Nexmo\Message\Text;
use NexmoTest\Psr7AssertionTrait;
use NexmoTest\MessageAssertionTrait;
Expand Down Expand Up @@ -254,6 +255,87 @@ public function searchRejectionsProvider()
return $r;
}

public function testShortcodeWithObject()
{
$message = new TwoFactor('14155550100', [ 'link' => 'https://example.com' ], ['status-report-req' => 1]);

$this->nexmoClient->send(Argument::that(function(Request $request) {
$this->assertRequestJsonBodyContains('to', '14155550100', $request);
$this->assertRequestJsonBodyContains('link', 'https://example.com', $request);
$this->assertRequestJsonBodyContains('status-report-req', 1, $request);
return true;
}))->willReturn($this->getResponse('success-2fa'));

$response = $this->messageClient->sendShortcode($message);
$this->assertEquals([
'message-count' => '1',
'messages' =>[
[
'status' => '0',
'message-id' => '00000123',
'to' => '14155550100',
'client-ref' => 'client-ref',
'remaining-balance' => '1.10',
'message-price' => '0.05',
'network' => '23410'
]
]
], $response);
}

public function testShortcodeError()
{
$args = [
'to' => '14155550100',
'custom' => [ 'link' => 'https://example.com' ],
'options' => ['status-report-req' => 1],
'type' => '2fa'
];

$this->nexmoClient->send(Argument::that(function(Request $request) use ($args){
return true;
}))->willReturn($this->getResponse('error-2fa'));

$this->expectException(Exception\Request::class);
$this->expectExceptionMessage('Invalid Account for Campaign');

$this->messageClient->sendShortcode($args);
}

public function testShortcodeWithArray()
{
$args = [
'to' => '14155550100',
'custom' => [ 'link' => 'https://example.com' ],
'options' => ['status-report-req' => 1],
'type' => '2fa'
];

$this->nexmoClient->send(Argument::that(function(Request $request) use ($args){
$this->assertRequestJsonBodyContains('to', $args['to'], $request);
$this->assertRequestJsonBodyContains('link', $args['custom']['link'], $request);
$this->assertRequestJsonBodyContains('status-report-req', $args['options']['status-report-req'], $request);
return true;
}))->willReturn($this->getResponse('success-2fa'));

$response = $this->messageClient->sendShortcode($args);
$this->assertEquals([
'message-count' => '1',
'messages' =>[
[
'status' => '0',
'message-id' => '00000123',
'to' => '14155550100',
'client-ref' => 'client-ref',
'remaining-balance' => '1.10',
'message-price' => '0.05',
'network' => '23410'
]
]
], $response);
}


/**
* Get the API response we'd expect for a call to the API. Message API currently returns 200 all the time, so only
* change between success / fail is body of the message.
Expand Down
64 changes: 64 additions & 0 deletions test/Message/ShortcodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Nexmo Client Library for PHP
*
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
*/

namespace NexmoTest\Message;
use Nexmo\Message\Shortcode;
use Nexmo\Message\Shortcode\TwoFactor;
use Nexmo\Message\Shortcode\Marketing;
use Nexmo\Message\Shortcode\Alert;

class ShortcodeTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
}

public function tearDown()
{
}

/**
* @dataProvider typeProvider
*/
public function testType($klass, $expectedType)
{
$m = new $klass('14155550100');
$this->assertEquals($expectedType, $m->getType());
}

/**
* @dataProvider typeProvider
*/
public function testCreateMessageFromArray($expected, $type)
{
$message = Shortcode::createMessageFromArray(['type' => $type, 'to' => '14155550100']);
$this->assertInstanceOf($expected, $message);
}

public function typeProvider()
{
return [
[TwoFactor::class, '2fa'],
[Marketing::class, 'marketing'],
[Alert::class, 'alert']
];
}

public function testGetRequestData()
{
$m = new TwoFactor("14155550100", ['link' => 'https://example.com'], ['status-report-req' => 1]);
$actual = $m->getRequestData();
$this->assertEquals([
'to' => '14155550100',
'link' => 'https://example.com',
'status-report-req' => 1
], $actual);
}


}
9 changes: 9 additions & 0 deletions test/Message/responses/error-2fa.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"message-count": "1",
"messages": [
{
"status": "101",
"error-text": "Invalid Account for Campaign"
}
]
}
14 changes: 14 additions & 0 deletions test/Message/responses/success-2fa.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"message-count":"1",
"messages":[
{
"status":"0",
"message-id":"00000123",
"to":"14155550100",
"client-ref": "client-ref",
"remaining-balance":"1.10",
"message-price":"0.05",
"network":"23410"
}
]
}

0 comments on commit 2f79f67

Please sign in to comment.