Skip to content

Commit 60f4c4e

Browse files
committed
Added Serialization and Rate Limit:
- Verification object can now be serialized / unserialized. - Client retires rate limit responses from API. This is not configurable. - TODO: Make rate limit retry configurable. - TODO: Test backoff time matches API response.
1 parent 23c6323 commit 60f4c4e

File tree

5 files changed

+105
-2
lines changed

5 files changed

+105
-2
lines changed

src/Message/Client.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function send($message)
3434
$message = $this->createMessageFromArray($message);
3535
}
3636

37-
$params = $message->getRequestData();
37+
$params = $message->getRequestData(false);
3838

3939
$request = new Request(
4040
\Nexmo\Client::BASE_REST . '/sms/json'
@@ -59,6 +59,14 @@ public function send($message)
5959
switch($part['status']){
6060
case '0':
6161
continue; //all okay
62+
case '1':
63+
if(preg_match('#\[\s+(\d+)\s+\]#', $part['error-text'], $match)){
64+
usleep($match[1] + 1);
65+
} else {
66+
sleep(1);
67+
}
68+
69+
return $this->send($message);
6270
case '5':
6371
$e = new Exception\Server($part['error-text'], $part['status']);
6472
$e->setEntity($message);

src/Verify/Verification.php

+35-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Nexmo\Entity\Psr7Trait;
1414
use Nexmo\Entity\RequestArrayTrait;
1515

16-
class Verification implements VerificationInterface, \ArrayAccess
16+
class Verification implements VerificationInterface, \ArrayAccess, \Serializable
1717
{
1818
use Psr7Trait;
1919
use RequestArrayTrait;
@@ -559,4 +559,38 @@ protected function getReadOnlyException($offset)
559559
$offset
560560
));
561561
}
562+
563+
public function serialize()
564+
{
565+
$data = [
566+
'requestData' => $this->requestData
567+
];
568+
569+
if($request = $this->getRequest()){
570+
$data['request'] = \Zend\Diactoros\Request\Serializer::toString($request);
571+
}
572+
573+
if($response = $this->getResponse()){
574+
$data['response'] = \Zend\Diactoros\Response\Serializer::toString($response);
575+
}
576+
577+
return serialize($data);
578+
}
579+
580+
public function unserialize($serialized)
581+
{
582+
$data = unserialize($serialized);
583+
584+
$this->requestData = $data['requestData'];
585+
586+
if(isset($data['request'])){
587+
$this->request = \Zend\Diactoros\Request\Serializer::fromString($data['request']);
588+
}
589+
590+
if(isset($data['response'])){
591+
$this->response = \Zend\Diactoros\Response\Serializer::fromString($data['response']);
592+
}
593+
}
594+
595+
562596
}

test/Message/ClientTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,28 @@ public function testCanSearchBySingleInboundId()
160160
$this->assertSame($response, $message->getResponse());
161161
}
162162

163+
public function testRateLimitRetires()
164+
{
165+
$rate = $this->getResponse('ratelimit');
166+
$success = $this->getResponse('success');
167+
168+
$args = [
169+
'to' => '14845551345',
170+
'from' => '1105551334',
171+
'text' => 'test message'
172+
];
173+
174+
$this->nexmoClient->send(Argument::that(function(Request $request) use ($args){
175+
$this->assertRequestJsonBodyContains('to', $args['to'], $request);
176+
$this->assertRequestJsonBodyContains('from', $args['from'], $request);
177+
$this->assertRequestJsonBodyContains('text', $args['text'], $request);
178+
return true;
179+
}))->willReturn($rate, $rate, $success);
180+
181+
$message = $this->messageClient->send(new Text($args['to'], $args['from'], $args['text']));
182+
$this->assertEquals($success, $message->getResponse());
183+
}
184+
163185
/**
164186
* Get the API response we'd expect for a call to the API. Message API currently returns 200 all the time, so only
165187
* change between success / fail is body of the message.

test/Message/responses/ratelimit.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"message-count": "1",
3+
"messages": [
4+
{
5+
"to": "14843472194",
6+
"status": "1",
7+
"error-text": "Throughput Rate Exceeded - please wait [ 97 ] and retry",
8+
"network": "310260"
9+
}
10+
]
11+
}

test/Verify/VerificationTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,34 @@ public function testExceptionForCheckFail()
253253
$this->exsisting->check('4321');
254254
}
255255

256+
/**
257+
* @dataProvider getSerializeResponses
258+
*/
259+
public function testSerialize($response)
260+
{
261+
$this->exsisting->setResponse($response);
262+
$this->exsisting->getResponse()->getBody()->rewind();
263+
$this->exsisting->getResponse()->getBody()->getContents();
264+
$serialized = serialize($this->exsisting);
265+
/* @var $unserialized Verification */
266+
$unserialized = unserialize($serialized);
267+
268+
$this->assertInstanceOf(get_class($this->exsisting), $unserialized);
269+
270+
$this->assertEquals($this->exsisting->getAccountId(), $unserialized->getAccountId());
271+
$this->assertEquals($this->exsisting->getStatus(), $unserialized->getStatus());
272+
273+
$this->assertEquals($this->exsisting->getResponseData(), $unserialized->getResponseData());
274+
}
275+
276+
public function getSerializeResponses()
277+
{
278+
return [
279+
[$this->getResponse('search')],
280+
[$this->getResponse('start')],
281+
];
282+
}
283+
256284
/**
257285
* @dataProvider getClientProxyMethods
258286
*/

0 commit comments

Comments
 (0)