Skip to content

Commit a38409d

Browse files
committed
Added Support For Inblund and Fetched Messages:
- Inbound message shares interface, but lacks message creation methods. - Both inbound and outbound messages can use a `search/message` response for data. - No support for `search/messages` at this time. - Split some context specific tests into specfic classes (not sure about that yet). - TODO: Should move context specific methods into traits, so can be reused if the entity is replaced. - TODO: Tests pass, but need to make the array access, detection of fetch response vrs sent message response cleaner, reuse code more. - Updated some constructor args (including verify) so 'id or' arguments are more obvious.
1 parent 0a399c2 commit a38409d

File tree

12 files changed

+846
-201
lines changed

12 files changed

+846
-201
lines changed

src/Message/InboundMessage.php

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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\Entity\JsonResponseTrait;
12+
use Nexmo\Entity\Psr7Trait;
13+
use Psr\Http\Message\ServerRequestInterface;
14+
15+
class InboundMessage implements MessageInterface, \ArrayAccess
16+
{
17+
use Psr7Trait;
18+
use JsonResponseTrait;
19+
20+
protected $id;
21+
22+
/**
23+
* InboundMessage constructor.
24+
* @param string|ServerRequestInterface $idOrRequest Message ID, or inbound HTTP request.
25+
*/
26+
public function __construct($idOrRequest)
27+
{
28+
if($idOrRequest instanceof ServerRequestInterface){
29+
$this->setRequest($idOrRequest);
30+
return;
31+
}
32+
33+
if(is_string($idOrRequest)){
34+
$this->id = $idOrRequest;
35+
return;
36+
}
37+
38+
throw new \RuntimeException(sprintf(
39+
'`%s` must be constructed with a server request or a message id',
40+
self::class
41+
));
42+
}
43+
44+
public function getRequestData($sent = true)
45+
{
46+
$request = $this->getRequest();
47+
48+
if(is_null($request)){
49+
return [];
50+
}
51+
52+
if(!($request instanceof ServerRequestInterface)){
53+
throw new \RuntimeException('inbound message request should only ever be `' . ServerRequestInterface::class . '`');
54+
}
55+
56+
switch($request->getMethod()){
57+
case 'POST':
58+
$params = $request->getParsedBody();
59+
break;
60+
case 'GET':
61+
$params = $request->getQueryParams();
62+
break;
63+
default:
64+
$params = [];
65+
break;
66+
}
67+
68+
return $params;
69+
}
70+
71+
public function getFrom()
72+
{
73+
if($this->getRequest()){
74+
return $this['msisdn'];
75+
} else {
76+
return $this['from'];
77+
}
78+
}
79+
80+
public function getTo()
81+
{
82+
return $this['to'];
83+
}
84+
85+
public function getMessageId()
86+
{
87+
if(isset($this->id)){
88+
return $this->id;
89+
}
90+
91+
return $this['messageId'];
92+
}
93+
94+
public function getText()
95+
{
96+
if($this->getRequest()){
97+
return $this['text'];
98+
} else {
99+
return $this['body'];
100+
}
101+
}
102+
103+
public function getType()
104+
{
105+
return $this['type'];
106+
}
107+
108+
public function getAccountId()
109+
{
110+
return $this['account-id'];
111+
}
112+
113+
public function getNetwork()
114+
{
115+
return $this['network'];
116+
}
117+
118+
/**
119+
* Allow the object to access the data from the API response, a sent API request, or the user set data that the
120+
* request will be created from - in that order.
121+
*
122+
* @param mixed $offset
123+
* @return bool
124+
* @throws \Exception
125+
*/
126+
public function offsetExists($offset)
127+
{
128+
$response = $this->getResponseData();
129+
$request = $this->getRequestData();
130+
$dirty = $this->getRequestData(false);
131+
return isset($response[$offset]) || isset($request[$offset]) || isset($dirty[$offset]);
132+
}
133+
134+
/**
135+
* Allow the object to access the data from the API response, a sent API request, or the user set data that the
136+
* request will be created from - in that order.
137+
*
138+
* @param mixed $offset
139+
* @return mixed
140+
* @throws \Exception
141+
*/
142+
public function offsetGet($offset)
143+
{
144+
$response = $this->getResponseData();
145+
$request = $this->getRequestData();
146+
$dirty = $this->getRequestData(false);
147+
148+
if(isset($response[$offset])){
149+
return $response[$offset];
150+
}
151+
152+
if(isset($request[$offset])){
153+
return $request[$offset];
154+
}
155+
156+
if(isset($dirty[$offset])){
157+
return $dirty[$offset];
158+
}
159+
}
160+
161+
/**
162+
* All properties are read only.
163+
*
164+
* @param mixed $offset
165+
* @param mixed $value
166+
*/
167+
public function offsetSet($offset, $value)
168+
{
169+
throw $this->getReadOnlyException($offset);
170+
}
171+
172+
/**
173+
* All properties are read only.
174+
*
175+
* @param mixed $offset
176+
*/
177+
public function offsetUnset($offset)
178+
{
179+
throw $this->getReadOnlyException($offset);
180+
}
181+
182+
/**
183+
* All properties are read only.
184+
*
185+
* @param $offset
186+
* @return \RuntimeException
187+
*/
188+
protected function getReadOnlyException($offset)
189+
{
190+
return new \RuntimeException(sprintf(
191+
'can not modify `%s` using array access',
192+
$offset
193+
));
194+
}
195+
}

src/Message/Message.php

Lines changed: 103 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,21 @@ class Message implements MessageInterface, \Countable, \ArrayAccess, \Iterator
3737

3838
protected $current = 0;
3939

40+
protected $id;
41+
4042
/**
41-
* @param string $to E.164 (international) formatted number
42-
* @param string $from Number or name the message is from
43+
* @param string $idOrTo Message ID or E.164 (international) formatted number to send the message
44+
* @param null|string $from Number or name the message is from
4345
* @param array $additional Additional API Params
4446
*/
45-
public function __construct($to, $from, $additional = [])
47+
public function __construct($idOrTo, $from = null, $additional = [])
4648
{
47-
$this->requestData['to'] = (string) $to;
49+
if(is_null($from)){
50+
$this->id = $idOrTo;
51+
return;
52+
}
53+
54+
$this->requestData['to'] = (string) $idOrTo;
4855
$this->requestData['from'] = (string) $from;
4956
if(static::TYPE){
5057
$this->requestData['type'] = static::TYPE;
@@ -88,8 +95,12 @@ public function count()
8895
return count($data['messages']);
8996
}
9097

91-
public function getId($index = null)
98+
public function getMessageId($index = null)
9299
{
100+
if(isset($this->id)){
101+
return $this->id;
102+
}
103+
93104
return $this->getMessageData('message-id', $index);
94105
}
95106

@@ -100,7 +111,15 @@ public function getStatus($index = null)
100111

101112
public function getTo($index = null)
102113
{
103-
return $this->getMessageData('to', $index);
114+
$data = $this->getResponseData();
115+
116+
//check if this is data from a send request
117+
//(which also has a status, but it's not the same)
118+
if(isset($data['messages'])){
119+
return $this->getMessageData('to', $index);
120+
}
121+
122+
return $this['to'];
104123
}
105124

106125
public function getRemainingBalance($index = null)
@@ -110,14 +129,60 @@ public function getRemainingBalance($index = null)
110129

111130
public function getPrice($index = null)
112131
{
113-
return $this->getMessageData('message-price', $index);
132+
$data = $this->getResponseData();
133+
134+
//check if this is data from a send request
135+
//(which also has a status, but it's not the same)
136+
if(isset($data['messages'])){
137+
return $this->getMessageData('message-price', $index);
138+
}
139+
140+
return $this['price'];
114141
}
115142

116143
public function getNetwork($index = null)
117144
{
118145
return $this->getMessageData('network', $index);
119146
}
120-
147+
148+
public function getDeliveryStatus()
149+
{
150+
$data = $this->getResponseData();
151+
152+
//check if this is data from a send request
153+
//(which also has a status, but it's not the same)
154+
if(isset($data['messages'])){
155+
return;
156+
}
157+
158+
return $this['status'];
159+
}
160+
161+
public function getFrom()
162+
{
163+
return $this['from'];
164+
}
165+
166+
public function getBody()
167+
{
168+
return $this['body'];
169+
}
170+
171+
public function getDateReceived()
172+
{
173+
return new \DateTime($this['date-received']);
174+
}
175+
176+
public function getDeliveryError()
177+
{
178+
return $this['error-code'];
179+
}
180+
181+
public function getDeliveryLabel()
182+
{
183+
return $this['error-code-label'];
184+
}
185+
121186
protected function getMessageData($name, $index = null)
122187
{
123188
if(!isset($this->response)){
@@ -133,10 +198,14 @@ protected function getMessageData($name, $index = null)
133198

134199
public function offsetExists($offset)
135200
{
136-
if(in_array($offset, $this->responseParams)){
201+
$response = $this->getResponseData();
202+
$request = $this->getRequestData();
203+
$dirty = $this->getRequestData(false);
204+
if(isset($response[$offset]) || isset($request[$offset]) || isset($dirty[$offset])){
137205
return true;
138206
}
139207

208+
//provide access to split messages by virtual index
140209
if(is_int($offset) && $offset < $this->count()){
141210
return true;
142211
}
@@ -146,18 +215,35 @@ public function offsetExists($offset)
146215

147216
public function offsetGet($offset)
148217
{
149-
if(!isset($this->response)){
150-
return null;
218+
$response = $this->getResponseData();
219+
$request = $this->getRequestData();
220+
$dirty = $this->getRequestData(false);
221+
222+
if(isset($response[$offset])){
223+
return $response[$offset];
151224
}
152225

153-
$data = $this->getResponseData();
226+
//provide access to split messages by virtual index, if there is data
227+
if(isset($response['messages'])){
228+
if(is_int($offset) && isset($response['messages'][$offset])){
229+
return $response['messages'][$offset];
230+
}
231+
232+
$index = $this->count() -1;
233+
234+
if(isset($response['messages'][$index]) && isset($response['messages'][$index][$offset])){
235+
return $response['messages'][$index][$offset];
236+
}
237+
238+
}
154239

155-
if(is_int($offset)){
156-
return $data['messages'][$offset];
240+
if(isset($request[$offset])){
241+
return $request[$offset];
157242
}
158243

159-
$index = $this->count() -1;
160-
return $data['messages'][$index][$offset];
244+
if(isset($dirty[$offset])){
245+
return $dirty[$offset];
246+
}
161247
}
162248

163249
public function offsetSet($offset, $value)
@@ -218,4 +304,5 @@ public function rewind()
218304
}
219305

220306

307+
221308
}

src/Message/MessageInterface.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,5 @@
1010

1111
interface MessageInterface extends \Nexmo\Entity\EntityInterface
1212
{
13-
public function requestDLR($dlr = true);
14-
15-
public function setClientRef($ref);
16-
17-
public function setNetwork($network);
18-
19-
public function setTTL($ttl);
20-
21-
public function setClass($class);
13+
2214
}

0 commit comments

Comments
 (0)