1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the smsmode-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2019 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Bundle\SMSModeBundle\EventListener; |
13
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use RuntimeException; |
16
|
|
|
use WBW\Bundle\SMSModeBundle\Event\AbstractSMSModeEvent; |
17
|
|
|
use WBW\Bundle\SMSModeBundle\Event\AccountBalanceEvent; |
18
|
|
|
use WBW\Bundle\SMSModeBundle\Event\AddingContactEvent; |
19
|
|
|
use WBW\Bundle\SMSModeBundle\Event\CheckingSMSMessageStatusEvent; |
20
|
|
|
use WBW\Bundle\SMSModeBundle\Event\CreatingAPIKeyEvent; |
21
|
|
|
use WBW\Bundle\SMSModeBundle\Event\CreatingSubAccountEvent; |
22
|
|
|
use WBW\Bundle\SMSModeBundle\Event\DeletingSMSEvent; |
23
|
|
|
use WBW\Bundle\SMSModeBundle\Event\DeletingSubAccountEvent; |
24
|
|
|
use WBW\Bundle\SMSModeBundle\Event\DeliveryReportEvent; |
25
|
|
|
use WBW\Bundle\SMSModeBundle\Event\RetrievingSMSReplyEvent; |
26
|
|
|
use WBW\Bundle\SMSModeBundle\Event\SendingSMSBatchEvent; |
27
|
|
|
use WBW\Bundle\SMSModeBundle\Event\SendingSMSMessageEvent; |
28
|
|
|
use WBW\Bundle\SMSModeBundle\Event\SendingTextToSpeechSMSEvent; |
29
|
|
|
use WBW\Bundle\SMSModeBundle\Event\SendingUnicodeSMSEvent; |
30
|
|
|
use WBW\Bundle\SMSModeBundle\Event\SentSMSMessageListEvent; |
31
|
|
|
use WBW\Bundle\SMSModeBundle\Event\TransferringCreditsEvent; |
32
|
|
|
use WBW\Bundle\SMSModeBundle\Factory\SMSModeFactory; |
33
|
|
|
use WBW\Library\SMSMode\Exception\APIException; |
34
|
|
|
use WBW\Library\SMSMode\Model\AbstractRequest; |
35
|
|
|
use WBW\Library\SMSMode\Model\AbstractResponse; |
36
|
|
|
use WBW\Library\SMSMode\Model\Authentication; |
37
|
|
|
use WBW\Library\SMSMode\Provider\APIProvider; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* sMsmode event listener. |
41
|
|
|
* |
42
|
|
|
* @author webeweb <https://github.com/webeweb/> |
43
|
|
|
* @package WBW\Bundle\SMSModeBundle\EventListener |
44
|
|
|
*/ |
45
|
|
|
class SMSModeEventListener { |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Runtime exception message. |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
const RUNTIME_EXCEPTION_MESSAGE = <<< EOT |
53
|
|
|
sMsmode configuration is missing in your app/config/config.yml. |
54
|
|
|
Please, add smsmode.accesss_token or smsmode.pseudo and smsmode.pass |
55
|
|
|
EOT; |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Service name. |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
const SERVICE_NAME = "webeweb.smsmode.event_listener"; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* API provider. |
67
|
|
|
* |
68
|
|
|
* @var APIProvider |
69
|
|
|
*/ |
70
|
|
|
private $apiProvider; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Constructor. |
74
|
|
|
*/ |
75
|
|
|
public function __construct() { |
76
|
|
|
$authentication = new Authentication(); |
77
|
|
|
$this->setApiProvider(new APIProvider($authentication)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Before handle event. |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
* @throws RuntimeException Throws an runtime exception if the sMsmode configuration is missing. |
85
|
|
|
*/ |
86
|
|
|
protected function beforeHandleEvent() { |
87
|
|
|
|
88
|
|
|
$authentication = $this->getApiProvider()->getAuthentication(); |
89
|
|
|
$requestNormalizer = $this->getApiProvider()->getRequestNormalizer(); |
90
|
|
|
|
91
|
|
|
try { |
92
|
|
|
|
93
|
|
|
$requestNormalizer->normalize($authentication); |
94
|
|
|
} catch (InvalidArgumentException $ex) { |
95
|
|
|
|
96
|
|
|
throw new RuntimeException(self::RUNTIME_EXCEPTION_MESSAGE, 500, $ex); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Before return an event. |
102
|
|
|
* |
103
|
|
|
* @param AbstractSMSModeEvent $event The event. |
104
|
|
|
* @param AbstractRequest $request The request. |
105
|
|
|
* @param AbstractResponse $response The response. |
106
|
|
|
* @return AbstractSMSModeEvent Returns the event. |
107
|
|
|
*/ |
108
|
|
|
protected function beforeReturnEvent(AbstractSMSModeEvent $event, AbstractRequest $request, AbstractResponse $response) { |
109
|
|
|
|
110
|
|
|
$event->setRequest($request); |
111
|
|
|
$event->setResponse($response); |
112
|
|
|
|
113
|
|
|
return $event; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get the API provider. |
118
|
|
|
* |
119
|
|
|
* @return APIProvider Returns the API provider. |
120
|
|
|
*/ |
121
|
|
|
public function getApiProvider() { |
122
|
|
|
return $this->apiProvider; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* On account balance. |
127
|
|
|
* |
128
|
|
|
* @param AccountBalanceEvent $event The account balance event. |
129
|
|
|
* @return AccountBalanceEvent Returns the account balance event. |
130
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
131
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
132
|
|
|
*/ |
133
|
|
|
public function onAccountBalance(AccountBalanceEvent $event) { |
134
|
|
|
|
135
|
|
|
$this->beforeHandleEvent(); |
136
|
|
|
|
137
|
|
|
$request = SMSModeFactory::newAccountBalanceRequest(); |
138
|
|
|
$response = $this->getApiProvider()->accountBalance($request); |
139
|
|
|
|
140
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* On adding contact. |
145
|
|
|
* |
146
|
|
|
* @param AddingContactEvent $event The adding contact event. |
147
|
|
|
* @return AddingContactEvent Returns the adding contact event. |
148
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
149
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
150
|
|
|
*/ |
151
|
|
|
public function onAddingContact(AddingContactEvent $event) { |
152
|
|
|
|
153
|
|
|
$this->beforeHandleEvent(); |
154
|
|
|
|
155
|
|
|
$request = SMSModeFactory::newAddingContactRequest($event->getAddingContact()); |
156
|
|
|
$response = $this->getApiProvider()->addingContact($request); |
157
|
|
|
|
158
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* On checking SMS message status. |
163
|
|
|
* |
164
|
|
|
* @param CheckingSMSMessageStatusEvent $event The checking SMS message status event. |
165
|
|
|
* @return CheckingSMSMessageStatusEvent Returns the checking SMS message status event. |
166
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
167
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
168
|
|
|
*/ |
169
|
|
|
public function onCheckingSMSMessageStatus(CheckingSMSMessageStatusEvent $event) { |
170
|
|
|
|
171
|
|
|
$this->beforeHandleEvent(); |
172
|
|
|
|
173
|
|
|
$request = SMSModeFactory::newCheckingSMSMessageStatusRequest($event->getCheckingSMSMessageStatus()); |
174
|
|
|
$response = $this->getApiProvider()->checkingSMSMessageStatus($request); |
175
|
|
|
|
176
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* On creating API key. |
181
|
|
|
* |
182
|
|
|
* @param CreatingAPIKeyEvent $event The account balance event. |
183
|
|
|
* @return CreatingAPIKeyEvent Returns the account balance event. |
184
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
185
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
186
|
|
|
*/ |
187
|
|
|
public function onCreatingAPIKey(CreatingAPIKeyEvent $event) { |
188
|
|
|
|
189
|
|
|
$this->beforeHandleEvent(); |
190
|
|
|
|
191
|
|
|
$request = SMSModeFactory::newCreatingAPIKeyRequest(); |
192
|
|
|
$response = $this->getApiProvider()->creatingAPIKey($request); |
193
|
|
|
|
194
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* On creating sub-account. |
199
|
|
|
* |
200
|
|
|
* @param CreatingSubAccountEvent $event The creating sub-account event. |
201
|
|
|
* @return CreatingSubAccountEvent Returns the creating sub-account event. |
202
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
203
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
204
|
|
|
*/ |
205
|
|
|
public function onCreatingSubAccount(CreatingSubAccountEvent $event) { |
206
|
|
|
|
207
|
|
|
$this->beforeHandleEvent(); |
208
|
|
|
|
209
|
|
|
$request = SMSModeFactory::newCreatingSubAccountRequest($event->getCreatingSubAccount()); |
210
|
|
|
$response = $this->getApiProvider()->creatingSubAccount($request); |
211
|
|
|
|
212
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* On deleting SMS. |
217
|
|
|
* |
218
|
|
|
* @param DeletingSMSEvent $event The deleting SMS event. |
219
|
|
|
* @return DeletingSMSEvent Returns the deleting SMS event. |
220
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
221
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
222
|
|
|
*/ |
223
|
|
|
public function onDeletingSMS(DeletingSMSEvent $event) { |
224
|
|
|
|
225
|
|
|
$this->beforeHandleEvent(); |
226
|
|
|
|
227
|
|
|
$request = SMSModeFactory::newDeletingSMSRequest($event->getDeletingSMS()); |
228
|
|
|
$response = $this->getApiProvider()->deletingSMS($request); |
229
|
|
|
|
230
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* On deleting sub-account. |
235
|
|
|
* |
236
|
|
|
* @param DeletingSubAccountEvent $event The deleting sub-account event. |
237
|
|
|
* @return DeletingSubAccountEvent Returns the deleting sub-account event. |
238
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
239
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
240
|
|
|
*/ |
241
|
|
|
public function onDeletingSubAccount(DeletingSubAccountEvent $event) { |
242
|
|
|
|
243
|
|
|
$this->beforeHandleEvent(); |
244
|
|
|
|
245
|
|
|
$request = SMSModeFactory::newDeletingSubAccountRequest($event->getDeletingSubAccount()); |
246
|
|
|
$response = $this->getApiProvider()->deletingSubAccount($request); |
247
|
|
|
|
248
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* On delivery report. |
253
|
|
|
* |
254
|
|
|
* @param DeliveryReportEvent $event The delivery report event. |
255
|
|
|
* @return DeliveryReportEvent Returns the delivery report event. |
256
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
257
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
258
|
|
|
*/ |
259
|
|
|
public function onDeliveryReport(DeliveryReportEvent $event) { |
260
|
|
|
|
261
|
|
|
$this->beforeHandleEvent(); |
262
|
|
|
|
263
|
|
|
$request = SMSModeFactory::newDeliveryReportRequest($event->getDeliveryReport()); |
264
|
|
|
$response = $this->getApiProvider()->deliveryReport($request); |
265
|
|
|
|
266
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* On retrieving SMS reply. |
271
|
|
|
* |
272
|
|
|
* @param RetrievingSMSReplyEvent $event The retrieving SMS reply event. |
273
|
|
|
* @return RetrievingSMSReplyEvent Returns the retrieving SMS reply event. |
274
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
275
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
276
|
|
|
*/ |
277
|
|
|
public function onRetrievingSMSReply(RetrievingSMSReplyEvent $event) { |
278
|
|
|
|
279
|
|
|
$this->beforeHandleEvent(); |
280
|
|
|
|
281
|
|
|
$request = SMSModeFactory::newRetrievingSMSReplyRequest($event->getRetrievingSMSReply()); |
282
|
|
|
$response = $this->getApiProvider()->retrievingSMSReply($request); |
283
|
|
|
|
284
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* On sending SMS batch. |
289
|
|
|
* |
290
|
|
|
* @param SendingSMSBatchEvent $event The sending SMS batch event. |
291
|
|
|
* @return SendingSMSBatchEvent Returns the sending SMS batch event. |
292
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
293
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
294
|
|
|
*/ |
295
|
|
|
public function onSendingSMSBatch(SendingSMSBatchEvent $event) { |
296
|
|
|
|
297
|
|
|
$this->beforeHandleEvent(); |
298
|
|
|
|
299
|
|
|
$request = SMSModeFactory::newSendingSMSBatchRequest($event->getSendingSMSBatch()); |
300
|
|
|
$response = $this->getApiProvider()->sendingSMSBatch($request); |
301
|
|
|
|
302
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* On sending SMS message. |
307
|
|
|
* |
308
|
|
|
* @param SendingSMSMessageEvent $event The sending SMS message event. |
309
|
|
|
* @return SendingSMSMessageEvent Returns the sending SMS message event. |
310
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
311
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
312
|
|
|
*/ |
313
|
|
|
public function onSendingSMSMessage(SendingSMSMessageEvent $event) { |
314
|
|
|
|
315
|
|
|
$this->beforeHandleEvent(); |
316
|
|
|
|
317
|
|
|
$request = SMSModeFactory::newSendingSMSMessageRequest($event->getSendingSMSMessage()); |
318
|
|
|
$response = $this->getApiProvider()->sendingSMSMessage($request); |
319
|
|
|
|
320
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* On sending text-to-speech. |
325
|
|
|
* |
326
|
|
|
* @param SendingTextToSpeechSMSEvent $event The sending text-to-speech event. |
327
|
|
|
* @return SendingTextToSpeechSMSEvent Returns the sending text-to-speech event. |
328
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
329
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
330
|
|
|
*/ |
331
|
|
|
public function onSendingTextToSpeechSMS(SendingTextToSpeechSMSEvent $event) { |
332
|
|
|
|
333
|
|
|
$this->beforeHandleEvent(); |
334
|
|
|
|
335
|
|
|
$request = SMSModeFactory::newSendingTextToSpeechSMSRequest($event->getSendingTextToSpeechSMS()); |
336
|
|
|
$response = $this->getApiProvider()->sendingTextToSpeechSMS($request); |
337
|
|
|
|
338
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* On sending unicode SMS. |
343
|
|
|
* |
344
|
|
|
* @param SendingUnicodeSMSEvent $event The sending unicode SMS event. |
345
|
|
|
* @return SendingUnicodeSMSEvent Returns the sending unicode SMS event. |
346
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
347
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
348
|
|
|
*/ |
349
|
|
|
public function onSendingUnicodeSMS(SendingUnicodeSMSEvent $event) { |
350
|
|
|
|
351
|
|
|
$this->beforeHandleEvent(); |
352
|
|
|
|
353
|
|
|
$request = SMSModeFactory::newSendingUnicodeSMSRequest($event->getSendingUnicodeSMS()); |
354
|
|
|
$response = $this->getApiProvider()->sendingUnicodeSMS($request); |
355
|
|
|
|
356
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* On sent SMS message lis. |
361
|
|
|
* |
362
|
|
|
* @param SentSMSMessageListEvent $event The sent SMS message list event. |
363
|
|
|
* @return SentSMSMessageListEvent Returns the sent SMS message list event. |
364
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
365
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
366
|
|
|
*/ |
367
|
|
|
public function onSentSMSMessageList(SentSMSMessageListEvent $event) { |
368
|
|
|
|
369
|
|
|
$this->beforeHandleEvent(); |
370
|
|
|
|
371
|
|
|
$request = SMSModeFactory::newSentSMSMessageListRequest($event->getSentSMSMessageList()); |
372
|
|
|
$response = $this->getApiProvider()->sentSMSMessageList($request); |
373
|
|
|
|
374
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* On transferring credits. |
379
|
|
|
* |
380
|
|
|
* @param TransferringCreditsEvent $event The transferring credits event. |
381
|
|
|
* @return TransferringCreditsEvent Returns the transferring credits event. |
382
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
383
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
384
|
|
|
*/ |
385
|
|
|
public function onTransferringCredits(TransferringCreditsEvent $event) { |
386
|
|
|
|
387
|
|
|
$this->beforeHandleEvent(); |
388
|
|
|
|
389
|
|
|
$request = SMSModeFactory::newTransferringCreditsRequest($event->getTransferringCredits()); |
390
|
|
|
$response = $this->getApiProvider()->transferringCredits($request); |
391
|
|
|
|
392
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Set the access token. |
397
|
|
|
* |
398
|
|
|
* @param string $accessToken The access token. |
399
|
|
|
* @return SMSModeEventListener Returns this event listener. |
400
|
|
|
*/ |
401
|
|
|
public function setAccessToken($accessToken) { |
402
|
|
|
$this->getApiProvider()->getAuthentication()->setAccessToken($accessToken); |
403
|
|
|
return $this; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Set the API provider. |
408
|
|
|
* |
409
|
|
|
* @param APIProvider $apiProvider The API provider. |
410
|
|
|
* @return SMSModeEventListener Returns this event listener. |
411
|
|
|
*/ |
412
|
|
|
protected function setApiProvider(ApiProvider $apiProvider) { |
413
|
|
|
$this->apiProvider = $apiProvider; |
414
|
|
|
return $this; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Set the pass. |
419
|
|
|
* |
420
|
|
|
* @param string $pass The pass. |
421
|
|
|
* @return SMSModeEventListener Returns this event listener. |
422
|
|
|
*/ |
423
|
|
|
public function setPass($pass) { |
424
|
|
|
$this->getApiProvider()->getAuthentication()->setPass($pass); |
425
|
|
|
return $this; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Set the pseudo. |
430
|
|
|
* |
431
|
|
|
* @param string $pseudo The pseudo. |
432
|
|
|
* @return SMSModeEventListener Returns this event listener. |
433
|
|
|
*/ |
434
|
|
|
public function setPseudo($pseudo) { |
435
|
|
|
$this->getApiProvider()->getAuthentication()->setPseudo($pseudo); |
436
|
|
|
return $this; |
437
|
|
|
} |
438
|
|
|
} |
439
|
|
|
|