Skip to content

Commit ed74031

Browse files
author
Alexandru Bucur
authored
Merge pull request #170 from JohnstonCode/message_response
Added MessageResponse DTO and added the ability to set response objects
2 parents 9bf98a1 + 0af8de6 commit ed74031

File tree

6 files changed

+299
-41
lines changed

6 files changed

+299
-41
lines changed

src/MessageBird/Objects/Message.php

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,6 @@ class Message extends Base
1717
const DATACODING_UNICODE = 'unicode';
1818
const DATACODING_PLAIN = 'plain';
1919

20-
/**
21-
* An unique random ID which is created on the MessageBird
22-
* platform and is returned upon creation of the object.
23-
*
24-
* @var string
25-
*/
26-
protected $id;
27-
28-
/**
29-
* The URL of the created object.
30-
*
31-
* @var string
32-
*/
33-
protected $href;
34-
3520
/**
3621
* Tells you if the message is sent or received.
3722
* mt: mobile terminated (sent to mobile)
@@ -192,26 +177,6 @@ public function setFlash($bool): void
192177
}
193178
}
194179

195-
/**
196-
* Get the created id
197-
*
198-
* @return mixed
199-
*/
200-
public function getId()
201-
{
202-
return $this->id;
203-
}
204-
205-
/**
206-
* Get the created href
207-
*
208-
* @return string
209-
*/
210-
public function getHref()
211-
{
212-
return $this->href;
213-
}
214-
215180
/**
216181
* Get the $createdDatetime value
217182
*
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
namespace MessageBird\Objects;
4+
5+
/**
6+
* Class Message
7+
*
8+
* @property int $protocolId
9+
* @package MessageBird\Objects
10+
*/
11+
class MessageResponse extends Base
12+
{
13+
/**
14+
* An unique random ID which is created on the MessageBird
15+
* platform and is returned upon creation of the object.
16+
*
17+
* @var string
18+
*/
19+
public $id;
20+
21+
/**
22+
* The URL of the created object.
23+
*
24+
* @var string
25+
*/
26+
public $href;
27+
28+
/**
29+
* Tells you if the message is sent or received.
30+
* mt: mobile terminated (sent to mobile)
31+
* mo: mobile originated (received from mobile)
32+
*
33+
* @var string
34+
*/
35+
public $direction;
36+
37+
/**
38+
* The type of message. Values can be: sms, binary, premium, or flash
39+
*
40+
* @var string
41+
*/
42+
public $type;
43+
44+
/**
45+
* The sender of the message. This can be a telephone number
46+
* (including country code) or an alphanumeric string. In case
47+
* of an alphanumeric string, the maximum length is 11 characters.
48+
*
49+
* @var string
50+
*/
51+
public $originator;
52+
53+
/**
54+
* The body of the SMS message.
55+
*
56+
* @var string
57+
*/
58+
public $body;
59+
60+
/**
61+
* A client reference. Here you can put your own reference,
62+
* like your internal reference.
63+
*
64+
* @var string
65+
*/
66+
public $reference;
67+
68+
/**
69+
* The amount of seconds that the message is valid.
70+
* If a message is not delivered within this time,
71+
* the message will be discarded.
72+
*
73+
* @var int
74+
*/
75+
public $validity;
76+
77+
/**
78+
* The SMS route that is used to send the message. This is for
79+
* advanced users.
80+
*
81+
* @var int
82+
*/
83+
public $gateway;
84+
85+
/**
86+
* An associative array with extra information. Is only used when a binary or premium
87+
* message is sent.
88+
*
89+
* @var array
90+
*/
91+
public $typeDetails = [];
92+
93+
/**
94+
* The datacoding used, can be plain or unicode
95+
*
96+
* @var string
97+
*/
98+
public $datacoding;
99+
100+
/**
101+
* Indicates the message type. 1 is a normal message, 0 is a flash message.
102+
*
103+
* @var int
104+
*/
105+
public $mclass = 1;
106+
107+
/**
108+
* The scheduled date and time of the message in RFC3339 format (Y-m-d\TH:i:sP)
109+
*
110+
* @var string|null
111+
*/
112+
public $scheduledDatetime;
113+
114+
/**
115+
* The date and time of the creation of the message in RFC3339 format (Y-m-d\TH:i:sP)
116+
* @var string
117+
*/
118+
public $createdDatetime;
119+
120+
/**
121+
* An array of recipients
122+
*
123+
* @var Recipients
124+
*/
125+
public $recipients;
126+
127+
/**
128+
* @param mixed $object
129+
*
130+
* @return $this
131+
*/
132+
public function loadFromArray($object)
133+
{
134+
parent::loadFromArray($object);
135+
136+
$this->recipients = (new Recipients())->loadFromArray($this->recipients);
137+
$this->typeDetails = get_object_vars($this->typeDetails);
138+
139+
return $this;
140+
}
141+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace MessageBird\Objects;
4+
5+
/**
6+
* Class Recipients
7+
*
8+
* @package MessageBird\Objects
9+
*/
10+
class Recipients extends Base
11+
{
12+
/**
13+
* @var int
14+
*/
15+
public $totalCount;
16+
17+
/**
18+
* @var int
19+
*/
20+
public $totalSentCount;
21+
22+
/**
23+
* @var int
24+
*/
25+
public $totalDeliveredCount;
26+
27+
/**
28+
* @var int
29+
*/
30+
public $totalDeliveryFailedCount;
31+
32+
/**
33+
* @var Recipient[]
34+
*/
35+
public $items;
36+
37+
/**
38+
* @param $object
39+
*
40+
* @return $this|void
41+
*/
42+
public function loadFromArray($object)
43+
{
44+
parent::loadFromArray($object);
45+
46+
if (!empty($this->items)) {
47+
foreach ($this->items as &$item) {
48+
$recipient = new Recipient();
49+
$recipient->loadFromArray($item);
50+
51+
$item = $recipient;
52+
}
53+
}
54+
55+
return $this;
56+
}
57+
}

src/MessageBird/Resources/Base.php

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class Base
2828
*/
2929
protected $object;
3030

31+
/**
32+
* @var Objects\MessageResponse
33+
*/
34+
protected $responseObject;
35+
3136
/**
3237
* @param Common\HttpClient $httpClient
3338
*/
@@ -72,13 +77,31 @@ public function getObject()
7277
return $this->object;
7378
}
7479

