DefaultEventListener::onSendingUnicodeSms()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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 GuzzleHttp\Exception\GuzzleException;
15
use InvalidArgumentException;
16
use Psr\Log\LoggerInterface;
17
use RuntimeException;
18
use WBW\Bundle\SmsModeBundle\Event\AbstractEvent;
19
use WBW\Bundle\SmsModeBundle\Event\AccountBalanceEvent;
20
use WBW\Bundle\SmsModeBundle\Event\AddingContactEvent;
21
use WBW\Bundle\SmsModeBundle\Event\CheckingSmsMessageStatusEvent;
22
use WBW\Bundle\SmsModeBundle\Event\CreatingApiKeyEvent;
23
use WBW\Bundle\SmsModeBundle\Event\CreatingSubAccountEvent;
24
use WBW\Bundle\SmsModeBundle\Event\DeletingSmsEvent;
25
use WBW\Bundle\SmsModeBundle\Event\DeletingSubAccountEvent;
26
use WBW\Bundle\SmsModeBundle\Event\DeliveryReportEvent;
27
use WBW\Bundle\SmsModeBundle\Event\RetrievingSmsReplyEvent;
28
use WBW\Bundle\SmsModeBundle\Event\SendingSmsBatchEvent;
29
use WBW\Bundle\SmsModeBundle\Event\SendingSmsMessageEvent;
30
use WBW\Bundle\SmsModeBundle\Event\SendingTextToSpeechSmsEvent;
31
use WBW\Bundle\SmsModeBundle\Event\SendingUnicodeSmsEvent;
32
use WBW\Bundle\SmsModeBundle\Event\SentSmsMessageListEvent;
33
use WBW\Bundle\SmsModeBundle\Event\TransferringCreditsEvent;
34
use WBW\Library\Logger\LoggerTrait;
35
use WBW\Library\Provider\Exception\ApiException;
36
use WBW\Library\SmsMode\Factory\RequestFactory;
37
use WBW\Library\SmsMode\Model\Authentication;
38
use WBW\Library\SmsMode\Provider\ApiProvider;
39
use WBW\Library\SmsMode\Request\AbstractRequest;
40
use WBW\Library\SmsMode\Response\AbstractResponse;
41
42
/**
43
 * Default event listener.
44
 *
45
 * @author webeweb <https://github.com/webeweb>
46
 * @package WBW\Bundle\SmsModeBundle\EventListener
47
 */
