Skip to content

Commit 2f79f67

Browse files
committed
Add US shortcode support
1 parent 3dc03ca commit 2f79f67

File tree

9 files changed

+323
-0
lines changed

9 files changed

+323
-0
lines changed

src/Message/Client.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,37 @@ public function send($message)
8181
return $message;
8282
}
8383

84+
public function sendShortcode($message) {
85+
if(!($message instanceof Shortcode)){
86+
$message = Shortcode::createMessageFromArray($message);
87+
}
88+
89+
$params = $message->getRequestData();
90+
91+
$request = new Request(
92+
$this->getClient()->getRestUrl() . '/sc/us/'.$message->getType().'/json'
93+
,'POST',
94+
'php://temp',
95+
['content-type' => 'application/json']
96+
);
97+
98+
$request->getBody()->write(json_encode($params));
99+
$response = $this->client->send($request);
100+
101+
$body = json_decode($response->getBody(), true);
102+
103+
foreach ($body['messages'] as $m) {
104+
if ($m['status'] != '0') {
105+
$e = new Exception\Request($m['error-text'], $m['status']);
106+
$e->setEntity($message);
107+
throw $e;
108+
}
109+
}
110+
111+
return $body;
112+
113+
}
114+
84115
/**
85116
* @param $query
86117
* @return MessageInterface[]

src/Message/Shortcode.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* Nexmo Client Library for PHP
4+
*
5+
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
6+
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
7+
*/
8+
9+
namespace Nexmo\Message;
10+
11+
use Nexmo\Client\Exception\Exception;
12+
use Nexmo\Message\Shortcode\Alert;
13+
use Nexmo\Message\Shortcode\Marketing;
14+
use Nexmo\Message\Shortcode\TwoFactor;
15+
16+
abstract class Shortcode
17+
{
18+
19+
protected $to;
20+
protected $custom;
21+
protected $options;
22+
23+
public function __construct($to, array $custom = [], array $options = []) {
24+
$this->to = $to;
25+
$this->custom = $custom;
26+
$this->options = $options;
27+
}
28+
29+
public function setCustom($custom) {
30+
$this->custom = $custom;
31+
}
32+
33+
public function setOptions($options) {
34+
$this->options = $options;
35+
}
36+
37+
public function getType() {
38+
return $this->type;
39+
}
40+
41+
public function getRequestData() {
42+
// Options, then custom, then to. This is the priority
43+
// we want so that people can't overwrite to with a custom param
44+
return $this->options + $this->custom + [
45+
'to' => $this->to
46+
];
47+
}
48+
49+
public static function createMessageFromArray($data){
50+
if (!isset($data['type'])) {
51+
throw new Exception('No type provided when creating a shortcode message');
52+
}
53+
54+
if (!isset($data['to'])) {
55+
throw new Exception('No to provided when creating a shortcode message');
56+
}
57+
58+
$data['type'] = strtolower($data['type']);
59+
60+
if ($data['type'] === '2fa') {
61+
$m = new TwoFactor($data['to']);
62+
} else if ($data['type'] === 'marketing') {
63+
$m = new Marketing($data['to']);
64+
} else if ($data['type'] === 'alert') {
65+
$m = new Alert($data['to']);
66+
}
67+
68+
if (isset($data['custom'])) {
69+
$m->setCustom($data['custom']);
70+
}
71+
72+
if (isset($data['options'])) {
73+
$m->setOptions($data['options']);
74+
}
75+
76+
return $m;
77+
}
78+
}