80+
/**
81+
* @param mixed $responseObject
82+
*
83+
* @return void
84+
*/
85+
public function setResponseObject($responseObject): void
86+
{
87+
$this->responseObject = $responseObject;
88+
}
89+
90+
/**
91+
* @return Objects\MessageResponse
92+
*/
93+
public function getResponseObject()
94+
{
95+
return $this->responseObject;
96+
}
97+
7598
/**
7699
* @no-named-arguments
77100
*
78101
* @param mixed $object
79102
* @param array|null $query
80103
*
81-
* @return Objects\Balance|Objects\Conversation\Conversation|Objects\Hlr|Objects\Lookup|Objects\Message|Objects\Verify|Objects\VoiceMessage|null
104+
* @return Objects\Balance|Objects\Conversation\Conversation|Objects\Hlr|Objects\Lookup|Objects\MessageResponse|Objects\Verify|Objects\VoiceMessage|null
82105
*
83106
* @throws Exceptions\HttpException
84107
* @throws Exceptions\RequestException
@@ -164,7 +187,7 @@ public function delete($id)
164187
/**
165188
* @param string $body
166189
*
167-
* @return Objects\Balance|Objects\Conversation\Conversation|Objects\Hlr|Objects\Lookup|Objects\Message|Objects\Verify|Objects\VoiceMessage|null
190+
* @return Objects\Balance|Objects\Conversation\Conversation|Objects\Hlr|Objects\Lookup|Objects\Message|Objects\Verify|Objects\VoiceMessage|Objects\MessageResponse|null
168191
*
169192
* @throws \MessageBird\Exceptions\RequestException
170193
* @throws \MessageBird\Exceptions\ServerException
@@ -177,12 +200,16 @@ public function processRequest($body)
177200
throw new Exceptions\ServerException('Got an invalid JSON response from the server.');
178201
}
179202

180-
if (empty($body->errors)) {
181-
return $this->object->loadFromArray($body);
203+
if (!empty($body->errors)) {
204+
$responseError = new Common\ResponseError($body);
205+
throw new Exceptions\RequestException($responseError->getErrorString());
206+
}
207+
208+
if ($this->responseObject) {
209+
return $this->responseObject->loadFromArray($body);
182210
}
183211

184-
$responseError = new Common\ResponseError($body);
185-
throw new Exceptions\RequestException($responseError->getErrorString());
212+
return $this->object->loadFromArray($body);
186213
}
187214

188215
/**

src/MessageBird/Resources/Messages.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Messages extends Base
1919
public function __construct(Common\HttpClient $httpClient)
2020
{
2121
$this->setObject(new Objects\Message);
22+
$this->setResponseObject(new Objects\MessageResponse);
2223
$this->setResourceName('messages');
2324

2425
parent::__construct($httpClient);

0 commit comments

Comments
 (0)