diff --git a/src/Message/Client.php b/src/Message/Client.php index 7a137ce4..f0ac54df 100644 --- a/src/Message/Client.php +++ b/src/Message/Client.php @@ -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[] diff --git a/src/Message/Shortcode.php b/src/Message/Shortcode.php new file mode 100644 index 00000000..8ce1774d --- /dev/null +++ b/src/Message/Shortcode.php @@ -0,0 +1,78 @@ +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; + } +} \ No newline at end of file diff --git a/src/Message/Shortcode/Alert.php b/src/Message/Shortcode/Alert.php new file mode 100644 index 00000000..ee43bcc9 --- /dev/null +++ b/src/Message/Shortcode/Alert.php @@ -0,0 +1,15 @@ + '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. diff --git a/test/Message/ShortcodeTest.php b/test/Message/ShortcodeTest.php new file mode 100644 index 00000000..c62574e1 --- /dev/null +++ b/test/Message/ShortcodeTest.php @@ -0,0 +1,64 @@ +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); + } + + +} diff --git a/test/Message/responses/error-2fa.json b/test/Message/responses/error-2fa.json new file mode 100644 index 00000000..5f4d92a1 --- /dev/null +++ b/test/Message/responses/error-2fa.json @@ -0,0 +1,9 @@ +{ + "message-count": "1", + "messages": [ + { + "status": "101", + "error-text": "Invalid Account for Campaign" + } + ] +} \ No newline at end of file diff --git a/test/Message/responses/success-2fa.json b/test/Message/responses/success-2fa.json new file mode 100644 index 00000000..0a513269 --- /dev/null +++ b/test/Message/responses/success-2fa.json @@ -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" + } + ] +}