1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* BongaTech SMS Client Library for PHP. |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2017 |
6
|
|
|
* @author Vincent Mosoti <[email protected]> |
7
|
|
|
* @license https://github.com/VMosoti/bongatech-sms/blob/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace VMosoti\BongaTech; |
11
|
|
|
|
12
|
|
|
use VMosoti\BongaTech\Exceptions\BongaTechException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class SMS. |
16
|
|
|
*/ |
17
|
|
|
class SMS |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Version number of the SMS API. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
const VERSION = '1.0.0'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* token is generated by md5(password). |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $token; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* timestamp is the current datetime. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $timestamp; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* type of SMS to sent. |
42
|
|
|
* |
43
|
|
|
* @var int message |
44
|
|
|
*/ |
45
|
|
|
protected $message_type = 0; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* batch type of the SMS being sent. |
49
|
|
|
* |
50
|
|
|
* @var int batch |
51
|
|
|
*/ |
52
|
|
|
protected $batch_type; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* sms configurations. |
56
|
|
|
* |
57
|
|
|
* @array config |
58
|
|
|
*/ |
59
|
|
|
protected $config; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* end point url. |
63
|
|
|
* |
64
|
|
|
* @string endpoint |
65
|
|
|
*/ |
66
|
|
|
protected $endpoint; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* the message(s) being sent (array of messages in case message is different for each user. |
70
|
|
|
* |
71
|
|
|
* @var array. |
72
|
|
|
*/ |
73
|
|
|
protected $message; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* the recipients . |
77
|
|
|
* |
78
|
|
|
* @var array. |
79
|
|
|
*/ |
80
|
|
|
protected $recipients; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* SMS constructor. |
84
|
|
|
*/ |
85
|
|
|
public function __construct() |
86
|
|
|
{ |
87
|
|
|
$this->config = Config::get(); |
88
|
|
|
$this->setTimestamp(); |
89
|
|
|
$this->setToken(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* set the timestamp. |
94
|
|
|
*/ |
95
|
|
|
private function setTimestamp() |
96
|
|
|
{ |
97
|
|
|
$this->timestamp = date('YmdHis'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* set the token. |
102
|
|
|
*/ |
103
|
|
|
private function setToken() |
104
|
|
|
{ |
105
|
|
|
$this->token = md5($this->config['password']); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* invoke if SMS being sent is of type subscribable. |
110
|
|
|
* |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
|
|
public function messageTypeSubscribable() |
114
|
|
|
{ |
115
|
|
|
$this->message_type = MessageType::SUBSCIBABLE; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* invoke if SMS being sent is of type on demand. |
122
|
|
|
* |
123
|
|
|
* @return $this |
124
|
|
|
*/ |
125
|
|
|
public function messageTypeOnDemand() |
126
|
|
|
{ |
127
|
|
|
$this->message_type = MessageType::ON_DEMAND; |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* invoke if SMS being sent is of type bulk SMS. This will be the common one. |
134
|
|
|
* |
135
|
|
|
* @return $this |
136
|
|
|
*/ |
137
|
|
|
public function messageTypeBulk() |
138
|
|
|
{ |
139
|
|
|
$this->message_type = MessageType::BULK; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* invoke if SMS is being sent to a single recipient. |
146
|
|
|
* |
147
|
|
|
* @return $this |
148
|
|
|
*/ |
149
|
|
|
public function batchTypeNoBatch() |
150
|
|
|
{ |
151
|
|
|
$this->batch_type = BatchType::NOT_BATCH; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* invoke if SMS is being sent to a different recipients. |
158
|
|
|
* |
159
|
|
|
* @return $this |
160
|
|
|
*/ |
161
|
|
|
public function batchTypeSameMessage() |
162
|
|
|
{ |
163
|
|
|
$this->batch_type = BatchType::SAME_MESSAGE; |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* invoke if each recipient will receive a different message. |
170
|
|
|
* |
171
|
|
|
* @return $this |
172
|
|
|
*/ |
173
|
|
|
public function batchTypeDifferentMessages() |
174
|
|
|
{ |
175
|
|
|
$this->batch_type = BatchType::DIFFERENT_MESSAGE; |
176
|
|
|
|
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param $recipients |
182
|
|
|
* @param $message |
183
|
|
|
* |
184
|
|
|
* @throws BongaTechException |
185
|
|
|
* |
186
|
|
|
* @return mixed |
187
|
|
|
*/ |
188
|
|
|
public function send($recipients, $message) |
189
|
|
|
{ |
190
|
|
|
if ($this->message_type < 1) { |
191
|
|
|
throw new BongaTechException('The Message Type has not been set.'); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$this->recipients = $recipients; |
195
|
|
|
$this->message = $message; |
196
|
|
|
$this->endpoint = $this->config['base_url'].$this->config['sms_endpoint']; |
197
|
|
|
|
198
|
|
|
if ($this->batch_type === BatchType::NOT_BATCH) { |
199
|
|
|
|
200
|
|
View Code Duplication |
if (is_array($this->message) && array_depth($this->message) == 2 && count($this->message) == 1) { |
201
|
|
|
|
202
|
|
|
if (is_array($this->recipients) && array_depth($this->recipients) == 2 && count($this->recipients) == 1) { |
203
|
|
|
|
204
|
|
|
$response = $this->sendForNonBatch($this->buildSendObject($this->recipients, $this->message)); |
205
|
|
|
|
206
|
|
|
} else { |
207
|
|
|
throw new BongaTechException('The recipient MUST be an array of depth 2 and count should not be more than 1'); |
208
|
|
|
} |
209
|
|
|
} else { |
210
|
|
|
|
211
|
|
|
throw new BongaTechException('Message should be provided as an array whose depth is 2 and count should equal 1'); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
|
215
|
|
View Code Duplication |
} elseif ($this->batch_type === BatchType::SAME_MESSAGE) { |
216
|
|
|
|
217
|
|
|
if (is_array($this->message) && array_depth($this->message) == 2 && count($this->message) == 1) { |
218
|
|
|
|
219
|
|
|
if (is_array($this->recipients) && array_depth($this->recipients) == 2 && count($this->recipients) > 1) { |
220
|
|
|
|
221
|
|
|
$response = $this->sendForBatch($this->buildSendObject($this->recipients, $this->message)); |
222
|
|
|
|
223
|
|
|
} else { |
224
|
|
|
|
225
|
|
|
throw new BongaTechException('The recipients MUST be an array of depth 2 and count should be more than 1'); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
|
229
|
|
|
} else { |
230
|
|
|
|
231
|
|
|
throw new BongaTechException('Message should be provided as an array whose depth and count should equal 1'); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
} elseif ($this->batch_type === BatchType::DIFFERENT_MESSAGE) { |
235
|
|
|
|
236
|
|
|
if (count($this->recipients) == count($this->message)) { |
237
|
|
|
|
238
|
|
|
if (is_array($this->message) && array_depth($this->message) == 2) { |
239
|
|
|
|
240
|
|
|
if (is_array($this->recipients) && array_depth($this->recipients) == 2) { |
241
|
|
|
|
242
|
|
|
$response = $this->sendForBatch($this->buildSendObject($this->recipients, $this->message)); |
243
|
|
|
|
244
|
|
|
} else { |
245
|
|
|
|
246
|
|
|
throw new BongaTechException('The recipients MUST be an array of depth 2'); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
} else { |
250
|
|
|
|
251
|
|
|
throw new BongaTechException('Message MUST be an array of depth 2'); |
252
|
|
|
} |
253
|
|
|
} else { |
254
|
|
|
|
255
|
|
|
throw new BongaTechException('No. of Messages MUST be equal to number of Recipients.'); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
} else { |
259
|
|
|
|
260
|
|
|
throw new BongaTechException('Message Batch Type has not been set.'); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return $response; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* build the send object. |
268
|
|
|
* |
269
|
|
|
* @param recipients |
270
|
|
|
* @param $messages |
271
|
|
|
* |
272
|
|
|
* @return array |
273
|
|
|
*/ |
274
|
|
|
private function buildSendObject($recipients, $messages) |
275
|
|
|
{ |
276
|
|
|
$body = [ |
277
|
|
|
'AuthDetails' => [ |
278
|
|
|
[ |
279
|
|
|
'UserID' => $this->config['user_id'], |
280
|
|
|
'Token' => $this->token, |
281
|
|
|
'Timestamp' => $this->timestamp |
282
|
|
|
|
283
|
|
|
], |
284
|
|
|
], |
285
|
|
|
'MessageType' => [ |
286
|
|
|
(string)$this->message_type, |
287
|
|
|
], |
288
|
|
|
'BatchType' => [ |
289
|
|
|
(string)$this->batch_type, |
290
|
|
|
], |
291
|
|
|
'SourceAddr' => [ |
292
|
|
|
(string)$this->config['sender_id'], |
293
|
|
|
], |
294
|
|
|
'MessagePayload' => $messages, |
295
|
|
|
'DestinationAddr' => $recipients, |
296
|
|
|
'DeliveryRequest' => [ |
297
|
|
|
[ |
298
|
|
|
'EndPoint' => $this->config['callback_url'], |
299
|
|
|
'Correlator' => mt_rand(), |
300
|
|
|
] |
301
|
|
|
] |
302
|
|
|
]; |
303
|
|
|
|
304
|
|
|
return $body; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* send a message to a single recipient. |
309
|
|
|
* |
310
|
|
|
* @param $body |
311
|
|
|
* |
312
|
|
|
* @return Response |
313
|
|
|
*/ |
314
|
|
|
private function sendForNonBatch($body) |
315
|
|
|
{ |
316
|
|
|
$request = new Request($this->endpoint, $body); |
317
|
|
|
$response = $request->sendSMS(); |
318
|
|
|
|
319
|
|
|
return new Response($response->body[0]); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* send batch. 1) same message to many recipients 2) different messages to many recipients. |
324
|
|
|
* |
325
|
|
|
* @param $body |
326
|
|
|
* |
327
|
|
|
* @return array |
328
|
|
|
*/ |
329
|
|
|
private function sendForBatch($body) |
330
|
|
|
{ |
331
|
|
|
$request = new Request($this->endpoint, $body); |
332
|
|
|
$response = $request->sendSMS(); |
333
|
|
|
|
334
|
|
|
$responses = array(); |
335
|
|
|
$response_count = count($response->body); |
336
|
|
|
|
337
|
|
|
for ($i = 0; $i < $response_count; $i++) { |
338
|
|
|
|
339
|
|
|
$res = new Response($response->body[$i]); |
340
|
|
|
$responses[] = $res; |
341
|
|
|
|
342
|
|
|
} |
343
|
|
|
return $responses; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
public static function getBalance() |
347
|
|
|
{ |
348
|
|
|
$config = Config::get(); |
349
|
|
|
|
350
|
|
|
$endpoint = $config['base_url'].$config['balance_endpoint'].'?UserID='.$config['user_id'].'&Token='.md5($config['password']); |
351
|
|
|
|
352
|
|
|
$request = new Request($endpoint); |
353
|
|
|
$response = $request->getBalance(); |
354
|
|
|
|
355
|
|
|
return new Response($response->body); |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|