Skip to content

Commit cdfc64b

Browse files
committed
Updated using NI Concepts for SMS
- Some shared abstract classes were updated. - Message namespace changed. - Adding a DLR to a message needs more work.
1 parent 7b828ab commit cdfc64b

File tree

20 files changed

+543
-67
lines changed

20 files changed

+543
-67
lines changed

src/Nexmo/Client.php

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function clearSecret()
8484
*/
8585
public function send(RequestInterface $request)
8686
{
87-
$httpRequest = $this->getClient()->post($this->base . $request->getURI());
87+
$httpRequest = $this->getClient()->post(implode('/', array($this->base, $request->getURI())));
8888
$this->authRequest($httpRequest);
8989

9090
$params = $request->getParams();
@@ -114,33 +114,6 @@ public function send(RequestInterface $request)
114114
return $response;
115115
}
116116

117-
/**
118-
* Send a message via SMS.
119-
* @param MessageInterface $message
120-
* @param string $url
121-
* @return Response
122-
*/
123-
public function sendSMS(MessageInterface $message, $url = self::URL_SMS)
124-
{
125-
$request = $this->getClient()->post($this->base . $url);
126-
$this->authRequest($request);
127-
128-
$params = $message->getParams();
129-
130-
//if we have a secret, use it to sign the request
131-
if($this->secret){
132-
//include any query params auth might have added
133-
$signature = new Signature(array_merge($params, $request->getQuery()->getAll()), $this->secret);
134-
//filter any params that were in the query
135-
$params = array_diff_assoc($signature->getSignedParams(), $request->getQuery()->getAll());
136-
}
137-
138-
$request->addPostFields($params);
139-
140-
$response = $request->send();
141-
return $this->parseResponse($response);
142-
}
143-
144117
/**
145118
* Check a response for errors, and get return value.
146119
* @param \Guzzle\Http\Message\Response $response

src/Nexmo/Client/Callback/Callback.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,19 @@ class Callback implements CallbackInterface
1212
const ENV_POST = 'post';
1313
const ENV_GET = 'get';
1414

15+
protected $expected = array();
1516
protected $data;
1617

1718

1819
public function __construct(array $data)
1920
{
21+
$keys = array_keys($data);
22+
$missing = array_diff($this->expected, $keys);
23+
24+
if($missing){
25+
throw new \RuntimeException('missing expected callback keys: ' . implode(', ', $missing));
26+
}
27+
2028
$this->data = $data;
2129
}
2230

src/Nexmo/Client/Response/AbstractResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function getData()
1717

1818
public function isSuccess()
1919
{
20-
return $this->data['status'] == 0;
20+
return isset($this->data['status']) AND $this->data['status'] == 0;
2121
}
2222

2323
public function isError()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* @author Tim Lytle <tim@timlytle.net>
4+
*/
5+
6+
namespace Nexmo\Client\Response;
7+
8+
9+
class Error extends Response
10+
{
11+
public function __construct($data)
12+
{
13+
//normalize the data
14+
if(isset($data['error_text'])){
15+
$data['error-text'] = $data['error_text'];
16+
}
17+
18+
$this->expected = ['status', 'error-text'];
19+
20+
return parent::__construct($data);
21+
}
22+
23+
public function isError()
24+
{
25+
return true;
26+
}
27+
28+
public function isSuccess()
29+
{
30+
return false;
31+
}
32+
33+
public function getCode()
34+
{
35+
return $this->data['status'];
36+
}
37+
38+
public function getMessage()
39+
{
40+
return $this->data['error-text'];
41+
}
42+
}

src/Nexmo/Client/Response/Response.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77