48
class DefaultEventListener {
49
50
    use LoggerTrait;
51
52
    /**
53
     * Runtime exception message.
54
     *
55
     * @var string
56
     */
57
    const RUNTIME_EXCEPTION_MESSAGE = <<< EOT
58
sMsmode configuration is missing in your app/config/config.yml.
59
Please, add smsmode.accesss_token or smsmode.pseudo and smsmode.pass
60
EOT;
61
62
    /**
63
     * Service name.
64
     *
65
     * @var string
66
     */
67
    const SERVICE_NAME = "wbw.smsmode.event_listener.default";
68
69
    /**
70
     * API provider.
71
     *
72
     * @var ApiProvider
73
     */
74
    private $apiProvider;
75
76
    /**
77
     * Constructor.
78
     *
79
     * @param LoggerInterface $logger The logger.
80
     */
81
    public function __construct(LoggerInterface $logger) {
82
        $authentication = new Authentication();
83
        $this->setApiProvider(new ApiProvider($authentication, $logger));
84
    }
85
86
    /**
87
     * Before handle event.
88
     *
89
     * @return void
90
     * @throws RuntimeException Throws a runtime exception if the sMsmode configuration is missing.
91
     */
92
    protected function beforeHandleEvent(): void {
93
94
        $authentication = $this->getApiProvider()->getAuthentication();
95
        $serializer     = $this->getApiProvider()->getRequestSerializer();
96
97
        try {
98
99
            $serializer->serialize($authentication);
100
        } catch (InvalidArgumentException $ex) {
101
102
            throw new RuntimeException(self::RUNTIME_EXCEPTION_MESSAGE, 500, $ex);
103
        }
104
    }
105
106
    /**
107
     * Before return an event.
108
     *
109
     * @param AbstractEvent $event The event.
110
     * @param AbstractRequest $request The request.
111
     * @param AbstractResponse $response The response.
112
     * @return AbstractEvent Returns the event.
113
     */
114
    protected function beforeReturnEvent(AbstractEvent $event, AbstractRequest $request, AbstractResponse $response): AbstractEvent {
115
116
        $event->setRequest($request);
117
        $event->setResponse($response);
118
119
        return $event;
120
    }
121
122
    /**
123
     * Get the API provider.
124
     *
125
     * @return ApiProvider Returns the API provider.
126
     */
127
    public function getApiProvider(): ApiProvider {
128
        return $this->apiProvider;
129
    }
130
131
    /**
132
     * On account balance.
133
     *
134
     * @param AccountBalanceEvent $event The account balance event.
135
     * @return AccountBalanceEvent Returns the account balance event.
136
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
137
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
138
     * @throws ApiException Throws an API exception if an error occurs.
139
     */
140
    public function onAccountBalance(AccountBalanceEvent $event): AccountBalanceEvent {
141
142
        $this->beforeHandleEvent();
143
144
        $request  = RequestFactory::newAccountBalanceRequest();
145
        $response = $this->getApiProvider()->accountBalance($request);
146
147
        return $this->beforeReturnEvent($event, $request, $response);
148
    }
149
150
    /**
151
     * On adding contact.
152
     *
153
     * @param AddingContactEvent $event The adding contact event.
154
     * @return AddingContactEvent Returns the adding contact event.
155
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
156
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
157
     * @throws ApiException Throws an API exception if an error occurs.
158
     */
159
    public function onAddingContact(AddingContactEvent $event): AddingContactEvent {
160
161
        $this->beforeHandleEvent();
162
163
        $request  = RequestFactory::newAddingContactRequest($event->getAddingContact());
164
        $response = $this->getApiProvider()->addingContact($request);
165
166
        return $this->beforeReturnEvent($event, $request, $response);
167
    }
168
169
    /**
170
     * On checking SMS message status.
171
     *
172
     * @param CheckingSmsMessageStatusEvent $event The checking SMS message status event.
173
     * @return CheckingSmsMessageStatusEvent Returns the checking SMS message status event.
174
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
175
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
176
     * @throws ApiException Throws an API exception if an error occurs.
177
     */
178
    public function onCheckingSmsMessageStatus(CheckingSmsMessageStatusEvent $event): CheckingSmsMessageStatusEvent {
179
180
        $this->beforeHandleEvent();
181
182
        $request  = RequestFactory::newCheckingSmsMessageStatusRequest($event->getCheckingSmsMessageStatus());
183
        $response = $this->getApiProvider()->checkingSmsMessageStatus($request);
184
185
        return $this->beforeReturnEvent($event, $request, $response);
186
    }
187
188
    /**
189
     * On creating API key.
190
     *
191
     * @param CreatingApiKeyEvent $event The account balance event.
192
     * @return CreatingApiKeyEvent Returns the account balance event.
193
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
194
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
195
     * @throws ApiException Throws an API exception if an error occurs.
196
     */
197
    public function onCreatingApiKey(CreatingApiKeyEvent $event): CreatingApiKeyEvent {
198
199
        $this->beforeHandleEvent();
200
201
        $request  = RequestFactory::newCreatingApiKeyRequest();
202
        $response = $this->getApiProvider()->creatingApiKey($request);
203
204
        return $this->beforeReturnEvent($event, $request, $response);
205
    }
206
207
    /**
208
     * On creating sub-account.
209
     *
210
     * @param CreatingSubAccountEvent $event The creating sub-account event.
211
     * @return CreatingSubAccountEvent Returns the creating sub-account event.
212
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
213
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
214
     * @throws ApiException Throws an API exception if an error occurs.
215
     */
216
    public function onCreatingSubAccount(CreatingSubAccountEvent $event): CreatingSubAccountEvent {
217
218
        $this->beforeHandleEvent();
219
220
        $request  = RequestFactory::newCreatingSubAccountRequest($event->getCreatingSubAccount());
221
        $response = $this->getApiProvider()->creatingSubAccount($request);
222
223
        return $this->beforeReturnEvent($event, $request, $response);
224
    }
225
226
    /**
227
     * On deleting SMS.
228
     *
229
     * @param DeletingSmsEvent $event The deleting SMS event.
230
     * @return DeletingSmsEvent Returns the deleting SMS event.
231
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
232
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
233
     * @throws ApiException Throws an API exception if an error occurs.
234
     */
235
    public function onDeletingSms(DeletingSmsEvent $event): DeletingSmsEvent {
236
237
        $this->beforeHandleEvent();
238
239
        $request  = RequestFactory::newDeletingSmsRequest($event->getDeletingSms());
240
        $response = $this->getApiProvider()->deletingSms($request);
241
242
        return $this->beforeReturnEvent($event, $request, $response);
243
    }
244
245
    /**
246
     * On deleting sub-account.
247
     *
248
     * @param DeletingSubAccountEvent $event The deleting sub-account event.
249
     * @return DeletingSubAccountEvent Returns the deleting sub-account event.
250
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
251
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
252
     * @throws ApiException Throws an API exception if an error occurs.
253
     */
254
    public function onDeletingSubAccount(DeletingSubAccountEvent $event): DeletingSubAccountEvent {
255
256
        $this->beforeHandleEvent();
257
258
        $request  = RequestFactory::newDeletingSubAccountRequest($event->getDeletingSubAccount());
259
        $response = $this->getApiProvider()->deletingSubAccount($request);
260
261
        return $this->beforeReturnEvent($event, $request, $response);
262
    }
263
264
    /**
265
     * On delivery report.
266
     *
267
     * @param DeliveryReportEvent $event The delivery report event.
268
     * @return DeliveryReportEvent Returns the delivery report event.
269
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
270
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
271
     * @throws ApiException Throws an API exception if an error occurs.
272
     */
273
    public function onDeliveryReport(DeliveryReportEvent $event): DeliveryReportEvent {
274
275
        $this->beforeHandleEvent();
276
277
        $request  = RequestFactory::newDeliveryReportRequest($event->getDeliveryReport());
278
        $response = $this->getApiProvider()->deliveryReport($request);
279
280
        return $this->beforeReturnEvent($event, $request, $response);
281
    }
282
283
    /**
284
     * On retrieving SMS reply.
285
     *
286
     * @param RetrievingSmsReplyEvent $event The retrieving SMS reply event.
287
     * @return RetrievingSmsReplyEvent Returns the retrieving SMS reply event.
288
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
289
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
290
     * @throws ApiException Throws an API exception if an error occurs.
291
     */
292
    public function onRetrievingSmsReply(RetrievingSmsReplyEvent $event): RetrievingSmsReplyEvent {
293
294
        $this->beforeHandleEvent();
295
296
        $request  = RequestFactory::newRetrievingSmsReplyRequest($event->getRetrievingSmsReply());
297
        $response = $this->getApiProvider()->retrievingSmsReply($request);
298
299
        return $this->beforeReturnEvent($event, $request, $response);
300
    }
301
302
    /**
303
     * On sending SMS batch.
304
     *
305
     * @param SendingSmsBatchEvent $event The sending SMS batch event.
306
     * @return SendingSmsBatchEvent Returns the sending SMS batch event.
307
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
308
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
309
     * @throws ApiException Throws an API exception if an error occurs.
310
     */
311
    public function onSendingSmsBatch(SendingSmsBatchEvent $event): SendingSmsBatchEvent {
312
313
        $this->beforeHandleEvent();
314
315
        $request  = RequestFactory::newSendingSmsBatchRequest($event->getSendingSmsBatch());
316
        $response = $this->getApiProvider()->sendingSmsBatch($request);
317
318
        return $this->beforeReturnEvent($event, $request, $response);
319
    }
320
321
    /**
322
     * On sending SMS message.
323
     *
324
     * @param SendingSmsMessageEvent $event The sending SMS message event.
325
     * @return SendingSmsMessageEvent Returns the sending SMS message event.
326
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
327
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
328
     * @throws ApiException Throws an API exception if an error occurs.
329
     */
330
    public function onSendingSmsMessage(SendingSmsMessageEvent $event): SendingSmsMessageEvent {
331
332
        $this->beforeHandleEvent();
333
334
        $request  = RequestFactory::newSendingSmsMessageRequest($event->getSendingSmsMessage());
335
        $response = $this->getApiProvider()->sendingSmsMessage($request);
336
337
        return $this->beforeReturnEvent($event, $request, $response);
338
    }
339
340
    /**
341
     * On sending text-to-speech.
342
     *
343
     * @param SendingTextToSpeechSmsEvent $event The sending text-to-speech event.
344
     * @return SendingTextToSpeechSmsEvent Returns the sending text-to-speech event.
345
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
346
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
347
     * @throws ApiException Throws an API exception if an error occurs.
348
     */
349
    public function onSendingTextToSpeechSms(SendingTextToSpeechSmsEvent $event): SendingTextToSpeechSmsEvent {
350
351
        $this->beforeHandleEvent();
352
353
        $request  = RequestFactory::newSendingTextToSpeechSmsRequest($event->getSendingTextToSpeechSms());
354
        $response = $this->getApiProvider()->sendingTextToSpeechSms($request);
355
356
        return $this->beforeReturnEvent($event, $request, $response);
357
    }
358
359
    /**
360
     * On sending unicode SMS.
361
     *
362
     * @param SendingUnicodeSmsEvent $event The sending unicode SMS event.
363
     * @return SendingUnicodeSmsEvent Returns the sending unicode SMS event.
364
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
365
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
366
     * @throws ApiException Throws an API exception if an error occurs.
367
     */
368
    public function onSendingUnicodeSms(SendingUnicodeSmsEvent $event): SendingUnicodeSmsEvent {
369
370
        $this->beforeHandleEvent();
371
372
        $request  = RequestFactory::newSendingUnicodeSmsRequest($event->getSendingUnicodeSms());
373
        $response = $this->getApiProvider()->sendingUnicodeSms($request);
374
375
        return $this->beforeReturnEvent($event, $request, $response);
376
    }
377
378
    /**
379
     * On sent SMS message lis.
380
     *
381
     * @param SentSmsMessageListEvent $event The sent SMS message list event.
382
     * @return SentSmsMessageListEvent Returns the sent SMS message list event.
383
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
384
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
385
     * @throws ApiException Throws an API exception if an error occurs.
386
     */
387
    public function onSentSmsMessageList(SentSmsMessageListEvent $event): SentSmsMessageListEvent {
388
389
        $this->beforeHandleEvent();
390
391
        $request  = RequestFactory::newSentSmsMessageListRequest($event->getSentSmsMessageList());
392
        $response = $this->getApiProvider()->sentSmsMessageList($request);
393
394
        return $this->beforeReturnEvent($event, $request, $response);
395
    }
396
397
    /**
398
     * On transferring credits.
399
     *
400
     * @param TransferringCreditsEvent $event The transferring credits event.
401
     * @return TransferringCreditsEvent Returns the transferring credits event.
402
     * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing.
403
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
404
     * @throws ApiException Throws an API exception if an error occurs.
405
     */
406
    public function onTransferringCredits(TransferringCreditsEvent $event): TransferringCreditsEvent {
407
408
        $this->beforeHandleEvent();
409
410
        $request  = RequestFactory::newTransferringCreditsRequest($event->getTransferringCredits());
411
        $response = $this->getApiProvider()->transferringCredits($request);
412
413
        return $this->beforeReturnEvent($event, $request, $response);
414
    }
415
416
    /**
417
     * Set the access token.
418
     *
419
     * @param string|null $accessToken The access token.
420
     * @return DefaultEventListener Returns this event listener.
421
     */
422
    public function setAccessToken(?string $accessToken): DefaultEventListener {
423
        $this->getApiProvider()->getAuthentication()->setAccessToken($accessToken);
424
        return $this;
425
    }
426
427
    /**
428
     * Set the API provider.
429
     *
430
     * @param ApiProvider $apiProvider The API provider.
431
     * @return DefaultEventListener Returns this event listener.
432
     */
433
    protected function setApiProvider(ApiProvider $apiProvider): DefaultEventListener {
434
        $this->apiProvider = $apiProvider;
435
        return $this;
436
    }
437
438
    /**
439
     * Set the pass.
440
     *
441
     * @param string|null $pass The pass.
442
     * @return DefaultEventListener Returns this event listener.
443
     */
444
    public function setPass(?string $pass): DefaultEventListener {
445
        $this->getApiProvider()->getAuthentication()->setPass($pass);
446
        return $this;
447
    }
448
449
    /**
450
     * Set the pseudo.
451
     *
452
     * @param string|null $pseudo The pseudo.
453
     * @return DefaultEventListener Returns this event listener.
454
     */
455
    public function setPseudo(?string $pseudo): DefaultEventListener {
456
        $this->getApiProvider()->getAuthentication()->setPseudo($pseudo);
457
        return $this;
458
    }
459
}
460