Skip to content

Commit

Permalink
Added parser for responses
Browse files Browse the repository at this point in the history
  • Loading branch information
vdbelt committed Apr 1, 2020
1 parent 54c6a90 commit e15618b
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 16 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"php": "^7.4",
"guzzlehttp/guzzle": "~6.0",
"ext-dom": "*",
"ext-json": "*"
"ext-json": "*",
"ext-simplexml": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.0"
Expand Down
28 changes: 15 additions & 13 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Inmobile;

use GuzzleHttp\Client;
use Inmobile\Exceptions\ResponseExceptionHandler;
use Psr\Http\Message\ResponseInterface;
use Inmobile\Response\MessagesSent;
use Inmobile\Exceptions\EmptyMessagePayload;
use Inmobile\Exceptions\ResponseExceptionHandler;

class Gateway
{
Expand Down Expand Up @@ -39,6 +39,19 @@ public function getMessages()
{
return $this->messages;
}

public function send() : MessagesSent
{
if (count($this->messages) < 1) {
throw new EmptyMessagePayload;
}

$response = $this->client->post('/Api/V2/SendMessages', [ 'form_params' => [ 'xml' => $this->toXml() ] ] );

$response = $this->responseExceptionHandler->transformResponseToException($response);

return new MessagesSent($response);
}

public function toXml()
{
Expand All @@ -61,15 +74,4 @@ public function toXml()

return $dom->saveXML($dom->documentElement);
}

public function send(): ResponseInterface
{
if (count($this->messages) < 1) {
throw new EmptyMessagePayload;
}

$response = $this->client->post('/Api/V2/SendMessages', [ 'form_params' => [ 'xml' => $this->toXml() ] ] );

return $this->responseExceptionHandler->transformResponseToException($response);
}
}
44 changes: 44 additions & 0 deletions src/Response/MessagesSent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php


namespace Inmobile\Response;


use Psr\Http\Message\ResponseInterface;

class MessagesSent
{
protected ResponseInterface $response;
protected ?\SimpleXMLElement $xml = null;

public function __construct(ResponseInterface $response)
{
$this->response = $response;
}

public function getResponse() : ResponseInterface
{
return $this->response;
}

public function id() : string
{
return array_values($this->ids()[0])[0];
}

public function ids() : array
{
$this->consumeResponseAndLoadXML();

$recipients = json_decode(json_encode($this->xml), true)['recipient'];

return array_map(fn($r) => [$r['@attributes']['msisdn'] => $r['@attributes']['id']], $recipients);
}

private function consumeResponseAndLoadXML() : void
{
if(null === $this->xml) {
$this->xml = simplexml_load_string($this->getResponse()->getBody()->getContents());
}
}
}
38 changes: 36 additions & 2 deletions tests/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,41 @@ public function testGatewayCanSendMessages()

$response = $gateway->send();

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals($stub, $response->getBody()->getContents());
$this->assertEquals(200, $response->getResponse()->getStatusCode());
$this->assertEquals($stub, $response->getResponse()->getBody()->getContents());
}

public function testGatewayCanReturnMessageIds()
{
$stub = <<<XML
<reply>
<recipient msisdn="4512345679" id="123" />
<recipient msisdn="4512345679" id="13cab0f4-0e4f-44cf-8f84-a9eb435f36a4" />
</reply>
XML;
$mockedResponse = new Response(200, [], $stub);

$mock = new MockHandler([$mockedResponse, $mockedResponse]);
$mockHandler = new HandlerStack($mock);
$client = new Client(['handler' => $mockHandler]);

$gateway = new Gateway('foo', $client);

$gateway->addMessage(
Message::create('foo')
->from('1245')
->to([
(new Recipient('4512345679'))->withMessageId('123'),
'4512345679'
])
);

$response = $gateway->send();

$firstId = $response->id();
$ids = $response->ids();

$this->assertEquals('123', $firstId);
$this->assertEquals([['4512345679' => '123'], ['4512345679' => '13cab0f4-0e4f-44cf-8f84-a9eb435f36a4']], $ids);
}
}

0 comments on commit e15618b

Please sign in to comment.