Skip to content

Commit 96afa7e

Browse files
committed
Added Message Search to Client:
- Can search with message objet or ID. - Normalized `getBody()` for both message types.
1 parent a38409d commit 96afa7e

File tree

5 files changed

+113
-5
lines changed

5 files changed

+113
-5
lines changed

src/Message/Client.php

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,69 @@ public function send($message)
7373
return $message;
7474
}
7575

76+
/**
77+
* @param string|MessageInterface $idOrMessage
78+
*/
79+
public function search($idOrMessage)
80+
{
81+
if($idOrMessage instanceof MessageInterface){
82+
$id = $idOrMessage->getMessageId();
83+
$message = $idOrMessage;
84+
} else {
85+
$id = $idOrMessage;
86+
}
87+
88+
$request = new Request(
89+
\Nexmo\Client::BASE_REST . '/search/message?' . http_build_query(['id' => $id]),
90+
'GET',
91+
'php://temp',
92+
['Accept' => 'application/json']
93+
);
94+
95+
$response = $this->client->send($request);
96+
97+
$response->getBody()->rewind();
98+
99+
$data = json_decode($response->getBody()->getContents(), true);
100+
101+
if(!$data){
102+
throw new Exception\Request('no message found for `' . $id . '`');
103+
}
104+
105+
if($response->getStatusCode() != '200' && isset($data['error-code'])){
106+
throw new Exception\Request($data['error-code-label'], $data['error-code']);
107+
} elseif($response->getStatusCode() != '200'){
108+
throw new Exception\Request('error status from API', $response->getStatusCode());
109+
}
110+
111+
112+
switch($data['type']){
113+
case 'MT':
114+
$new = new Message($data['message-id']);
115+
break;
116+
case 'MO':
117+
$new = new InboundMessage($data['message-id']);
118+
break;
119+
default:
120+
throw new Exception\Exception('unexpected response from API');
121+
}
122+
123+
if(isset($message) && !($message instanceof $new)){
124+
throw new Exception\Exception(sprintf(
125+
'searched for message with type `%s` but message of type `%s`',
126+
get_class($message),
127+
get_class($new)
128+
));
129+
}
130+
131+
if(!isset($message)){
132+
$message = $new;
133+
}
134+
135+
$message->setResponse($response);
136+
return $message;
137+
}
138+
76139
/**
77140
* @param array $message
78141
* @return Message
@@ -97,7 +160,7 @@ protected function createMessageFromArray($message)
97160

98161
return new Message($to, $from, $message);
99162
}
100-
163+
101164
/**
102165
* Convenience feature allowing messages to be sent without creating a message object first.
103166
*

src/Message/InboundMessage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function getMessageId()
9191
return $this['messageId'];
9292
}
9393

94-
public function getText()
94+
public function getBody()
9595
{
9696
if($this->getRequest()){
9797
return $this['text'];

src/Message/MessageInterface.php

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

1111
interface MessageInterface extends \Nexmo\Entity\EntityInterface
1212
{
13-
13+
public function getMessageId();
1414
}

test/Message/ClientTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace NexmoTest\Message;
1010

1111
use Nexmo\Message\Client;
12+
use Nexmo\Message\Message;
1213
use Nexmo\Message\Text;
1314
use NexmoTest\Psr7AssertionTrait;
1415
use Prophecy\Argument;
@@ -115,6 +116,50 @@ public function testThrowServerException()
115116
}
116117
}
117118

119+
public function testCanSearchByMessage()
120+
{
121+
$message = new Message('02000000D912945A');
122+
$response = $this->getResponse('search-outbound');
123+
124+
$this->nexmoClient->send(Argument::that(function(Request $request) {
125+
$this->assertRequestQueryContains('id', '02000000D912945A', $request);
126+
return true;
127+
}))->willReturn($response);
128+
129+
$this->messageClient->search($message);
130+
$this->assertSame($response, $message->getResponse());
131+
}
132+
133+
public function testCanSearchBySingleOutboundId()
134+
{
135+
$response = $this->getResponse('search-outbound');
136+
137+
$this->nexmoClient->send(Argument::that(function(Request $request) {
138+
$this->assertRequestQueryContains('id', '02000000D912945A', $request);
139+
return true;
140+
}))->willReturn($response);
141+
142+
$message = $this->messageClient->search('02000000D912945A');
143+
144+
$this->assertInstanceOf('Nexmo\Message\Message', $message);
145+
$this->assertSame($response, $message->getResponse());
146+
}
147+
148+
public function testCanSearchBySingleInboundId()
149+
{
150+
$response = $this->getResponse('search-inbound');
151+
152+
$this->nexmoClient->send(Argument::that(function(Request $request) {
153+
$this->assertRequestQueryContains('id', '02000000DA7C52E7', $request);
154+
return true;
155+
}))->willReturn($response);
156+
157+
$message = $this->messageClient->search('02000000DA7C52E7');
158+
159+
$this->assertInstanceOf('Nexmo\Message\InboundMessage', $message);
160+
$this->assertSame($response, $message->getResponse());
161+
}
162+
118163
/**
119164
* Get the API response we'd expect for a call to the API. Message API currently returns 200 all the time, so only
120165
* change between success / fail is body of the message.

test/Message/InboundMessageTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function testRequestObjectAccess($request)
4242
$this->assertEquals('14845552121', $message->getFrom());
4343
$this->assertEquals('16105553939', $message->getTo());
4444
$this->assertEquals('02000000DA7C52E7', $message->getMessageId());
45-
$this->assertEquals('Test this.', $message->getText());
45+
$this->assertEquals('Test this.', $message->getBody());
4646
$this->assertEquals('text', $message->getType());
4747
}
4848

@@ -74,7 +74,7 @@ public function testResponseObjectAccess($response)
7474
$this->assertEquals('14845552121', $message->getFrom());
7575
$this->assertEquals('16105553939', $message->getTo());
7676
$this->assertEquals('02000000DA7C52E7', $message->getMessageId());
77-
$this->assertEquals('Test this.', $message->getText());
77+
$this->assertEquals('Test this.', $message->getBody());
7878
$this->assertEquals('6cff3913', $message->getAccountId());
7979
$this->assertEquals('US-VIRTUAL-BANDWIDTH', $message->getNetwork());
8080
}

0 commit comments

Comments
 (0)