1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ittoolspl\Smslabs; |
4
|
|
|
|
5
|
|
|
use Ittoolspl\Smslabs\Entity\AccountBalance; |
6
|
|
|
use Ittoolspl\Smslabs\Entity\Sender; |
7
|
|
|
use Ittoolspl\Smslabs\Entity\SmsDetails; |
8
|
|
|
use Ittoolspl\Smslabs\Entity\SmsIn; |
9
|
|
|
use Ittoolspl\Smslabs\Entity\SmsOut; |
10
|
|
|
use Ittoolspl\Smslabs\Entity\SmsSentResponse; |
11
|
|
|
use Ittoolspl\Smslabs\Exception\EmptySMSQueueException; |
12
|
|
|
|
13
|
|
|
class SmslabsClient |
14
|
|
|
{ |
15
|
|
|
const SEND_SMS_URL = '/sendSms'; |
16
|
|
|
const SENDERS_URL = '/senders'; |
17
|
|
|
const SMS_STATUS_URL = '/smsStatus'; |
18
|
|
|
const SMS_LIST_URL = '/sms'; |
19
|
|
|
const SMS_IN_URL = '/smsIn'; |
20
|
|
|
const ACCOUNT_URL = '/account'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var HttpClient |
24
|
|
|
*/ |
25
|
|
|
private $client; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
private $isFlashMessage = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $senderId; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
private $expirationMinutes = 0; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \DateTime |
44
|
|
|
*/ |
45
|
|
|
private $sendDateTime; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array[] |
49
|
|
|
*/ |
50
|
|
|
private $smsToSend = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var SmsSentResponse[] |
54
|
|
|
*/ |
55
|
|
|
private $smsStatus = []; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Smslabs constructor. |
59
|
|
|
* @param string $appKey |
60
|
|
|
* @param string $secretKey |
61
|
|
|
*/ |
62
|
|
|
public function __construct($appKey, $secretKey) |
63
|
|
|
{ |
64
|
|
|
$this->client = new HttpClient($appKey, $secretKey); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param boolean $isFlashMessage |
69
|
|
|
* @return SmslabsClient $this |
70
|
|
|
*/ |
71
|
|
|
public function setIsFlashMessage($isFlashMessage) |
72
|
|
|
{ |
73
|
|
|
$this->isFlashMessage = (bool)$isFlashMessage; |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $senderId |
80
|
|
|
* @return SmslabsClient $this |
81
|
|
|
*/ |
82
|
|
|
public function setSenderId($senderId) |
83
|
|
|
{ |
84
|
|
|
$this->senderId = $senderId; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param int $expirationMinutes |
91
|
|
|
* @return SmslabsClient $this |
92
|
|
|
*/ |
93
|
|
|
public function setExpiration($expirationMinutes) |
94
|
|
|
{ |
95
|
|
|
if ($expirationMinutes < 1 || $expirationMinutes > 5520) { |
96
|
|
|
throw new \InvalidArgumentException('Valid values: 1 - 5520'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->expirationMinutes = (int)$expirationMinutes; |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param \DateTime $sendDateTime |
106
|
|
|
* @return SmslabsClient $this |
107
|
|
|
*/ |
108
|
|
|
public function setSendDate(\DateTime $sendDateTime) |
109
|
|
|
{ |
110
|
|
|
$this->sendDateTime = $sendDateTime; |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $phoneNumber |
117
|
|
|
* @param string $message |
118
|
|
|
* @param bool $isFlashMessage |
119
|
|
|
* @param int $expirationMinutes |
120
|
|
|
* @param \DateTime $sendDateTime |
121
|
|
|
* @return SmslabsClient $this |
122
|
|
|
*/ |
123
|
|
|
public function add( |
124
|
|
|
$phoneNumber, |
125
|
|
|
$message, |
126
|
|
|
$isFlashMessage = null, |
127
|
|
|
$expirationMinutes = null, |
128
|
|
|
\DateTime $sendDateTime = null |
129
|
|
|
) { |
130
|
|
|
if ($this->checkPhoneNumber($phoneNumber) === false) { |
131
|
|
|
throw new \InvalidArgumentException('Invalid phone number'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$sms = [ |
135
|
|
|
'phone_number' => $phoneNumber, |
136
|
|
|
'message' => $message, |
137
|
|
|
'flash' => $isFlashMessage === null ? (int)$this->isFlashMessage : (int)$isFlashMessage, |
138
|
|
|
'expiration' => $expirationMinutes === null ? (int)$this->expirationMinutes : (int)$expirationMinutes, |
139
|
|
|
'sender_id' => $this->senderId, |
140
|
|
|
]; |
141
|
|
|
|
142
|
|
|
if ($sendDateTime instanceof \DateTime) { |
143
|
|
|
$sms['send_date'] = (int)$sendDateTime->getTimestamp(); |
144
|
|
|
} elseif ($this->sendDateTime instanceof \DateTime) { |
145
|
|
|
$sms['send_date'] = (int)$this->sendDateTime->getTimestamp(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$this->smsToSend[] = $sms; |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $phoneNumber |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
private function checkPhoneNumber($phoneNumber) |
158
|
|
|
{ |
159
|
|
|
return (bool)preg_match('/^\+[0-9]{10,13}$/', $phoneNumber); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return SmslabsClient $this |
164
|
|
|
*/ |
165
|
|
|
public function send() |
166
|
|
|
{ |
167
|
|
|
if (empty($this->smsToSend)) { |
168
|
|
|
throw new EmptySMSQueueException('No messages to send'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if ($this->senderId === null) { |
172
|
|
|
throw new \InvalidArgumentException('SenderId is missing'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
foreach ($this->smsToSend as $key => $sms) { |
176
|
|
|
$httpResponse = $this->client->sendRequest(self::SEND_SMS_URL, $sms, 'PUT'); |
177
|
|
|
|
178
|
|
|
$this->smsStatus[] = new SmsSentResponse($httpResponse->account, $httpResponse->sms_id); |
179
|
|
|
|
180
|
|
|
unset($this->smsToSend[$key]); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return SmsSentResponse[] |
188
|
|
|
*/ |
189
|
|
|
public function getSentStatus() |
190
|
|
|
{ |
191
|
|
|
return $this->smsStatus; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return Sender[] |
196
|
|
|
*/ |
197
|
|
|
public function getAvailableSenders() |
198
|
|
|
{ |
199
|
|
|
$sendersResponse = $this->client->sendRequest(self::SENDERS_URL); |
200
|
|
|
|
201
|
|
|
$list = []; |
202
|
|
|
|
203
|
|
|
foreach ($sendersResponse as $sender) { |
|
|
|
|
204
|
|
|
$list[] = Sender::createFromResponseObject($sender); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $list; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return AccountBalance |
212
|
|
|
*/ |
213
|
|
|
public function getAccountBalance() |
214
|
|
|
{ |
215
|
|
|
$balanceResponse = $this->client->sendRequest(self::ACCOUNT_URL); |
216
|
|
|
|
217
|
|
|
return new AccountBalance($balanceResponse->account / 100); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return SmsIn[] |
222
|
|
|
*/ |
223
|
|
|
public function getSmsIn() |
224
|
|
|
{ |
225
|
|
|
$smsInResponse = $this->client->sendRequest(self::SMS_IN_URL); |
226
|
|
|
|
227
|
|
|
$list = []; |
228
|
|
|
|
229
|
|
|
foreach ($smsInResponse as $smsIn) { |
|
|
|
|
230
|
|
|
$list[] = SmsIn::createFromResponseObject($smsIn); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $list; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param int $offset |
238
|
|
|
* @param int $limit |
239
|
|
|
* @return SmsOut[] |
240
|
|
|
*/ |
241
|
|
|
public function getSmsOut($offset = 0, $limit = 100) |
242
|
|
|
{ |
243
|
|
|
$smsOutResponse = $this->client->sendRequest(self::SMS_LIST_URL.'?offset='.$offset.'&limit='.$limit); |
244
|
|
|
|
245
|
|
|
$list = []; |
246
|
|
|
|
247
|
|
|
foreach ($smsOutResponse as $smsOut) { |
|
|
|
|
248
|
|
|
$list[] = SmsOut::createFromResponseObject($smsOut); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return $list; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param string $smsId |
256
|
|
|
* @return SmsDetails |
257
|
|
|
*/ |
258
|
|
|
public function getSmsDetails($smsId) |
259
|
|
|
{ |
260
|
|
|
if ($this->senderId === null) { |
261
|
|
|
throw new \InvalidArgumentException('Sender ID'); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
$detailsResponse = $this->client->sendRequest(self::SMS_STATUS_URL.'?id='.$smsId); |
265
|
|
|
|
266
|
|
|
$smsDetails = SmsDetails::createFromResponseObject($detailsResponse); |
267
|
|
|
|
268
|
|
|
return $smsDetails; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @return array[] |
273
|
|
|
*/ |
274
|
|
|
public function getSmsQueue() |
275
|
|
|
{ |
276
|
|
|
return $this->smsToSend; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|