src/Message/Shortcode/Alert.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Nexmo Client Library for PHP
4+
*
5+
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
6+
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
7+
*/
8+
9+
namespace Nexmo\Message\Shortcode;
10+
use Nexmo\Message\Shortcode;
11+
12+
class Alert extends Shortcode
13+
{
14+
protected $type = 'alert';
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Nexmo Client Library for PHP
4+
*
5+
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
6+
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
7+
*/
8+
9+
namespace Nexmo\Message\Shortcode;
10+
use Nexmo\Message\Shortcode;
11+
12+
class Marketing extends Shortcode
13+
{
14+
protected $type = 'marketing';
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Nexmo Client Library for PHP
4+
*
5+
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
6+
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
7+
*/
8+
9+
namespace Nexmo\Message\Shortcode;
10+
use Nexmo\Message\Shortcode;
11+
12+
class TwoFactor extends Shortcode
13+
{
14+
protected $type = '2fa';
15+
}

test/Message/ClientTest.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Nexmo\Client\Exception;
1212
use Nexmo\Message\Client;
1313
use Nexmo\Message\Message;
14+
use Nexmo\Message\Shortcode\TwoFactor;
1415
use Nexmo\Message\Text;
1516
use NexmoTest\Psr7AssertionTrait;
1617
use NexmoTest\MessageAssertionTrait;
@@ -254,6 +255,87 @@ public function searchRejectionsProvider()
254255
return $r;
255256
}
256257

258+
public function testShortcodeWithObject()
259+
{
260+
$message = new TwoFactor('14155550100', [ 'link' => 'https://example.com' ], ['status-report-req' => 1]);
261+
262+
$this->nexmoClient->send(Argument::that(function(Request $request) {
263+
$this->assertRequestJsonBodyContains('to', '14155550100', $request);
264+
$this->assertRequestJsonBodyContains('link', 'https://example.com', $request);
265+
$this->assertRequestJsonBodyContains('status-report-req', 1, $request);
266+
return true;
267+
}))->willReturn($this->getResponse('success-2fa'));
268+
269+
$response = $this->messageClient->sendShortcode($message);
270+
$this->assertEquals([
271+
'message-count' => '1',
272+
'messages' =>[
273+
[
274+
'status' => '0',
275+
'message-id' => '00000123',
276+
'to' => '14155550100',
277+
'client-ref' => 'client-ref',
278+
'remaining-balance' => '1.10',
279+
'message-price' => '0.05',
280+
'network' => '23410'
281+
]
282+
]
283+
], $response);
284+
}
285+
286+
public function testShortcodeError()
287+
{
288+
$args = [
289+
'to' => '14155550100',
290+
'custom' => [ 'link' => 'https://example.com' ],
291+
'options' => ['status-report-req' => 1],
292+
'type' => '2fa'
293+
];
294+
295+
$this->nexmoClient->send(Argument::that(function(Request $request) use ($args){
296+
return true;
297+
}))->willReturn($this->getResponse('error-2fa'));
298+
299+
$this->expectException(Exception\Request::class);
300+
$this->expectExceptionMessage('Invalid Account for Campaign');
301+
302+
$this->messageClient->sendShortcode($args);
303+
}
304+
305+
public function testShortcodeWithArray()
306+
{
307+
$args = [
308+
'to' => '14155550100',
309+
'custom' => [ 'link' => 'https://example.com' ],
310+
'options' => ['status-report-req' => 1],
311+
'type' => '2fa'
312+
];
313+
314+
$this->nexmoClient->send(Argument::that(function(Request $request) use ($args){
315+
$this->assertRequestJsonBodyContains('to', $args['to'], $request);
316+
$this->assertRequestJsonBodyContains('link', $args['custom']['link'], $request);
317+
$this->assertRequestJsonBodyContains('status-report-req', $args['options']['status-report-req'], $request);
318+
return true;
319+
}))->willReturn($this->getResponse('success-2fa'));
320+
321+
$response = $this->messageClient->sendShortcode($args);
322+
$this->assertEquals([
323+
'message-count' => '1',
324+
'messages' =>[
325+
[
326+
'status' => '0',
327+
'message-id' => '00000123',
328+
'to' => '14155550100',
329+
'client-ref' => 'client-ref',
330+
'remaining-balance' => '1.10',
331+
'message-price' => '0.05',
332+
'network' => '23410'
333+
]
334+
]
335+
], $response);
336+
}
337+
338+
257339
/**
258340
* Get the API response we'd expect for a call to the API. Message API currently returns 200 all the time, so only
259341
* change between success / fail is body of the message.

test/Message/ShortcodeTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Nexmo Client Library for PHP
4+
*
5+
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
6+
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
7+
*/
8+
9+
namespace NexmoTest\Message;
10+
use Nexmo\Message\Shortcode;
11+
use Nexmo\Message\Shortcode\TwoFactor;
12+
use Nexmo\Message\Shortcode\Marketing;
13+
use Nexmo\Message\Shortcode\Alert;
14+
15+
class ShortcodeTest extends \PHPUnit_Framework_TestCase
16+
{
17+
public function setUp()
18+
{
19+
}
20+
21+
public function tearDown()
22+
{
23+
}
24+
25+
/**
26+
* @dataProvider typeProvider
27+
*/
28+
public function testType($klass, $expectedType)
29+
{
30+
$m = new $klass('14155550100');
31+
$this->assertEquals($expectedType, $m->getType());
32+
}
33+
34+
/**
35+
* @dataProvider typeProvider
36+
*/
37+
public function testCreateMessageFromArray($expected, $type)
38+
{
39+
$message = Shortcode::createMessageFromArray(['type' => $type, 'to' => '14155550100']);
40+
$this->assertInstanceOf($expected, $message);
41+
}
42+
43+
public function typeProvider()
44+
{
45+
return [
46+
[TwoFactor::class, '2fa'],
47+
[Marketing::class, 'marketing'],
48+
[Alert::class, 'alert']
49+
];
50+
}
51+
52+
public function testGetRequestData()
53+
{
54+
$m = new TwoFactor("14155550100", ['link' => 'https://example.com'], ['status-report-req' => 1]);
55+
$actual = $m->getRequestData();
56+
$this->assertEquals([
57+
'to' => '14155550100',
58+
'link' => 'https://example.com',
59+
'status-report-req' => 1
60+
], $actual);
61+
}
62+
63+
64+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"message-count": "1",
3+
"messages": [
4+
{
5+
"status": "101",
6+
"error-text": "Invalid Account for Campaign"
7+
}
8+
]
9+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"message-count":"1",
3+
"messages":[
4+
{
5+
"status":"0",
6+
"message-id":"00000123",
7+
"to":"14155550100",
8+
"client-ref": "client-ref",
9+
"remaining-balance":"1.10",
10+
"message-price":"0.05",
11+
"network":"23410"
12+
}
13+
]
14+
}

0 commit comments

Comments
 (0)