Skip to content

Commit

Permalink
Add support for DispatchService multiple messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Doelia committed Oct 24, 2018
1 parent 922d455 commit 41b5bc9
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 7 deletions.
30 changes: 30 additions & 0 deletions src/Esendex/DispatchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,36 @@ public function send(Model\DispatchMessage $message)
}
}

/**
* @param array of Model\DispatchMessage $messages
* @return array of Model\ResultItem
* @throws Exceptions\EsendexException
*/
public function sendMultiple(array $messages)
{
$xml = $this->parser->encodeMultiple($messages);
$uri = Http\UriBuilder::serviceUri(
self::DISPATCH_SERVICE_VERSION,
self::DISPATCH_SERVICE,
null,
$this->httpClient->isSecure()
);

$result = $this->httpClient->post(
$uri,
$this->authentication,
$xml
);

$arr = $this->parser->parse($result);

if (count($arr) >= 1) {
return $arr;
} else {
throw new Exceptions\EsendexException("Error parsing the dispatch result", null, array('data_returned' => $result));
}
}

/**
* Get the number of remaining credits for your account
*
Expand Down
39 changes: 32 additions & 7 deletions src/Esendex/Parser/DispatchXmlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,38 @@ function __construct($accountReference)
}

public function encode(\Esendex\Model\DispatchMessage $message)
{
$doc = new \SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\"?><messages />", 0, false, Api::NS);
$doc->addAttribute("xmlns", Api::NS);
$doc->accountreference = $this->reference;
if ($message->characterSet() != null)
$doc->characterset = $message->characterSet();

$this->addMessage($doc, $message);

return $doc->asXML();
}

public function encodeMultiple(array $messages)
{
if (count($messages) < 1) {
throw new ArgumentException("No message found");
}

$doc = new \SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\"?><messages />", 0, false, Api::NS);
$doc->addAttribute("xmlns", Api::NS);
$doc->accountreference = $this->reference;
if ($messages[0]->characterSet() != null)
$doc->characterset = $messages[0]->characterSet();

foreach ($messages as $message) {
$this->addMessage($doc, $message);
}

return $doc->asXML();
}

public function addMessage(\SimpleXMLElement $doc, \Esendex\Model\DispatchMessage $message)
{
if ($message->originator() != null) {
if (ctype_digit($message->originator())) {
Expand All @@ -67,12 +99,6 @@ public function encode(\Esendex\Model\DispatchMessage $message)
if ($message->validityPeriod() > 72)
throw new ArgumentException("Validity too long, must be less or equal to than 72");

$doc = new \SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\"?><messages />", 0, false, Api::NS);
$doc->addAttribute("xmlns", Api::NS);
$doc->accountreference = $this->reference;
if ($message->characterSet() != null)
$doc->characterset = $message->characterSet();

$child = $doc->addChild("message");
if ($message->originator() != null)
$child->from = $message->originator();
Expand All @@ -86,7 +112,6 @@ public function encode(\Esendex\Model\DispatchMessage $message)
if ($message->retries() != null)
$child->retries = $message->retries();

return $doc->asXML();
}

public function parse($xml)
Expand Down
43 changes: 43 additions & 0 deletions test/Esendex/Services/DispatchServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,49 @@ function setUp()
$this->service = new DispatchService($this->authentication, $this->httpUtil, $this->parser);
}

/**
* @test
*/
function sendMultipleSuccess()
{
$messages = array(
new Model\DispatchMessage("DispatcherTest", "447712345678", "Message Body", Model\Message::SmsType)
);

$request = "xml request";
$response = "xml response";
$resultItem = new \Esendex\Model\ResultItem(
"1183C73D-2E62-4F60-B610-30F160BDFBD5",
"https://api.esendex.com/v1.0/MessageHeaders/1183C73D-2E62-4F60-B610-30F160BDFBD5"
);

$this->parser
->expects($this->once())
->method("encodeMultiple")
->with($this->equalTo($messages))
->will($this->returnValue($request));
$this->httpUtil
->expects($this->once())
->method("post")
->with(
$this->equalTo(
"https://api.esendex.com/v1.0/messagedispatcher"
),
$this->equalTo($this->authentication),
$this->equalTo($request)
)
->will($this->returnValue($response));
$this->parser
->expects($this->once())
->method("parse")
->with($this->equalTo($response))
->will($this->returnValue(array($resultItem)));

$result = $this->service->sendMultiple($messages);

$this->assertSame(array($resultItem), $result);
}

/**
* @test
*/
Expand Down

0 comments on commit 41b5bc9

Please sign in to comment.