Passed
Push — master ( 356de5...45bce2 )
by Nikolay
03:54
created

BotApi::sendVideoNote()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TgBotApi\BotApiBase;
6
7
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
8
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
9
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
10
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
11
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
12
use Symfony\Component\Serializer\Serializer;
13
use TgBotApi\BotApiBase\Exception\NormalizationException;
14
use TgBotApi\BotApiBase\Exception\ResponseException;
15
use TgBotApi\BotApiBase\Method\AddStickerToSetMethod;
16
use TgBotApi\BotApiBase\Method\AnswerCallbackQueryMethod;
17
use TgBotApi\BotApiBase\Method\AnswerInlineQueryMethod;
18
use TgBotApi\BotApiBase\Method\ForwardMessageMethod;
19
use TgBotApi\BotApiBase\Method\GetChatAdministratorsMethod;
20
use TgBotApi\BotApiBase\Method\GetChatMemberMethod;
21
use TgBotApi\BotApiBase\Method\GetChatMembersCountMethod;
22
use TgBotApi\BotApiBase\Method\GetChatMethod;
23
use TgBotApi\BotApiBase\Method\GetFileMethod;
24
use TgBotApi\BotApiBase\Method\GetMeMethod;
25
use TgBotApi\BotApiBase\Method\GetUpdatesMethod;
26
use TgBotApi\BotApiBase\Method\GetUserProfilePhotosMethod;
27
use TgBotApi\BotApiBase\Method\GetWebhookInfoMethod;
28
use TgBotApi\BotApiBase\Method\Interfaces\SendMessageInterface;
29
use TgBotApi\BotApiBase\Method\KickChatMemberMethod;
30
use TgBotApi\BotApiBase\Method\LeaveChatMethod;
31
use TgBotApi\BotApiBase\Method\PinChatMessageMethod;
32
use TgBotApi\BotApiBase\Method\PromoteChatMemberMethod;
33
use TgBotApi\BotApiBase\Method\RestrictChatMemberMethod;
34
use TgBotApi\BotApiBase\Method\SendAnimationMethod;
35
use TgBotApi\BotApiBase\Method\SendAudioMethod;
36
use TgBotApi\BotApiBase\Method\SendChatActionMethod;
37
use TgBotApi\BotApiBase\Method\SendContactMethod;
38
use TgBotApi\BotApiBase\Method\SendDocumentMethod;
39
use TgBotApi\BotApiBase\Method\SendGameMethod;
40
use TgBotApi\BotApiBase\Method\SendInvoiceMethod;
41
use TgBotApi\BotApiBase\Method\SendLocationMethod;
42
use TgBotApi\BotApiBase\Method\SendMediaGroupMethod;
43
use TgBotApi\BotApiBase\Method\SendMessageMethod;
44
use TgBotApi\BotApiBase\Method\SendPhotoMethod;
45
use TgBotApi\BotApiBase\Method\SendStickerMethod;
46
use TgBotApi\BotApiBase\Method\SendVenueMethod;
47
use TgBotApi\BotApiBase\Method\SendVideoMethod;
48
use TgBotApi\BotApiBase\Method\SendVideoNoteMethod;
49
use TgBotApi\BotApiBase\Method\SendVoiceMethod;
50
use TgBotApi\BotApiBase\Method\SetChatDescriptionMethod;
51
use TgBotApi\BotApiBase\Method\SetChatPhotoMethod;
52
use TgBotApi\BotApiBase\Method\SetChatStickerSetMethod;
53
use TgBotApi\BotApiBase\Method\SetChatTitleMethod;
54
use TgBotApi\BotApiBase\Method\SetGameScoreMethod;
55
use TgBotApi\BotApiBase\Method\SetStickerPositionInSetMethod;
56
use TgBotApi\BotApiBase\Method\SetWebhookMethod;
57
use TgBotApi\BotApiBase\Normalizer\AnswerInlineQueryNormalizer;
58
use TgBotApi\BotApiBase\Normalizer\InputFileNormalizer;
59
use TgBotApi\BotApiBase\Normalizer\InputMediaNormalizer;
60
use TgBotApi\BotApiBase\Normalizer\JsonSerializableNormalizer;
61
use TgBotApi\BotApiBase\Normalizer\MediaGroupNormalizer;
62
use TgBotApi\BotApiBase\Normalizer\UserProfilePhotosNormalizer;
63
use TgBotApi\BotApiBase\Type\ChatMemberType;
64
use TgBotApi\BotApiBase\Type\ChatType;
65
use TgBotApi\BotApiBase\Type\FileType;
66
use TgBotApi\BotApiBase\Type\MessageType;
67
use TgBotApi\BotApiBase\Type\UpdateType;
68
use TgBotApi\BotApiBase\Type\UserProfilePhotosType;
69
use TgBotApi\BotApiBase\Type\UserType;
70
use TgBotApi\BotApiBase\Type\WebhookInfoType;
71
72
/**
73
 * Class BotApi.
74
 */
