diff --git a/src/MessageBird/Objects/Message.php b/src/MessageBird/Objects/Message.php index aa77a276..a13d65f0 100644 --- a/src/MessageBird/Objects/Message.php +++ b/src/MessageBird/Objects/Message.php @@ -17,21 +17,6 @@ class Message extends Base const DATACODING_UNICODE = 'unicode'; const DATACODING_PLAIN = 'plain'; - /** - * An unique random ID which is created on the MessageBird - * platform and is returned upon creation of the object. - * - * @var string - */ - protected $id; - - /** - * The URL of the created object. - * - * @var string - */ - protected $href; - /** * Tells you if the message is sent or received. * mt: mobile terminated (sent to mobile) @@ -192,26 +177,6 @@ public function setFlash($bool): void } } - /** - * Get the created id - * - * @return mixed - */ - public function getId() - { - return $this->id; - } - - /** - * Get the created href - * - * @return string - */ - public function getHref() - { - return $this->href; - } - /** * Get the $createdDatetime value * diff --git a/src/MessageBird/Objects/MessageResponse.php b/src/MessageBird/Objects/MessageResponse.php new file mode 100644 index 00000000..224a0307 --- /dev/null +++ b/src/MessageBird/Objects/MessageResponse.php @@ -0,0 +1,141 @@ +recipients = (new Recipients())->loadFromArray($this->recipients); + $this->typeDetails = get_object_vars($this->typeDetails); + + return $this; + } +} diff --git a/src/MessageBird/Objects/Recipients.php b/src/MessageBird/Objects/Recipients.php new file mode 100644 index 00000000..b786b1b5 --- /dev/null +++ b/src/MessageBird/Objects/Recipients.php @@ -0,0 +1,57 @@ +items)) { + foreach ($this->items as &$item) { + $recipient = new Recipient(); + $recipient->loadFromArray($item); + + $item = $recipient; + } + } + + return $this; + } +} diff --git a/src/MessageBird/Resources/Base.php b/src/MessageBird/Resources/Base.php index c50e52b1..4c2892c9 100644 --- a/src/MessageBird/Resources/Base.php +++ b/src/MessageBird/Resources/Base.php @@ -28,6 +28,11 @@ class Base */ protected $object; + /** + * @var Objects\MessageResponse + */ + protected $responseObject; + /** * @param Common\HttpClient $httpClient */ @@ -72,13 +77,31 @@ public function getObject() return $this->object; } + /** + * @param mixed $responseObject + * + * @return void + */ + public function setResponseObject($responseObject): void + { + $this->responseObject = $responseObject; + } + + /** + * @return Objects\MessageResponse + */ + public function getResponseObject() + { + return $this->responseObject; + } + /** * @no-named-arguments * * @param mixed $object * @param array|null $query * - * @return Objects\Balance|Objects\Conversation\Conversation|Objects\Hlr|Objects\Lookup|Objects\Message|Objects\Verify|Objects\VoiceMessage|null + * @return Objects\Balance|Objects\Conversation\Conversation|Objects\Hlr|Objects\Lookup|Objects\MessageResponse|Objects\Verify|Objects\VoiceMessage|null * * @throws Exceptions\HttpException * @throws Exceptions\RequestException @@ -164,7 +187,7 @@ public function delete($id) /** * @param string $body * - * @return Objects\Balance|Objects\Conversation\Conversation|Objects\Hlr|Objects\Lookup|Objects\Message|Objects\Verify|Objects\VoiceMessage|null + * @return Objects\Balance|Objects\Conversation\Conversation|Objects\Hlr|Objects\Lookup|Objects\Message|Objects\Verify|Objects\VoiceMessage|Objects\MessageResponse|null * * @throws \MessageBird\Exceptions\RequestException * @throws \MessageBird\Exceptions\ServerException @@ -177,12 +200,16 @@ public function processRequest($body) throw new Exceptions\ServerException('Got an invalid JSON response from the server.'); } - if (empty($body->errors)) { - return $this->object->loadFromArray($body); + if (!empty($body->errors)) { + $responseError = new Common\ResponseError($body); + throw new Exceptions\RequestException($responseError->getErrorString()); + } + + if ($this->responseObject) { + return $this->responseObject->loadFromArray($body); } - $responseError = new Common\ResponseError($body); - throw new Exceptions\RequestException($responseError->getErrorString()); + return $this->object->loadFromArray($body); } /** diff --git a/src/MessageBird/Resources/Messages.php b/src/MessageBird/Resources/Messages.php index e3c7e7b2..33840fea 100644 --- a/src/MessageBird/Resources/Messages.php +++ b/src/MessageBird/Resources/Messages.php @@ -19,6 +19,7 @@ class Messages extends Base public function __construct(Common\HttpClient $httpClient) { $this->setObject(new Objects\Message); + $this->setResponseObject(new Objects\MessageResponse); $this->setResourceName('messages'); parent::__construct($httpClient); diff --git a/tests/Integration/Messages/MessagesTest.php b/tests/Integration/Messages/MessagesTest.php index 234ae345..d98ca964 100644 --- a/tests/Integration/Messages/MessagesTest.php +++ b/tests/Integration/Messages/MessagesTest.php @@ -108,4 +108,71 @@ public function testDeleteMessage() $this->mockClient->expects($this->once())->method('performHttpRequest')->with("DELETE", 'messages/message_id', null, null); $this->client->messages->delete("message_id"); } + + public function testCreateMessageResponse() + { + $message = new \MessageBird\Objects\Message(); + $message->originator = 'MessageBird'; + $message->recipients = [31612345678]; + $message->body = 'This is a test message.'; + + $this->mockClient->expects($this->atLeastOnce())->method('performHttpRequest')->willReturn([200, '', '{ + "id":"e8077d803532c0b5937c639b60216938", + "href":"https://rest.messagebird.com/messages/e8077d803532c0b5937c639b60216938", + "direction":"mt", + "type":"sms", + "originator":"YourName", + "body":"This is a test message", + "reference":null, + "validity":null, + "gateway":null, + "typeDetails":{ + + }, + "datacoding":"plain", + "mclass":1, + "scheduledDatetime":null, + "createdDatetime":"2015-07-03T07:55:31+00:00", + "recipients":{ + "totalCount":1, + "totalSentCount":1, + "totalDeliveredCount":0, + "totalDeliveryFailedCount":0, + "items":[ + { + "recipient":31612345678, + "status":"sent", + "statusDatetime":"2015-07-03T07:55:31+00:00" + } + ] + }, + "reportUrl":null + }']); + + /** @var \MessageBird\Objects\MessageResponse $messageResponse */ + $messageResponse = $this->client->messages->create($message); + + $this->assertSame('e8077d803532c0b5937c639b60216938', $messageResponse->id); + $this->assertSame('https://rest.messagebird.com/messages/e8077d803532c0b5937c639b60216938', $messageResponse->href); + $this->assertSame('mt', $messageResponse->direction); + $this->assertSame('sms', $messageResponse->type); + $this->assertSame('YourName', $messageResponse->originator); + $this->assertSame('This is a test message', $messageResponse->body); + $this->assertNull($messageResponse->reference); + $this->assertNull($messageResponse->validity); + $this->assertNull($messageResponse->gateway); + $this->assertEmpty($messageResponse->typeDetails); + $this->assertSame('plain', $messageResponse->datacoding); + $this->assertSame(1, $messageResponse->mclass); + $this->assertNull($messageResponse->scheduledDatetime); + $this->assertSame('2015-07-03T07:55:31+00:00', $messageResponse->createdDatetime); + $this->assertSame(1, $messageResponse->recipients->totalCount); + $this->assertSame(1, $messageResponse->recipients->totalSentCount); + $this->assertSame(0, $messageResponse->recipients->totalDeliveredCount); + $this->assertSame(0, $messageResponse->recipients->totalDeliveryFailedCount); + $this->assertCount(1, $messageResponse->recipients->items); + $this->assertSame(31612345678, $messageResponse->recipients->items[0]->recipient); + $this->assertSame('sent', $messageResponse->recipients->items[0]->status); + $this->assertSame('2015-07-03T07:55:31+00:00', $messageResponse->recipients->items[0]->statusDatetime); + } }