diff --git a/src/Esendex/DispatchService.php b/src/Esendex/DispatchService.php index 7e69454..8a69bb9 100644 --- a/src/Esendex/DispatchService.php +++ b/src/Esendex/DispatchService.php @@ -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 * diff --git a/src/Esendex/Parser/DispatchXmlParser.php b/src/Esendex/Parser/DispatchXmlParser.php index ccda7ef..92c37dd 100644 --- a/src/Esendex/Parser/DispatchXmlParser.php +++ b/src/Esendex/Parser/DispatchXmlParser.php @@ -49,6 +49,38 @@ function __construct($accountReference) } public function encode(\Esendex\Model\DispatchMessage $message) + { + $doc = new \SimpleXMLElement("", 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("", 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())) { @@ -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("", 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(); @@ -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) diff --git a/test/Esendex/Services/DispatchServiceTest.php b/test/Esendex/Services/DispatchServiceTest.php index 7ba9f92..e9db3a2 100644 --- a/test/Esendex/Services/DispatchServiceTest.php +++ b/test/Esendex/Services/DispatchServiceTest.php @@ -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 */