88
class Response extends AbstractResponse implements ResponseInterface
99
{
10-
protected $expected = array('status');
10+
/**
11+
* Allow specific responses to easily define required parameters.
12+
* @var array
13+
*/
14+
protected $expected = array();
1115

1216
public function __construct(array $data)
1317
{

src/Nexmo/Message/Binary.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<?php
22
namespace Nexmo\Message;
3-
use Nexmo\MessageAbstract;
4-
use Nexmo\MessageInterface;
3+
use Nexmo\Message\MessageAbstract;
54

65
/**
76
* SMS Binary Message
87
* @author Tim Lytle <tim.lytle@nexmo.com>
98
*/
10-
class Binary extends MessageAbstract implements MessageInterface
9+
class Binary extends MessageAbstract
1110
{
1211
const TYPE = 'binary';
1312

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
/**
3+
* @author Tim Lytle <tim@timlytle.net>
4+
*/
5+
6+
namespace Nexmo\Message\Callback;
7+
use Nexmo\Client\Callback\Callback;
8+
9+
class Receipt extends Callback
10+
{
11+
protected $expected = array(
12+
'err-code',
13+
'message-timestamp',
14+
'msisdn',
15+
'network-code',
16+
'price',
17+
'scts',
18+
'status',
19+
//'timestamp',
20+
'to'
21+
);
22+
23+
public function __construct(array $data)
24+
{
25+
//default value
26+
$data = array_merge(array('client-ref' => null), $data);
27+
28+
parent::__construct($data);
29+
}
30+
31+
/**
32+
* @return int
33+
*/
34+
public function getErrorCode()
35+
{
36+
return (int) $this->data['err-code'];
37+
}
38+
39+
/**
40+
* @return string
41+
*/
42+
public function getNetwork()
43+
{
44+
return (string) $this->data['network-code'];
45+
}
46+
47+
/**
48+
* @return string
49+
*/
50+
public function getId()
51+
{
52+
return (string) $this->data['messageId'];
53+
}
54+
55+
/**
56+
* @return string
57+
*/
58+
public function getReceiptFrom()
59+
{
60+
return (string) $this->data['msisdn'];
61+
}
62+
63+
/**
64+
* @return string
65+
*/
66+
public function getTo()
67+
{
68+
return $this->getReceiptFrom();
69+
}
70+
71+
/**
72+
* @return string
73+
*/
74+
public function getReceiptTo()
75+
{
76+
return (string) $this->data['to'];
77+
}
78+
79+
/**
80+
* @return string
81+
*/
82+
public function getFrom()
83+
{
84+
return $this->getReceiptTo();
85+
}
86+
87+
/**
88+
* @return string
89+
*/
90+
public function getStatus()
91+
{
92+
return (string) $this->data['status'];
93+
}
94+
95+
/**
96+
* @return string
97+
*/
98+
public function getPrice()
99+
{
100+
return (string) $this->data['price'];
101+
}
102+
103+
/**
104+
* @return \DateTime
105+
*/
106+
public function getTimestamp()
107+
{
108+
$date = \DateTime::createFromFormat('ymdHi', $this->data['scts']);
109+
if($date){
110+
return $date;
111+
}
112+
113+
throw new \UnexpectedValueException('could not parse message timestamp');
114+
}
115+
116+
/**
117+
* @return \DateTime
118+
*/
119+
public function getSent()
120+
{
121+
$date = \DateTime::createFromFormat('Y-m-d H:i:s', $this->data['message-timestamp']);
122+
if($date){
123+
return $date;
124+
}
125+
126+
throw new \UnexpectedValueException('could not parse message timestamp');
127+
}
128+
129+
/**
130+
* @return string|null
131+
*/
132+
public function getClientRef()
133+
{
134+
return $this->data['client-ref'];
135+
}
136+
}
Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
2-
namespace Nexmo;
3-
use Nexmo\MessageInterface;
2+
namespace Nexmo\Message;
3+
use Nexmo\Client\Request\RequestInterface;
4+
use Nexmo\Client\Request\WrapResponseInterface;
5+
use Nexmo\Client\Response\ResponseInterface;
46

57
/**
68
* SMS Text Message
@@ -9,7 +11,7 @@
911
* @todo Should implement from some interface / extend an abstract. Will know
1012
* better what that should look like once all the message types are built.
1113
*/
12-
class MessageAbstract implements MessageInterface
14+
class MessageAbstract implements RequestInterface, WrapResponseInterface
1315
{
1416
const TYPE = 'abstract';
1517

@@ -98,7 +100,7 @@ public function getParams()
98100
return $params;
99101
}
100102

101-
public function requestDLR($dlr)
103+
public function requestDLR($dlr = true)
102104
{
103105
$this->dlr = $dlr ? 1 : 0;
104106
}
@@ -122,4 +124,18 @@ public function setClass($class)
122124
{
123125
$this->class = $class;
124126
}
127+
128+
public function getUri()
129+
{
130+
return 'sms/json';
131+
}
132+
133+
/**
134+
* @param ResponseInterface $response
135+
* @return ResponseInterface
136+
*/
137+
public function wrapResponse(ResponseInterface $response)
138+
{
139+
return new Response\Collection($response->getData());
140+
}
125141
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
namespace Nexmo;
2+
namespace Nexmo\Message;
33

44
interface MessageInterface
55
{

0 commit comments

Comments
 (0)