Skip to content

Added MessageResponse DTO and added the ability to set response objects #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions src/MessageBird/Objects/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
*
Expand Down
141 changes: 141 additions & 0 deletions src/MessageBird/Objects/MessageResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace MessageBird\Objects;

/**
* Class Message
*
* @property int $protocolId
* @package MessageBird\Objects
*/
class MessageResponse extends Base
{
/**
* An unique random ID which is created on the MessageBird
* platform and is returned upon creation of the object.
*
* @var string
*/
public $id;

/**
* The URL of the created object.
*
* @var string
*/
public $href;

/**
* Tells you if the message is sent or received.
* mt: mobile terminated (sent to mobile)
* mo: mobile originated (received from mobile)
*
* @var string
*/
public $direction;

/**
* The type of message. Values can be: sms, binary, premium, or flash
*
* @var string
*/
public $type;

/**
* The sender of the message. This can be a telephone number
* (including country code) or an alphanumeric string. In case
* of an alphanumeric string, the maximum length is 11 characters.
*
* @var string
*/
public $originator;

/**
* The body of the SMS message.
*
* @var string
*/
public $body;

/**
* A client reference. Here you can put your own reference,
* like your internal reference.
*
* @var string
*/
public $reference;

/**
* The amount of seconds that the message is valid.
* If a message is not delivered within this time,
* the message will be discarded.
*
* @var int
*/
public $validity;

/**
* The SMS route that is used to send the message. This is for
* advanced users.
*
* @var int
*/
public $gateway;

/**
* An associative array with extra information. Is only used when a binary or premium
* message is sent.
*
* @var array
*/
public $typeDetails = [];

/**
* The datacoding used, can be plain or unicode
*
* @var string
*/
public $datacoding;

/**
* Indicates the message type. 1 is a normal message, 0 is a flash message.
*
* @var int
*/
public $mclass = 1;

/**
* The scheduled date and time of the message in RFC3339 format (Y-m-d\TH:i:sP)
*
* @var string|null
*/
public $scheduledDatetime;

/**
* The date and time of the creation of the message in RFC3339 format (Y-m-d\TH:i:sP)
* @var string
*/
public $createdDatetime;

/**
* An array of recipients
*
* @var Recipients
*/
public $recipients;

/**
* @param mixed $object
*
* @return $this
*/
public function loadFromArray($object)
{
parent::loadFromArray($object);

$this->recipients = (new Recipients())->loadFromArray($this->recipients);
$this->typeDetails = get_object_vars($this->typeDetails);
Copy link
Contributor Author

@JohnstonCode JohnstonCode Apr 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation returns $typeDetails as a stdClass when its type is set to array


return $this;
}
}
57 changes: 57 additions & 0 deletions src/MessageBird/Objects/Recipients.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace MessageBird\Objects;

/**
* Class Recipients
*
* @package MessageBird\Objects
*/
class Recipients extends Base
{
/**
* @var int
*/
public $totalCount;

/**
* @var int
*/
public $totalSentCount;

/**
* @var int
*/
public $totalDeliveredCount;

/**
* @var int
*/
public $totalDeliveryFailedCount;

/**
* @var Recipient[]
*/
public $items;

/**
* @param $object
*
* @return $this|void
*/
public function loadFromArray($object)
{
parent::loadFromArray($object);

if (!empty($this->items)) {
foreach ($this->items as &$item) {
$recipient = new Recipient();
$recipient->loadFromArray($item);

$item = $recipient;
}
}

return $this;
}
}
39 changes: 33 additions & 6 deletions src/MessageBird/Resources/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class Base
*/
protected $object;

/**
* @var Objects\MessageResponse
*/
protected $responseObject;

/**
* @param Common\HttpClient $httpClient
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/MessageBird/Resources/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading