|
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
|
8 |
|
public function __construct($appKey, $secretKey) |
|
63
|
|
|
{ |
|
64
|
8 |
|
$this->client = new HttpClient($appKey, $secretKey); |
|
65
|
8 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return HttpClient |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function getClient() |
|
71
|
|
|
{ |
|
72
|
1 |
|
return $this->client; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param HttpClient $client |
|
77
|
|
|
*/ |
|
78
|
2 |
|
public function setClient($client) |
|
79
|
|
|
{ |
|
80
|
2 |
|
$this->client = $client; |
|
81
|
2 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return boolean |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function isFlashMessage() |
|
87
|
|
|
{ |
|
88
|
1 |
|
return $this->isFlashMessage; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param boolean $isFlashMessage |
|
93
|
|
|
* @return SmslabsClient $this |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public function setFlashMessage($isFlashMessage) |
|
96
|
|
|
{ |
|
97
|
1 |
|
$this->isFlashMessage = (bool)$isFlashMessage; |
|
98
|
|
|
|
|
99
|
1 |
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function getSenderId() |
|
106
|
|
|
{ |
|
107
|
1 |
|
return $this->senderId; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param string $senderId |
|
112
|
|
|
* @return SmslabsClient $this |
|
113
|
|
|
*/ |
|
114
|
6 |
|
public function setSenderId($senderId) |
|
115
|
|
|
{ |
|
116
|
6 |
|
$this->senderId = $senderId; |
|
117
|
|
|
|
|
118
|
6 |
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return int |
|
123
|
|
|
*/ |
|
124
|
1 |
|
public function getExpirationMinutes() |
|
125
|
|
|
{ |
|
126
|
1 |
|
return $this->expirationMinutes; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param int $expirationMinutes |
|
131
|
|
|
* @return SmslabsClient $this |
|
132
|
|
|
*/ |
|
133
|
2 |
|
public function setExpirationMinutes($expirationMinutes) |
|
134
|
|
|
{ |
|
135
|
2 |
|
if ($expirationMinutes < 1 || $expirationMinutes > 5520) { |
|
136
|
1 |
|
throw new \InvalidArgumentException('Valid values: 1 - 5520'); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
1 |
|
$this->expirationMinutes = (int)$expirationMinutes; |
|
140
|
|
|
|
|
141
|
1 |
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return \DateTime |
|
146
|
|
|
*/ |
|
147
|
1 |
|
public function getSendDateTime() |
|
148
|
|
|
{ |
|
149
|
1 |
|
return $this->sendDateTime; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param \DateTime $sendDateTime |
|
154
|
|
|
* @return SmslabsClient $this |
|
155
|
|
|
*/ |
|
156
|
1 |
|
public function setSendDateTime(\DateTime $sendDateTime) |
|
157
|
|
|
{ |
|
158
|
1 |
|
$this->sendDateTime = $sendDateTime; |
|
159
|
|
|
|
|
160
|
1 |
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param string $phoneNumber |
|
165
|
|
|
* @param string $message |
|
166
|
|
|
* @param bool $isFlashMessage |
|
167
|
|
|
* @param int $expirationMinutes |
|
168
|
|
|
* @param \DateTime $sendDateTime |
|
169
|
|
|
* @return SmslabsClient $this |
|
170
|
|
|
*/ |
|
171
|
6 |
|
public function add( |
|
172
|
|
|
$phoneNumber, |
|
173
|
|
|
$message, |
|
174
|
|
|
$isFlashMessage = null, |
|
175
|
|
|
$expirationMinutes = null, |
|
176
|
|
|
\DateTime $sendDateTime = null |
|
177
|
|
|
) { |
|
178
|
6 |
|
if ($this->checkPhoneNumber($phoneNumber) === false) { |
|
179
|
1 |
|
throw new \InvalidArgumentException('Invalid phone number'); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
$sms = [ |
|
183
|
5 |
|
'phone_number' => $phoneNumber, |
|
184
|
5 |
|
'message' => $message, |
|
185
|
5 |
|
'flash' => $isFlashMessage === null ? (int)$this->isFlashMessage : (int)$isFlashMessage, |
|
186
|
5 |
|
'expiration' => $expirationMinutes === null ? (int)$this->expirationMinutes : (int)$expirationMinutes, |
|
187
|
5 |
|
'sender_id' => $this->senderId, |
|
188
|
5 |
|
]; |
|
189
|
|
|
|
|
190
|
5 |
|
if ($sendDateTime instanceof \DateTime) { |
|
191
|
|
|
$sms['send_date'] = (int)$sendDateTime->getTimestamp(); |
|
192
|
5 |
|
} elseif ($this->sendDateTime instanceof \DateTime) { |
|
193
|
1 |
|
$sms['send_date'] = (int)$this->sendDateTime->getTimestamp(); |
|
194
|
1 |
|
} |
|
195
|
|
|
|
|
196
|
5 |
|
$this->smsToSend[] = $sms; |
|
197
|
|
|
|
|
198
|
5 |
|
return $this; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @param string $phoneNumber |
|
203
|
|
|
* @return bool |
|
204
|
|
|
*/ |
|
205
|
6 |
|
private function checkPhoneNumber($phoneNumber) |
|
206
|
|
|
{ |
|
207
|
6 |
|
return (bool)preg_match('/^\+[0-9]{10,13}$/', $phoneNumber); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @return SmslabsClient $this |
|
212
|
|
|
*/ |
|
213
|
3 |
|
public function send() |
|
214
|
|
|
{ |
|
215
|
3 |
|
if (empty($this->smsToSend)) { |
|
216
|
1 |
|
throw new EmptySMSQueueException('No messages to send'); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
2 |
|
if ($this->senderId === null) { |
|
220
|
1 |
|
throw new \InvalidArgumentException('SenderId is missing'); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
1 |
|
foreach ($this->smsToSend as $key => $sms) { |
|
224
|
1 |
|
$httpResponse = $this->client->sendRequest(self::SEND_SMS_URL, $sms, 'PUT'); |
|
225
|
|
|
|
|
226
|
1 |
|
$this->smsStatus[] = new SmsSentResponse($httpResponse['account'], $httpResponse['sms_id']); |
|
227
|
|
|
|
|
228
|
1 |
|
unset($this->smsToSend[$key]); |
|
229
|
1 |
|
} |
|
230
|
|
|
|
|
231
|
1 |
|
return $this; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @return SmsSentResponse[] |
|
236
|
|
|
*/ |
|
237
|
1 |
|
public function getSentStatus() |
|
238
|
|
|
{ |
|
239
|
1 |
|
return $this->smsStatus; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @return Sender[] |
|
244
|
|
|
*/ |
|
245
|
|
|
public function getAvailableSenders() |
|
246
|
|
|
{ |
|
247
|
|
|
$sendersResponse = $this->client->sendRequest(self::SENDERS_URL); |
|
248
|
|
|
|
|
249
|
|
|
$list = []; |
|
250
|
|
|
|
|
251
|
|
|
foreach ($sendersResponse as $sender) { |
|
252
|
|
|
$list[] = Sender::createFromResponseArray($sender); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
return $list; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* @return AccountBalance |
|
260
|
|
|
*/ |
|
261
|
|
|
public function getAccountBalance() |
|
262
|
|
|
{ |
|
263
|
|
|
$balanceResponse = $this->client->sendRequest(self::ACCOUNT_URL); |
|
264
|
|
|
|
|
265
|
|
|
return new AccountBalance($balanceResponse['account'] / 100); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @return SmsIn[] |
|
270
|
|
|
*/ |
|
271
|
|
|
public function getSmsIn() |
|
272
|
|
|
{ |
|
273
|
|
|
$smsInResponse = $this->client->sendRequest(self::SMS_IN_URL); |
|
274
|
|
|
|
|
275
|
|
|
$list = []; |
|
276
|
|
|
|
|
277
|
|
|
foreach ($smsInResponse as $smsIn) { |
|
278
|
|
|
$list[] = SmsIn::createFromResponseArray($smsIn); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
return $list; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* @param int $offset |
|
286
|
|
|
* @param int $limit |
|
287
|
|
|
* @return SmsOut[] |
|
288
|
|
|
*/ |
|
289
|
|
|
public function getSmsOut($offset = 0, $limit = 100) |
|
290
|
|
|
{ |
|
291
|
|
|
$smsOutResponse = $this->client->sendRequest(self::SMS_LIST_URL.'?offset='.$offset.'&limit='.$limit); |
|
292
|
|
|
|
|
293
|
|
|
$list = []; |
|
294
|
|
|
|
|
295
|
|
|
foreach ($smsOutResponse as $smsOut) { |
|
296
|
|
|
$list[] = SmsOut::createFromResponseArray($smsOut); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
return $list; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* @param string $smsId |
|
304
|
|
|
* @return SmsDetails |
|
305
|
|
|
*/ |
|
306
|
|
|
public function getSmsDetails($smsId) |
|
307
|
|
|
{ |
|
308
|
|
|
if ($this->senderId === null) { |
|
309
|
|
|
throw new \InvalidArgumentException('Sender ID'); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
$detailsResponse = $this->client->sendRequest(self::SMS_STATUS_URL.'?id='.$smsId); |
|
313
|
|
|
|
|
314
|
|
|
$smsDetails = SmsDetails::createFromResponseArray($detailsResponse); |
|
315
|
|
|
|
|
316
|
|
|
return $smsDetails; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* @return array[] |
|
321
|
|
|
*/ |
|
322
|
3 |
|
public function getSmsQueue() |
|
323
|
|
|
{ |
|
324
|
3 |
|
return $this->smsToSend; |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
|