75
class BotApi implements BotApiInterface
76
{
77
    /**
78
     * @var string
79
     */
80
    private $botKey;
81
82
    /**
83
     * @var ApiClientInterface
84
     */
85
    private $apiClient;
86
87
    /**
88
     * @var string
89
     */
90
    private $endPoint;
91
92
    /**
93
     * BotApi constructor.
94
     *
95
     * @param string             $botKey
96
     * @param ApiClientInterface $apiClient
97
     * @param string             $endPoint
98
     */
99 46
    public function __construct(
100
        string $botKey,
101
        ApiClientInterface $apiClient,
102
        string $endPoint = 'https://api.telegram.org'
103
    ) {
104 46
        $this->botKey = $botKey;
105 46
        $this->apiClient = $apiClient;
106 46
        $this->endPoint = $endPoint;
107
108 46
        $this->apiClient->setBotKey($botKey);
109 46
        $this->apiClient->setEndpoint($endPoint);
110 46
    }
111
112
    /**
113
     * @param $method
114
     * @param $type
115
     *
116
     * @throws ResponseException
117
     * @throws NormalizationException
118
     *
119
     * @return mixed
120
     */
121 46
    public function call($method, $type = null)
122
    {
123 46
        list($data, $files) = $this->encode($method);
124
125 46
        $json = $this->apiClient->send($this->getMethodName($method), $data, $files);
126
127 46
        if (true !== $json->ok) {
128
            throw new ResponseException($json->description);
129
        }
130
131 46
        return $type ? $this->denormalize($json, $type) : $json->result;
132
    }
133
134
    /**
135
     * @param GetUpdatesMethod $method
136
     *
137
     * @throws ResponseException
138
     * @throws NormalizationException
139
     *
140
     * @return UpdateType[]
141
     */
142 2
    public function getUpdates(GetUpdatesMethod $method): array
143
    {
144 2
        return $this->call($method, UpdateType::class . '[]');
145
    }
146
147
    /**
148
     * @param GetMeMethod $method
149
     *
150
     * @throws ResponseException
151
     * @throws NormalizationException
152
     *
153
     * @return UserType
154
     */
155 2
    public function getMe(GetMeMethod $method): UserType
156
    {
157 2
        return $this->call($method, UserType::class);
158
    }
159
160
    /**
161
     * @param SendMessageMethod $method
162
     *
163
     * @throws ResponseException
164
     * @throws NormalizationException
165
     *
166
     * @return MessageType
167
     */
168 2
    public function sendMessage(SendMessageMethod $method): MessageType
169
    {
170 2
        return $this->call($method, MessageType::class);
171
    }
172
173
    /**
174
     * @param ForwardMessageMethod $method
175
     *
176
     * @throws ResponseException
177
     * @throws NormalizationException
178
     *
179
     * @return MessageType
180
     */
181 1
    public function sendForwardMessage(ForwardMessageMethod $method): MessageType
182
    {
183 1
        return $this->call($method, MessageType::class);
184
    }
185
186
    /**
187
     * @param SendPhotoMethod $method
188
     *
189
     * @throws ResponseException
190
     * @throws NormalizationException
191
     *
192
     * @return MessageType
193
     */
194 2
    public function sendPhoto(SendPhotoMethod $method): MessageType
195
    {
196 2
        return $this->call($method, MessageType::class);
197
    }
198
199
    /**
200
     * @param SendAudioMethod $method
201
     *
202
     * @throws ResponseException
203
     * @throws NormalizationException
204
     *
205
     * @return MessageType
206
     */
207 2
    public function sendAudio(SendAudioMethod $method): MessageType
208
    {
209 2
        return $this->call($method, MessageType::class);
210
    }
211
212
    /**
213
     * @param SendDocumentMethod $method
214
     *
215
     * @throws ResponseException
216
     * @throws NormalizationException
217
     *
218
     * @return MessageType
219
     */
220 2
    public function sendDocument(SendDocumentMethod $method): MessageType
221
    {
222 2
        return $this->call($method, MessageType::class);
223
    }
224
225
    /**
226
     * @param SendVideoMethod $method
227
     *
228
     * @throws ResponseException
229
     * @throws NormalizationException
230
     *
231
     * @return MessageType
232
     */
233 2
    public function sendVideo(SendVideoMethod $method): MessageType
234
    {
235 2
        return $this->call($method, MessageType::class);
236
    }
237
238
    /**
239
     * @param SendAnimationMethod $method
240
     *
241
     * @throws ResponseException
242
     * @throws NormalizationException
243
     *
244
     * @return MessageType
245
     */
246 2
    public function sendAnimation(SendAnimationMethod $method): MessageType
247
    {
248 2
        return $this->call($method, MessageType::class);
249
    }
250
251
    /**
252
     * @param SendVoiceMethod $method
253
     *
254
     * @throws ResponseException
255
     * @throws NormalizationException
256
     *
257
     * @return MessageType
258
     */
259 2
    public function sendVoice(SendVoiceMethod $method): MessageType
260
    {
261 2
        return $this->call($method, MessageType::class);
262
    }
263
264
    /**
265
     * @param SendVideoNoteMethod $method
266
     *
267
     * @throws ResponseException
268
     * @throws NormalizationException
269
     *
270
     * @return MessageType
271
     */
272 2
    public function sendVideoNote(SendVideoNoteMethod $method): MessageType
273
    {
274 2
        return $this->call($method, MessageType::class);
275
    }
276
277
    /**
278
     * @param SendMediaGroupMethod $method
279
     *
280
     * @throws ResponseException
281
     * @throws NormalizationException
282
     *
283
     * @return MessageType[]
284
     */
285 2
    public function sendMediaGroup(SendMediaGroupMethod $method): array
286
    {
287 2
        return $this->call($method, MessageType::class . '[]');
288
    }
289
290
    /**
291
     * @param SendChatActionMethod $method
292
     *
293
     * @throws ResponseException
294
     * @throws NormalizationException
295
     *
296
     * @return bool
297
     */
298 1
    public function sendChatAction(SendChatActionMethod $method): bool
299
    {
300 1
        return $this->call($method);
301
    }
302
303
    /**
304
     * @param SendGameMethod $method
305
     *
306
     * @throws ResponseException
307
     * @throws NormalizationException
308
     *
309
     * @return MessageType
310
     */
311 1
    public function sendGame(SendGameMethod $method): MessageType
312
    {
313 1
        return $this->call($method, MessageType::class);
314
    }
315
316
    /**
317
     * @param SendInvoiceMethod $method
318
     *
319
     * @throws ResponseException
320
     * @throws NormalizationException
321
     *
322
     * @return MessageType
323
     */
324 1
    public function sendInvoice(SendInvoiceMethod $method): MessageType
325
    {
326 1
        return $this->call($method, MessageType::class);
327
    }
328
329
    /**
330
     * @param SendLocationMethod $method
331
     *
332
     * @throws ResponseException
333
     * @throws NormalizationException
334
     *
335
     * @return MessageType
336
     */
337 2
    public function sendLocation(SendLocationMethod $method): MessageType
338
    {
339 2
        return $this->call($method, MessageType::class);
340
    }
341
342
    /**
343
     * @param SendVenueMethod $method
344
     *
345
     * @throws ResponseException
346
     * @throws NormalizationException
347
     *
348
     * @return MessageType
349
     */
350 2
    public function sendVenue(SendVenueMethod $method): MessageType
351
    {
352 2
        return $this->call($method, MessageType::class);
353
    }
354
355
    /**
356
     * @param SendContactMethod $method
357
     *
358
     * @throws ResponseException
359
     * @throws NormalizationException
360
     *
361
     * @return MessageType
362
     */
363 2
    public function sendContact(SendContactMethod $method): MessageType
364
    {
365 2
        return $this->call($method, MessageType::class);
366
    }
367
368
    /**
369
     * @param SendMessageInterface $method
370
     *
371
     * @throws ResponseException
372
     * @throws NormalizationException
373
     *
374
     * @return MessageType
375
     */
376 15
    public function send(SendMessageInterface $method): MessageType
377
    {
378 15
        return $this->call($method, MessageType::class);
379
    }
380
381
    /**
382
     * @param GetUserProfilePhotosMethod $method
383
     *
384
     * @throws ResponseException
385
     * @throws NormalizationException
386
     *
387
     * @return UserProfilePhotosType
388
     */
389 2
    public function getUserProfilePhotos(GetUserProfilePhotosMethod $method): UserProfilePhotosType
390
    {
391 2
        return $this->call($method, UserProfilePhotosType::class);
392
    }
393
394
    /**
395
     * @param GetWebhookInfoMethod $method
396
     *
397
     * @throws ResponseException
398
     * @throws NormalizationException
399
     *
400
     * @return WebhookInfoType
401
     */
402 1
    public function getWebhookInfo(GetWebhookInfoMethod $method): WebhookInfoType
403
    {
404 1
        return $this->call($method, WebhookInfoType::class);
405
    }
406
407
    /**
408
     * @param LeaveChatMethod $method
409
     *
410
     * @throws ResponseException
411
     * @throws NormalizationException
412
     *
413
     * @return bool
414
     */
415 1
    public function leaveChat(LeaveChatMethod $method): bool
416
    {
417 1
        return $this->call($method);
418
    }
419
420
    /**
421
     * @param PinChatMessageMethod $method
422
     *
423
     * @throws ResponseException
424
     * @throws NormalizationException
425
     *
426
     * @return bool
427
     */
428 1
    public function pinChatMessage(PinChatMessageMethod $method): bool
429
    {
430 1
        return $this->call($method);
431
    }
432
433
    /**
434
     * @param SendStickerMethod $method
435
     *
436
     * @throws ResponseException
437
     * @throws NormalizationException
438
     *
439
     * @return MessageType
440
     */
441 1
    public function sendSticker(SendStickerMethod $method): MessageType
442
    {
443 1
        return $this->call($method, MessageType::class);
444
    }
445
446
    /**
447
     * @param PromoteChatMemberMethod $method
448
     *
449
     * @throws ResponseException
450
     * @throws NormalizationException
451
     *
452
     * @return bool
453
     */
454 1
    public function promoteChatMember(PromoteChatMemberMethod $method): bool
455
    {
456 1
        return $this->call($method);
457
    }
458
459
    /**
460
     * @param RestrictChatMemberMethod $method
461
     *
462
     * @throws ResponseException
463
     * @throws NormalizationException
464
     *
465
     * @return bool
466
     */
467 1
    public function restrictChatMember(RestrictChatMemberMethod $method): bool
468
    {
469 1
        return $this->call($method);
470
    }
471
472
    /**
473
     * @param GetFileMethod $method
474
     *
475
     * @throws ResponseException
476
     * @throws NormalizationException
477
     *
478
     * @return FileType
479
     */
480 2
    public function getFile(GetFileMethod $method): FileType
481
    {
482 2
        return $this->call($method, FileType::class);
483
    }
484
485
    /**
486
     * @param FileType $file
487
     *
488
     * @return string
489
     */
490
    public function getAbsoluteFilePath(FileType $file): string
491
    {
492
        return \sprintf('%s/file/bot%s/%s', $this->endPoint, $this->botKey, $file->filePath);
493
    }
494
495
    /**
496
     * @param GetChatMethod $method
497
     *
498
     * @throws ResponseException
499
     * @throws NormalizationException
500
     *
501
     * @return ChatType
502
     */
503 2
    public function getChat(GetChatMethod $method): ChatType
504
    {
505 2
        return $this->call($method, ChatType::class);
506
    }
507
508
    /**
509
     * @param GetChatAdministratorsMethod $method
510
     *
511
     * @throws ResponseException
512
     * @throws NormalizationException
513
     *
514
     * @return ChatMemberType[]
515
     */
516 2
    public function getChatAdministrators(GetChatAdministratorsMethod $method): array
517
    {
518 2
        return $this->call($method, ChatMemberType::class . '[]');
519
    }
520
521
    /**
522
     * @param GetChatMemberMethod $method
523
     *
524
     * @throws ResponseException
525
     * @throws NormalizationException
526
     *
527
     * @return ChatMemberType
528
     */
529 2
    public function getChatMember(GetChatMemberMethod $method): ChatMemberType
530
    {
531 2
        return $this->call($method, ChatMemberType::class);
532
    }
533
534
    /**
535
     * @param KickChatMemberMethod $method
536
     *
537
     * @throws ResponseException
538
     * @throws NormalizationException
539
     *
540
     * @return bool
541
     */
542 1
    public function kickChatMember(KickChatMemberMethod $method): bool
543
    {
544 1
        return $this->call($method);
545
    }
546
547
    /**
548
     * @param SetChatDescriptionMethod $method
549
     *
550
     * @throws ResponseException
551
     * @throws NormalizationException
552
     *
553
     * @return bool
554
     */
555 1
    public function setChatDescription(SetChatDescriptionMethod $method): bool
556
    {
557 1
        return $this->call($method);
558
    }
559
560
    /**
561
     * @param SetChatPhotoMethod $method
562
     *
563
     * @throws ResponseException
564
     * @throws NormalizationException
565
     *
566
     * @return bool
567
     */
568 1
    public function setChatPhoto(SetChatPhotoMethod $method): bool
569
    {
570 1
        return $this->call($method);
571
    }
572
573
    /**
574
     * @param SetChatStickerSetMethod $method
575
     *
576
     * @throws ResponseException
577
     * @throws NormalizationException
578
     *
579
     * @return bool
580
     */
581 1
    public function setChatStickerSet(SetChatStickerSetMethod $method): bool
582
    {
583 1
        return $this->call($method);
584
    }
585
586
    /**
587
     * @param SetChatTitleMethod $method
588
     *
589
     * @throws ResponseException
590
     * @throws NormalizationException
591
     *
592
     * @return bool
593
     */
594 1
    public function setChatTitle(SetChatTitleMethod $method): bool
595
    {
596 1
        return $this->call($method);
597
    }
598
599
    /**
600
     * @param SetGameScoreMethod $method
601
     *
602
     * @throws ResponseException
603
     * @throws NormalizationException
604
     *
605
     * @return bool
606
     */
607 2
    public function setGameScore(SetGameScoreMethod $method): bool
608
    {
609 2
        return $this->call($method);
610
    }
611
612
    /**
613
     * @param SetStickerPositionInSetMethod $method
614
     *
615
     * @throws ResponseException
616
     * @throws NormalizationException
617
     *
618
     * @return bool
619
     */
620 1
    public function setStickerPositionInSet(SetStickerPositionInSetMethod $method): bool
621
    {
622 1
        return $this->call($method);
623
    }
624
625
    /**
626
     * @param SetWebhookMethod $method
627
     *
628
     * @throws ResponseException
629
     * @throws NormalizationException
630
     *
631
     * @return bool
632
     */
633 1
    public function setWebhook(SetWebhookMethod $method): bool
634
    {
635 1
        return $this->call($method);
636
    }
637
638
    /**
639
     * @param AddStickerToSetMethod $method
640
     *
641
     * @throws ResponseException
642
     * @throws NormalizationException
643
     *
644
     * @return bool
645
     */
646 1
    public function addStickerToSet(AddStickerToSetMethod $method): bool
647
    {
648 1
        return $this->call($method);
649
    }
650
651
    /**
652
     * @param GetChatMembersCountMethod $method
653
     *
654
     * @throws ResponseException
655
     * @throws NormalizationException
656
     *
657
     * @return int
658
     */
659 1
    public function getChatMembersCount(GetChatMembersCountMethod $method): int
660
    {
661 1
        return $this->call($method);
662
    }
663
664
    /**
665
     * @param AnswerCallbackQueryMethod $method
666
     *
667
     * @throws ResponseException
668
     * @throws NormalizationException
669
     *
670
     * @return bool
671
     */
672 3
    public function answerCallbackQuery(AnswerCallbackQueryMethod $method): bool
673
    {
674 3
        return $this->call($method);
675
    }
676
677
    /**
678
     * @param AnswerInlineQueryMethod $method
679
     *
680
     * @throws ResponseException
681
     * @throws NormalizationException
682
     *
683
     * @return bool
684
     */
685 2
    public function answerInlineQuery(AnswerInlineQueryMethod $method): bool
686
    {
687 2
        return $this->call($method);
688
    }
689
690
    /**
691
     * @param $method
692
     *
693
     * @return string
694
     */
695 46
    private function getMethodName($method): string
696
    {
697 46
        return \lcfirst(\substr(
698 46
            \get_class($method),
699 46
            \strrpos(\get_class($method), '\\') + 1,
700 46
            -1 * \strlen('Method')
701
        ));
702
    }
703
704
    /**
705
     * @param $data
706
     * @param $type
707
     *
708
     * @return object|array
709
     */
710 25
    private function denormalize($data, $type)
711
    {
712 25
        $normalizer = new ObjectNormalizer(
713 25
            null,
714 25
            new CamelCaseToSnakeCaseNameConverter(),
715 25
            null,
716 25
            new PhpDocExtractor()
717
        );
718 25
        $arrayNormalizer = new ArrayDenormalizer();
719 25
        $serializer = new Serializer([
720 25
            new UserProfilePhotosNormalizer($normalizer, $arrayNormalizer),
721 25
            new DateTimeNormalizer(),
722 25
            $normalizer,
723 25
            $arrayNormalizer,
724
        ]);
725
726 25
        return $serializer->denormalize($data->result, $type, null, [DateTimeNormalizer::FORMAT_KEY => 'U']);
727
    }
728
729
    /**
730
     * @param $method
731
     *
732
     * @throws NormalizationException
733
     *
734
     * @return array []
735
     */
736 46
    private function encode($method): array
737
    {
738 46
        $files = [];
739
740 46
        $objectNormalizer = new ObjectNormalizer(null, new CamelCaseToSnakeCaseNameConverter());
741 46
        $serializer = new Serializer([
742 46
            new InputFileNormalizer($files),
743 46
            new MediaGroupNormalizer(new InputMediaNormalizer($objectNormalizer, $files), $objectNormalizer),
744 46
            new JsonSerializableNormalizer($objectNormalizer),
745 46
            new AnswerInlineQueryNormalizer($objectNormalizer),
746 46
            new DateTimeNormalizer(),
747 46
            $objectNormalizer,
748
        ]);
749
750 46
        $data = $serializer->normalize(
751 46
            $method,
752 46
            null,
753 46
            ['skip_null_values' => true, DateTimeNormalizer::FORMAT_KEY => 'U']
754
        );
755
756 46
        if (!\is_array($data)) {
757
            throw new NormalizationException('Normalized data must be array');
758
        }
759
760 46
        return [$data, $files];
761
    }
762
}
763