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 Symfony\Component\EventDispatcher\Event; |
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
|
|
|
* Service name. |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
const SERVICE_NAME = "webeweb.smsmode.event_listener"; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* API provider. |
56
|
|
|
* |
57
|
|
|
* @var APIProvider |
58
|
|
|
*/ |
59
|
|
|
private $apiProvider; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Constructor. |
63
|
|
|
*/ |
64
|
|
|
public function __construct() { |
65
|
|
|
$authentication = new Authentication(); |
66
|
|
|
$this->setApiProvider(new APIProvider($authentication)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Before return an event. |
71
|
|
|
* |
72
|
|
|
* @param AbstractSMSModeEvent $event The event. |
73
|
|
|
* @param AbstractRequest $request The request. |
74
|
|
|
* @param AbstractResponse $response The response. |
75
|
|
|
* @return Event Returns the event. |
76
|
|
|
*/ |
77
|
|
|
protected function beforeReturnEvent(AbstractSMSModeEvent $event, AbstractRequest $request, AbstractResponse $response) { |
78
|
|
|
|
79
|
|
|
$event->setRequest($request); |
80
|
|
|
$event->setResponse($response); |
81
|
|
|
|
82
|
|
|
return $event; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the API provider. |
87
|
|
|
* |
88
|
|
|
* @return APIProvider Returns the API provider. |
89
|
|
|
*/ |
90
|
|
|
public function getApiProvider() { |
91
|
|
|
return $this->apiProvider; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* On account balance. |
96
|
|
|
* |
97
|
|
|
* @param AccountBalanceEvent $event The account balance event. |
98
|
|
|
* @return AccountBalanceEvent Returns the account balance event. |
99
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
100
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
101
|
|
|
*/ |
102
|
|
|
public function onAccountBalance(AccountBalanceEvent $event) { |
103
|
|
|
|
104
|
|
|
$request = SMSModeFactory::newAccountBalanceRequest(); |
105
|
|
|
$response = $this->getApiProvider()->accountBalance($request); |
106
|
|
|
|
107
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* On adding contact. |
112
|
|
|
* |
113
|
|
|
* @param AddingContactEvent $event The adding contact event. |
114
|
|
|
* @return AddingContactEvent Returns the adding contact event. |
115
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
116
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
117
|
|
|
*/ |
118
|
|
|
public function onAddingContact(AddingContactEvent $event) { |
119
|
|
|
|
120
|
|
|
$request = SMSModeFactory::newAddingContactRequest($event->getAddingContact()); |
121
|
|
|
$response = $this->getApiProvider()->addingContact($request); |
122
|
|
|
|
123
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* On checking SMS message status. |
128
|
|
|
* |
129
|
|
|
* @param CheckingSMSMessageStatusEvent $event The checking SMS message status event. |
130
|
|
|
* @return CheckingSMSMessageStatusEvent Returns the checking SMS message status event. |
131
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
132
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
133
|
|
|
*/ |
134
|
|
|
public function onCheckingSMSMessageStatus(CheckingSMSMessageStatusEvent $event) { |
135
|
|
|
|
136
|
|
|
$request = SMSModeFactory::newCheckingSMSMessageStatusRequest($event->getCheckingSMSMessageStatus()); |
137
|
|
|
$response = $this->getApiProvider()->checkingSMSMessageStatus($request); |
138
|
|
|
|
139
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* On creating API key. |
144
|
|
|
* |
145
|
|
|
* @param CreatingAPIKeyEvent $event The account balance event. |
146
|
|
|
* @return CreatingAPIKeyEvent Returns the account balance event. |
147
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
148
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
149
|
|
|
*/ |
150
|
|
|
public function onCreatingAPIKey(CreatingAPIKeyEvent $event) { |
151
|
|
|
|
152
|
|
|
$request = SMSModeFactory::newCreatingAPIKeyRequest(); |
153
|
|
|
$response = $this->getApiProvider()->creatingAPIKey($request); |
154
|
|
|
|
155
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* On creating sub-account. |
160
|
|
|
* |
161
|
|
|
* @param CreatingSubAccountEvent $event The creating sub-account event. |
162
|
|
|
* @return CreatingSubAccountEvent Returns the creating sub-account event. |
163
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
164
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
165
|
|
|
*/ |
166
|
|
|
public function onCreatingSubAccount(CreatingSubAccountEvent $event) { |
167
|
|
|
|
168
|
|
|
$request = SMSModeFactory::newCreatingSubAccountRequest($event->getCreatingSubAccount()); |
169
|
|
|
$response = $this->getApiProvider()->creatingSubAccount($request); |
170
|
|
|
|
171
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* On deleting SMS. |
176
|
|
|
* |
177
|
|
|
* @param DeletingSMSEvent $event The deleting SMS event. |
178
|
|
|
* @return DeletingSMSEvent Returns the deleting SMS event. |
179
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
180
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
181
|
|
|
*/ |
182
|
|
|
public function onDeletingSMS(DeletingSMSEvent $event) { |
183
|
|
|
|
184
|
|
|
$request = SMSModeFactory::newDeletingSMSRequest($event->getDeletingSMS()); |
185
|
|
|
$response = $this->getApiProvider()->deletingSMS($request); |
186
|
|
|
|
187
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* On deleting sub-account. |
192
|
|
|
* |
193
|
|
|
* @param DeletingSubAccountEvent $event The deleting sub-account event. |
194
|
|
|
* @return DeletingSubAccountEvent Returns the deleting sub-account event. |
195
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
196
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
197
|
|
|
*/ |
198
|
|
|
public function onDeletingSubAccount(DeletingSubAccountEvent $event) { |
199
|
|
|
|
200
|
|
|
$request = SMSModeFactory::newDeletingSubAccountRequest($event->getDeletingSubAccount()); |
201
|
|
|
$response = $this->getApiProvider()->deletingSubAccount($request); |
202
|
|
|
|
203
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* On delivery report. |
208
|
|
|
* |
209
|
|
|
* @param DeliveryReportEvent $event The delivery report event. |
210
|
|
|
* @return DeliveryReportEvent Returns the delivery report event. |
211
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
212
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
213
|
|
|
*/ |
214
|
|
|
public function onDeliveryReport(DeliveryReportEvent $event) { |
215
|
|
|
|
216
|
|
|
$request = SMSModeFactory::newDeliveryReportRequest($event->getDeliveryReport()); |
217
|
|
|
$response = $this->getApiProvider()->deliveryReport($request); |
218
|
|
|
|
219
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* On retrieving SMS reply. |
224
|
|
|
* |
225
|
|
|
* @param RetrievingSMSReplyEvent $event The retrieving SMS reply event. |
226
|
|
|
* @return RetrievingSMSReplyEvent Returns the retrieving SMS reply event. |
227
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
228
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
229
|
|
|
*/ |
230
|
|
|
public function onRetrievingSMSReply(RetrievingSMSReplyEvent $event) { |
231
|
|
|
|
232
|
|
|
$request = SMSModeFactory::newRetrievingSMSReplyRequest($event->getRetrievingSMSReply()); |
233
|
|
|
$response = $this->getApiProvider()->retrievingSMSReply($request); |
234
|
|
|
|
235
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* On sending SMS batch. |
240
|
|
|
* |
241
|
|
|
* @param SendingSMSBatchEvent $event The sending SMS batch event. |
242
|
|
|
* @return SendingSMSBatchEvent Returns the sending SMS batch event. |
243
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
244
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
245
|
|
|
*/ |
246
|
|
|
public function onSendingSMSBatch(SendingSMSBatchEvent $event) { |
247
|
|
|
|
248
|
|
|
$request = SMSModeFactory::newSendingSMSBatchRequest($event->getSendingSMSBatch()); |
249
|
|
|
$response = $this->getApiProvider()->sendingSMSBatch($request); |
250
|
|
|
|
251
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* On sending SMS message. |
256
|
|
|
* |
257
|
|
|
* @param SendingSMSMessageEvent $event The sending SMS message event. |
258
|
|
|
* @return SendingSMSMessageEvent Returns the sending SMS message event. |
259
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
260
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
261
|
|
|
*/ |
262
|
|
|
public function onSendingSMSMessage(SendingSMSMessageEvent $event) { |
263
|
|
|
|
264
|
|
|
$request = SMSModeFactory::newSendingSMSMessageRequest($event->getSendingSMSMessage()); |
265
|
|
|
$response = $this->getApiProvider()->sendingSMSMessage($request); |
266
|
|
|
|
267
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* On sending text-to-speech. |
272
|
|
|
* |
273
|
|
|
* @param SendingTextToSpeechSMSEvent $event The sending text-to-speech event. |
274
|
|
|
* @return SendingTextToSpeechSMSEvent Returns the sending text-to-speech event. |
275
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
276
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
277
|
|
|
*/ |
278
|
|
|
public function onSendingTextToSpeechSMS(SendingTextToSpeechSMSEvent $event) { |
279
|
|
|
|
280
|
|
|
$request = SMSModeFactory::newSendingTextToSpeechSMSRequest($event->getSendingTextToSpeechSMS()); |
281
|
|
|
$response = $this->getApiProvider()->sendingTextToSpeechSMS($request); |
282
|
|
|
|
283
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* On sending unicode SMS. |
288
|
|
|
* |
289
|
|
|
* @param SendingUnicodeSMSEvent $event The sending unicode SMS event. |
290
|
|
|
* @return SendingUnicodeSMSEvent Returns the sending unicode SMS event. |
291
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
292
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
293
|
|
|
*/ |
294
|
|
|
public function onSendingUnicodeSMS(SendingUnicodeSMSEvent $event) { |
295
|
|
|
|
296
|
|
|
$request = SMSModeFactory::newSendingUnicodeSMSRequest($event->getSendingUnicodeSMS()); |
297
|
|
|
$response = $this->getApiProvider()->sendingUnicodeSMS($request); |
298
|
|
|
|
299
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* On sent SMS message lis. |
304
|
|
|
* |
305
|
|
|
* @param SentSMSMessageListEvent $event The sent SMS message list event. |
306
|
|
|
* @return SentSMSMessageListEvent Returns the sent SMS message list event. |
307
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
308
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
309
|
|
|
*/ |
310
|
|
|
public function onSentSMSMessageList(SentSMSMessageListEvent $event) { |
311
|
|
|
|
312
|
|
|
$request = SMSModeFactory::newSentSMSMessageListRequest($event->getSentSMSMessageList()); |
313
|
|
|
$response = $this->getApiProvider()->sentSMSMessageList($request); |
314
|
|
|
|
315
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* On transferring credits. |
320
|
|
|
* |
321
|
|
|
* @param TransferringCreditsEvent $event The transferring credits event. |
322
|
|
|
* @return TransferringCreditsEvent Returns the transferring credits event. |
323
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
324
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
325
|
|
|
*/ |
326
|
|
|
public function onTransferringCredits(TransferringCreditsEvent $event) { |
327
|
|
|
|
328
|
|
|
$request = SMSModeFactory::newTransferringCreditsRequest($event->getTransferringCredits()); |
329
|
|
|
$response = $this->getApiProvider()->transferringCredits($request); |
330
|
|
|
|
331
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Set the access token. |
336
|
|
|
* |
337
|
|
|
* @param string $accessToken The access token. |
338
|
|
|
* @return SMSModeEventListener Returns this event listener. |
339
|
|
|
*/ |
340
|
|
|
public function setAccessToken($accessToken) { |
341
|
|
|
$this->getApiProvider()->getAuthentication()->setAccessToken($accessToken); |
342
|
|
|
return $this; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Set the API provider. |
347
|
|
|
* |
348
|
|
|
* @param APIProvider $apiProvider The API provider. |
349
|
|
|
* @return SMSModeEventListener Returns this event listener. |
350
|
|
|
*/ |
351
|
|
|
protected function setApiProvider(ApiProvider $apiProvider) { |
352
|
|
|
$this->apiProvider = $apiProvider; |
353
|
|
|
return $this; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Set the pass. |
358
|
|
|
* |
359
|
|
|
* @param string $pass The pass. |
360
|
|
|
* @return SMSModeEventListener Returns this event listener. |
361
|
|
|
*/ |
362
|
|
|
public function setPass($pass) { |
363
|
|
|
$this->getApiProvider()->getAuthentication()->setPass($pass); |
364
|
|
|
return $this; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Set the pseudo. |
369
|
|
|
* |
370
|
|
|
* @param string $pseudo The pseudo. |
371
|
|
|
* @return SMSModeEventListener Returns this event listener. |
372
|
|
|
*/ |
373
|
|
|
public function setPseudo($pseudo) { |
374
|
|
|
$this->getApiProvider()->getAuthentication()->setPseudo($pseudo); |
375
|
|
|
return $this; |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
|