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; |
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
|
|
|
$this->recipients = $recipients; |
191
|
|
|
$this->message = $message; |
192
|
|
|
$this->endpoint = $this->config['base_url'] . $this->config['sms_endpoint']; |
193
|
|
|
|
194
|
|
|
if ($this->batch_type === BatchType::NOT_BATCH) { |
195
|
|
|
|
196
|
|
View Code Duplication |
if (is_array($this->message) && array_depth($this->message) == 2 && count($this->message) == 1) { |
|
|
|
|
197
|
|
|
|
198
|
|
|
if (is_array($this->recipients) && array_depth($this->recipients) == 2 && count($this->recipients) == 1) { |
199
|
|
|
|
200
|
|
|
$response = $this->sendForNonBatch($this->buildSendObject($this->recipients, $this->message)); |
201
|
|
|
|
202
|
|
|
} else { |
203
|
|
|
|
204
|
|
|
throw new BongaTechException('The recipient MUST be an array of depth 2 and count should not be more than 1'); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
} else { |
208
|
|
|
|
209
|
|
|
throw new BongaTechException('Message should be provided as an array whose depth is 2 and count should equal 1'); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
|
213
|
|
View Code Duplication |
} elseif ($this->batch_type === BatchType::SAME_MESSAGE) { |
|
|
|
|
214
|
|
|
|
215
|
|
|
if (is_array($this->message) && array_depth($this->message) == 2 && count($this->message) == 1) { |
216
|
|
|
|
217
|
|
|
if (is_array($this->recipients) && array_depth($this->recipients) == 2 && count($this->recipients) > 1) { |
218
|
|
|
|
219
|
|
|
$response = $this->sendForBatch($this->buildSendObject($this->recipients, $this->message)); |
220
|
|
|
|
221
|
|
|
} else { |
222
|
|
|
|
223
|
|
|
throw new BongaTechException('The recipients MUST be an array of depth 2 and count should be more than 1'); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
|
227
|
|
|
} else { |
228
|
|
|
|
229
|
|
|
throw new BongaTechException('Message should be provided as an array whose depth and count should equal 1'); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
} elseif ($this->batch_type === BatchType::DIFFERENT_MESSAGE) { |
233
|
|
|
|
234
|
|
|
if (count($this->recipients) == count($this->message)) { |
235
|
|
|
|
236
|
|
|
if (is_array($this->message) && array_depth($this->message) == 2) { |
237
|
|
|
|
238
|
|
|
if (is_array($this->recipients) && array_depth($this->recipients) == 2) { |
239
|
|
|
|
240
|
|
|
$response = $this->sendForBatch($this->buildSendObject($this->recipients, $this->message)); |
241
|
|
|
|
242
|
|
|
} else { |
243
|
|
|
|
244
|
|
|
throw new BongaTechException('The recipients MUST be an array of depth 2'); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
} else { |
248
|
|
|
|
249
|
|
|
throw new BongaTechException('Message MUST be an array of depth 2'); |
250
|
|
|
} |
251
|
|
|
} else { |
252
|
|
|
|
253
|
|
|
throw new BongaTechException('No. of Messages MUST be equal to number of Recipients.'); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
} else { |
257
|
|
|
|
258
|
|
|
throw new BongaTechException('Message Batch Type has not been set.'); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return $response; |
262
|
|
|
|
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* build the send object. |
267
|
|
|
* |
268
|
|
|
* @param recipients |
269
|
|
|
* @param $messages |
270
|
|
|
* |
271
|
|
|
* @return array |
272
|
|
|
*/ |
273
|
|
|
private function buildSendObject($recipients, $messages) |
274
|
|
|
{ |
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
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* send a message to a single recipient. |
311
|
|
|
* |
312
|
|
|
* @param $body |
313
|
|
|
* |
314
|
|
|
* @return Response |
315
|
|
|
*/ |
316
|
|
|
private function sendForNonBatch($body) |
317
|
|
|
{ |
318
|
|
|
|
319
|
|
|
$request = new Request($this->endpoint, $body); |
320
|
|
|
$response = $request->sendSMS(); |
321
|
|
|
|
322
|
|
|
return new Response($response->body[0]); |
323
|
|
|
|
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* send batch. 1) same message to many recipients 2) different messages to many recipients. |
328
|
|
|
* |
329
|
|
|
* @param $body |
330
|
|
|
* |
331
|
|
|
* @return Response |
332
|
|
|
*/ |
333
|
|
|
private function sendForBatch($body) |
334
|
|
|
{ |
335
|
|
|
|
336
|
|
|
$request = new Request($this->endpoint, $body); |
337
|
|
|
$response = $request->sendSMS(); |
338
|
|
|
|
339
|
|
|
//return $response->body; |
|
|
|
|
340
|
|
|
|
341
|
|
|
$responses = []; |
|
|
|
|
342
|
|
|
|
343
|
|
|
for ($i = 0; $i < count($response->body); $i++) { |
|
|
|
|
344
|
|
|
|
345
|
|
|
$res = new Response($response->body[$i]); |
346
|
|
|
$responses = $res; |
347
|
|
|
|
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
return $responses; |
351
|
|
|
|
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
public static function getBalance() |
355
|
|
|
{ |
356
|
|
|
$config = Config::get(); |
357
|
|
|
|
358
|
|
|
$endpoint = $config['base_url'] . $config['balance_endpoint'] . '?UserID=' . $config['user_id'] . '&Token=' . md5($config['password']); |
359
|
|
|
|
360
|
|
|
$request = new Request($endpoint); |
361
|
|
|
$response = $request->getBalance(); |
362
|
|
|
|
363
|
|
|
return new Response($response->body); |
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.