Passed
Push — master ( 88dbe1...356de5 )
by Nikolay
02:35
created

BotApi::sendVenue()   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\ResponseException;
14
use TgBotApi\BotApiBase\Method\AddStickerToSetMethod;
15
use TgBotApi\BotApiBase\Method\AnswerCallbackQueryMethod;
16
use TgBotApi\BotApiBase\Method\AnswerInlineQueryMethod;
17
use TgBotApi\BotApiBase\Method\ForwardMessageMethod;
18
use TgBotApi\BotApiBase\Method\GetChatAdministratorsMethod;
19
use TgBotApi\BotApiBase\Method\GetChatMemberMethod;
20
use TgBotApi\BotApiBase\Method\GetChatMembersCountMethod;
21
use TgBotApi\BotApiBase\Method\GetChatMethod;
22
use TgBotApi\BotApiBase\Method\GetFileMethod;
23
use TgBotApi\BotApiBase\Method\GetMeMethod;
24
use TgBotApi\BotApiBase\Method\GetUpdatesMethod;
25
use TgBotApi\BotApiBase\Method\GetUserProfilePhotosMethod;
26
use TgBotApi\BotApiBase\Method\GetWebhookInfoMethod;
27
use TgBotApi\BotApiBase\Method\Interfaces\SendMessageInterface;
28
use TgBotApi\BotApiBase\Method\KickChatMemberMethod;
29
use TgBotApi\BotApiBase\Method\LeaveChatMethod;
30
use TgBotApi\BotApiBase\Method\PinChatMessageMethod;
31
use TgBotApi\BotApiBase\Method\PromoteChatMemberMethod;
32
use TgBotApi\BotApiBase\Method\RestrictChatMemberMethod;
33
use TgBotApi\BotApiBase\Method\SendAnimationMethod;
34
use TgBotApi\BotApiBase\Method\SendAudioMethod;
35
use TgBotApi\BotApiBase\Method\SendChatActionMethod;
36
use TgBotApi\BotApiBase\Method\SendContactMethod;
37
use TgBotApi\BotApiBase\Method\SendDocumentMethod;
38
use TgBotApi\BotApiBase\Method\SendGameMethod;
39
use TgBotApi\BotApiBase\Method\SendInvoiceMethod;
40
use TgBotApi\BotApiBase\Method\SendLocationMethod;
41
use TgBotApi\BotApiBase\Method\SendMediaGroupMethod;
42
use TgBotApi\BotApiBase\Method\SendMessageMethod;
43
use TgBotApi\BotApiBase\Method\SendPhotoMethod;
44
use TgBotApi\BotApiBase\Method\SendStickerMethod;
45
use TgBotApi\BotApiBase\Method\SendVenueMethod;
46
use TgBotApi\BotApiBase\Method\SendVideoMethod;
47
use TgBotApi\BotApiBase\Method\SendVideoNoteMethod;
48
use TgBotApi\BotApiBase\Method\SendVoiceMethod;
49
use TgBotApi\BotApiBase\Method\SetChatDescriptionMethod;
50
use TgBotApi\BotApiBase\Method\SetChatPhotoMethod;
51
use TgBotApi\BotApiBase\Method\SetChatStickerSetMethod;
52
use TgBotApi\BotApiBase\Method\SetChatTitleMethod;
53
use TgBotApi\BotApiBase\Method\SetGameScoreMethod;
54
use TgBotApi\BotApiBase\Method\SetStickerPositionInSetMethod;
55
use TgBotApi\BotApiBase\Method\SetWebhookMethod;
56
use TgBotApi\BotApiBase\Normalizer\AnswerInlineQueryNormalizer;
57
use TgBotApi\BotApiBase\Normalizer\InputFileNormalizer;
58
use TgBotApi\BotApiBase\Normalizer\InputMediaNormalizer;
59
use TgBotApi\BotApiBase\Normalizer\JsonSerializableNormalizer;
60
use TgBotApi\BotApiBase\Normalizer\MediaGroupNormalizer;
61
use TgBotApi\BotApiBase\Normalizer\UserProfilePhotosNormalizer;
62
use TgBotApi\BotApiBase\Type\ChatMemberType;
63
use TgBotApi\BotApiBase\Type\ChatType;
64
use TgBotApi\BotApiBase\Type\FileType;
65
use TgBotApi\BotApiBase\Type\MessageType;
66
use TgBotApi\BotApiBase\Type\UpdateType;
67
use TgBotApi\BotApiBase\Type\UserProfilePhotosType;
68
use TgBotApi\BotApiBase\Type\UserType;
69
use TgBotApi\BotApiBase\Type\WebhookInfoType;
70
71
/**
72
 * Class BotApi.
73
 */
74
class BotApi implements BotApiInterface
75
{
76
    /**
77
     * @var string
78
     */
79
    private $botKey;
80
81
    /**
82
     * @var ApiClientInterface
83
     */
84
    private $apiClient;
85
86
    /**
87
     * @var string
88
     */
89
    private $endPoint;
90
91
    /**
92
     * BotApi constructor.
93
     *
94
     * @param string             $botKey
95
     * @param ApiClientInterface $apiClient
96
     * @param string             $endPoint
97
     */
98 46
    public function __construct(
99
        string $botKey,
100
        ApiClientInterface $apiClient,
101
        string $endPoint = 'https://api.telegram.org'
102
    ) {
103 46
        $this->botKey = $botKey;
104 46
        $this->apiClient = $apiClient;
105 46
        $this->endPoint = $endPoint;
106
107 46
        $this->apiClient->setBotKey($botKey);
108 46
        $this->apiClient->setEndpoint($endPoint);
109 46
    }
110
111
    /**
112
     * @param $method
113
     * @param $type
114
     *
115
     * @throws ResponseException
116
     *
117
     * @return mixed
118
     */
119 46
    public function call($method, $type = null)
120
    {
121 46
        list($data, $files) = $this->encode($method);
122
123 46
        $json = $this->apiClient->send($this->getMethodName($method), $data, $files);
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type boolean and double and integer and string; however, parameter $data of TgBotApi\BotApiBase\ApiClientInterface::send() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
        $json = $this->apiClient->send($this->getMethodName($method), /** @scrutinizer ignore-type */ $data, $files);
Loading history...
124
125 46
        if (true !== $json->ok) {
126
            throw new ResponseException($json->description);
127
        }
128
129 46
        return $type ? $this->denormalize($json, $type) : $json->result;
130
    }
131
132
    /**
133
     * @param GetUpdatesMethod $method
134
     *
135
     * @throws ResponseException
136
     *
137
     * @return UpdateType[]
138
     */
139 2
    public function getUpdates(GetUpdatesMethod $method): array
140
    {
141 2
        return $this->call($method, UpdateType::class . '[]');
142
    }
143
144
    /**
145
     * @param GetMeMethod $method
146
     *
147
     * @throws ResponseException
148
     *
149
     * @return UserType
150
     */
151 2
    public function getMe(GetMeMethod $method): UserType
152
    {
153 2
        return $this->call($method, UserType::class);
154
    }
155
156
    /**
157
     * @param SendMessageMethod $method
158
     *
159
     * @throws ResponseException
160
     *
161
     * @return MessageType
162
     */
163 2
    public function sendMessage(SendMessageMethod $method): MessageType
164
    {
165 2
        return $this->call($method, MessageType::class);
166
    }
167
168
    /**
169
     * @param ForwardMessageMethod $method
170
     *
171
     * @throws ResponseException
172
     *
173
     * @return MessageType
174
     */
175 1
    public function sendForwardMessage(ForwardMessageMethod $method): MessageType
176
    {
177 1
        return $this->call($method, MessageType::class);
178
    }
179
180
    /**
181
     * @param SendPhotoMethod $method
182
     *
183
     * @throws ResponseException
184
     *
185
     * @return MessageType
186
     */
187 2
    public function sendPhoto(SendPhotoMethod $method): MessageType
188
    {
189 2
        return $this->call($method, MessageType::class);
190
    }
191
192
    /**
193
     * @param SendAudioMethod $method
194
     *
195
     * @throws ResponseException
196
     *
197
     * @return MessageType
198
     */
199 2
    public function sendAudio(SendAudioMethod $method): MessageType
200
    {
201 2
        return $this->call($method, MessageType::class);
202
    }
203
204
    /**
205
     * @param SendDocumentMethod $method
206
     *
207
     * @throws ResponseException
208
     *
209
     * @return MessageType
210
     */
211 2
    public function sendDocument(SendDocumentMethod $method): MessageType
212
    {
213 2
        return $this->call($method, MessageType::class);
214
    }
215
216
    /**
217
     * @param SendVideoMethod $method
218
     *
219
     * @throws ResponseException
220
     *
221
     * @return MessageType
222
     */
223 2
    public function sendVideo(SendVideoMethod $method): MessageType
224
    {
225 2
        return $this->call($method, MessageType::class);
226
    }
227
228
    /**
229
     * @param SendAnimationMethod $method
230
     *
231
     * @throws ResponseException
232
     *
233
     * @return MessageType
234
     */
235 2
    public function sendAnimation(SendAnimationMethod $method): MessageType
236
    {
237 2
        return $this->call($method, MessageType::class);
238
    }
239
240
    /**
241
     * @param SendVoiceMethod $method
242
     *
243
     * @throws ResponseException
244
     *
245
     * @return MessageType
246
     */
247 2
    public function sendVoice(SendVoiceMethod $method): MessageType
248
    {
249 2
        return $this->call($method, MessageType::class);
250
    }
251
252
    /**
253
     * @param SendVideoNoteMethod $method
254
     *
255
     * @throws ResponseException
256
     *
257
     * @return MessageType
258
     */
259 2
    public function sendVideoNote(SendVideoNoteMethod $method): MessageType
260
    {
261 2
        return $this->call($method, MessageType::class);
262
    }
263
264
    /**
265
     * @param SendMediaGroupMethod $method
266
     *
267
     * @throws ResponseException
268
     *
269
     * @return MessageType[]
270
     */
271 2
    public function sendMediaGroup(SendMediaGroupMethod $method): array
272
    {
273 2
        return $this->call($method, MessageType::class . '[]');
274
    }
275
276
    /**
277
     * @param SendChatActionMethod $method
278
     *
279
     * @throws ResponseException
280
     *
281
     * @return bool
282
     */
283 1
    public function sendChatAction(SendChatActionMethod $method): bool
284
    {
285 1
        return $this->call($method);
286
    }
287
288
    /**
289
     * @param SendGameMethod $method
290
     *
291
     * @throws ResponseException
292
     *
293
     * @return MessageType
294
     */
295 1
    public function sendGame(SendGameMethod $method): MessageType
296
    {
297 1
        return $this->call($method, MessageType::class);
298
    }
299
300
    /**
301
     * @param SendInvoiceMethod $method
302
     *
303
     * @throws ResponseException
304
     *
305
     * @return MessageType
306
     */
307 1
    public function sendInvoice(SendInvoiceMethod $method): MessageType
308
    {
309 1
        return $this->call($method, MessageType::class);
310
    }
311
312
    /**
313
     * @param SendLocationMethod $method
314
     *
315
     * @throws ResponseException
316
     *
317
     * @return MessageType
318
     */
319 2
    public function sendLocation(SendLocationMethod $method): MessageType
320
    {
321 2
        return $this->call($method, MessageType::class);
322
    }
323
324
    /**
325
     * @param SendVenueMethod $method
326
     *
327
     * @throws ResponseException
328
     *
329
     * @return MessageType
330
     */
331 2
    public function sendVenue(SendVenueMethod $method): MessageType
332
    {
333 2
        return $this->call($method, MessageType::class);
334
    }
335
336
    /**
337
     * @param SendContactMethod $method
338
     *
339
     * @throws ResponseException
340
     *
341
     * @return MessageType
342
     */
343 2
    public function sendContact(SendContactMethod $method): MessageType
344
    {
345 2
        return $this->call($method, MessageType::class);
346
    }
347
348
    /**
349
     * @param SendMessageInterface $method
350
     *
351
     * @throws ResponseException
352
     *
353
     * @return MessageType
354
     */
355 15
    public function send(SendMessageInterface $method): MessageType
356
    {
357 15
        return $this->call($method, MessageType::class);
358
    }
359
360
    /**
361
     * @param GetUserProfilePhotosMethod $method
362
     *
363
     * @throws ResponseException
364
     *
365
     * @return UserProfilePhotosType
366
     */
367 2
    public function getUserProfilePhotos(GetUserProfilePhotosMethod $method): UserProfilePhotosType
368
    {
369 2
        return $this->call($method, UserProfilePhotosType::class);
370
    }
371
372
    /**
373
     * @param GetWebhookInfoMethod $method
374
     *
375
     * @throws ResponseException
376
     *
377
     * @return WebhookInfoType
378
     */
379 1
    public function getWebhookInfo(GetWebhookInfoMethod $method): WebhookInfoType
380
    {
381 1
        return $this->call($method, WebhookInfoType::class);
382
    }
383
384
    /**
385
     * @param LeaveChatMethod $method
386
     *
387
     * @throws ResponseException
388
     *
389
     * @return bool
390
     */
391 1
    public function leaveChat(LeaveChatMethod $method): bool
392
    {
393 1
        return $this->call($method);
394
    }
395
396
    /**
397
     * @param PinChatMessageMethod $method
398
     *
399
     * @throws ResponseException
400
     *
401
     * @return bool
402
     */
403 1
    public function pinChatMessage(PinChatMessageMethod $method): bool
404
    {
405 1
        return $this->call($method);
406
    }
407
408
    /**
409
     * @param SendStickerMethod $method
410
     *
411
     * @throws ResponseException
412
     *
413
     * @return MessageType
414
     */
415 1
    public function sendSticker(SendStickerMethod $method): MessageType
416
    {
417 1
        return $this->call($method, MessageType::class);
418
    }
419
420
    /**
421
     * @param PromoteChatMemberMethod $method
422
     *
423
     * @throws ResponseException
424
     *
425
     * @return bool
426
     */
427 1
    public function promoteChatMember(PromoteChatMemberMethod $method): bool
428
    {
429 1
        return $this->call($method);
430
    }
431
432
    /**
433
     * @param RestrictChatMemberMethod $method
434
     *
435
     * @throws ResponseException
436
     *
437
     * @return bool
438
     */
439 1
    public function restrictChatMember(RestrictChatMemberMethod $method): bool
440
    {
441 1
        return $this->call($method);
442
    }
443
444
    /**
445
     * @todo fix this is bad
446
     *
447
     * @param GetFileMethod $method
448
     *
449
     * @throws ResponseException
450
     *
451
     * @return FileType
452
     */
453 2
    public function getFile(GetFileMethod $method): FileType
454
    {
455 2
        return $this->call($method, FileType::class);
456
    }
457
458
    /**
459
     * @param FileType $file
460
     *
461
     * @return string
462
     */
463
    public function getAbsoluteFilePath(FileType $file): string
464
    {
465
        return \sprintf('%s/file/bot%s/%s', $this->endPoint, $this->botKey, $file->filePath);
466
    }
467
468
    /**
469
     * @param GetChatMethod $method
470
     *
471
     * @throws ResponseException
472
     *
473
     * @return ChatType
474
     */
475 2
    public function getChat(GetChatMethod $method): ChatType
476
    {
477 2
        return $this->call($method, ChatType::class);
478
    }
479
480
    /**
481
     * @param GetChatAdministratorsMethod $method
482
     *
483
     * @throws ResponseException
484
     *
485
     * @return ChatMemberType[]
486
     */
487 2
    public function getChatAdministrators(GetChatAdministratorsMethod $method): array
488
    {
489 2
        return $this->call($method, ChatMemberType::class . '[]');
490
    }
491
492
    /**
493
     * @param GetChatMemberMethod $method
494
     *
495
     * @throws ResponseException
496
     *
497
     * @return ChatMemberType
498
     */
499 2
    public function getChatMember(GetChatMemberMethod $method): ChatMemberType
500
    {
501 2
        return $this->call($method, ChatMemberType::class);
502
    }
503
504
    /**
505
     * @param KickChatMemberMethod $method
506
     *
507
     * @throws ResponseException
508
     *
509
     * @return bool
510
     */
511 1
    public function kickChatMember(KickChatMemberMethod $method): bool
512
    {
513 1
        return $this->call($method);
514
    }
515
516
    /**
517
     * @param SetChatDescriptionMethod $method
518
     *
519
     * @throws ResponseException
520
     *
521
     * @return bool
522
     */
523 1
    public function setChatDescription(SetChatDescriptionMethod $method): bool
524
    {
525 1
        return $this->call($method);
526
    }
527
528
    /**
529
     * @param SetChatPhotoMethod $method
530
     *
531
     * @throws ResponseException
532
     *
533
     * @return bool
534
     */
535 1
    public function setChatPhoto(SetChatPhotoMethod $method): bool
536
    {
537 1
        return $this->call($method);
538
    }
539
540
    /**
541
     * @param SetChatStickerSetMethod $method
542
     *
543
     * @throws ResponseException
544
     *
545
     * @return bool
546
     */
547 1
    public function setChatStickerSet(SetChatStickerSetMethod $method): bool
548
    {
549 1
        return $this->call($method);
550
    }
551
552
    /**
553
     * @param SetChatTitleMethod $method
554
     *
555
     * @throws ResponseException
556
     *
557
     * @return bool
558
     */
559 1
    public function setChatTitle(SetChatTitleMethod $method): bool
560
    {
561 1
        return $this->call($method);
562
    }
563
564
    /**
565
     * @param SetGameScoreMethod $method
566
     *
567
     * @throws ResponseException
568
     *
569
     * @return bool
570
     */
571 2
    public function setGameScore(SetGameScoreMethod $method): bool
572
    {
573 2
        return $this->call($method);
574
    }
575
576
    /**
577
     * @param SetStickerPositionInSetMethod $method
578
     *
579
     * @throws ResponseException
580
     *
581
     * @return bool
582
     */
583 1
    public function setStickerPositionInSet(SetStickerPositionInSetMethod $method): bool
584
    {
585 1
        return $this->call($method);
586
    }
587
588
    /**
589
     * @param SetWebhookMethod $method
590
     *
591
     * @throws ResponseException
592
     *
593
     * @return bool
594
     */
595 1
    public function setWebhook(SetWebhookMethod $method): bool
596
    {
597 1
        return $this->call($method);
598
    }
599
600
    /**
601
     * @param AddStickerToSetMethod $method
602
     *
603
     * @throws ResponseException
604
     *
605
     * @return bool
606
     */
607 1
    public function addStickerToSet(AddStickerToSetMethod $method): bool
608
    {
609 1
        return $this->call($method);
610
    }
611
612
    /**
613
     * @param GetChatMembersCountMethod $method
614
     *
615
     * @throws ResponseException
616
     *
617
     * @return int
618
     */
619 1
    public function getChatMembersCount(GetChatMembersCountMethod $method): int
620
    {
621 1
        return $this->call($method);
622
    }
623
624
//    public function answerInlineQuery(AnswerInlineQueryMethod $method)
625
//    {
626
//        return $this->call($method, '');
627
//    }
628
629
    /**
630
     * @param AnswerCallbackQueryMethod $method
631
     *
632
     * @throws ResponseException
633
     *
634
     * @return bool
635
     */
636 3
    public function answerCallbackQuery(AnswerCallbackQueryMethod $method): bool
637
    {
638 3
        return $this->call($method);
639
    }
640
641
    /**
642
     * @param AnswerInlineQueryMethod $method
643
     *
644
     * @throws ResponseException
645
     *
646
     * @return bool
647
     */
648 2
    public function answerInlineQuery(AnswerInlineQueryMethod $method): bool
649
    {
650 2
        return $this->call($method);
651
    }
652
653
    /**
654
     * @param $method
655
     *
656
     * @return string
657
     */
658 46
    private function getMethodName($method): string
659
    {
660 46
        return \lcfirst(\substr(
661 46
            \get_class($method),
662 46
            \strrpos(\get_class($method), '\\') + 1,
663 46
            -1 * \strlen('Method')
664
        ));
665
    }
666
667
    /**
668
     * @param $data
669
     * @param $type
670
     *
671
     * @return object|array
672
     */
673 25
    private function denormalize($data, $type)
674
    {
675 25
        $normalizer = new ObjectNormalizer(
676 25
            null,
677 25
            new CamelCaseToSnakeCaseNameConverter(),
678 25
            null,
679 25
            new PhpDocExtractor()
680
        );
681 25
        $arrayNormalizer = new ArrayDenormalizer();
682 25
        $serializer = new Serializer([
683 25
            new UserProfilePhotosNormalizer($normalizer, $arrayNormalizer),
684 25
            new DateTimeNormalizer(),
685 25
            $normalizer,
686 25
            $arrayNormalizer,
687
        ]);
688
689 25
        return $serializer->denormalize($data->result, $type, null, [DateTimeNormalizer::FORMAT_KEY => 'U']);
690
    }
691
692
    /**
693
     * @param $method
694
     *
695
     * @return array[]
696
     */
697 46
    private function encode($method): array
698
    {
699 46
        $files = [];
700
701 46
        $objectNormalizer = new ObjectNormalizer(null, new CamelCaseToSnakeCaseNameConverter());
702 46
        $serializer = new Serializer([
703 46
            new InputFileNormalizer($files),
704 46
            new MediaGroupNormalizer(new InputMediaNormalizer($objectNormalizer, $files), $objectNormalizer),
705 46
            new JsonSerializableNormalizer($objectNormalizer),
706 46
            new AnswerInlineQueryNormalizer($objectNormalizer),
707 46
            new DateTimeNormalizer(),
708 46
            $objectNormalizer,
709
        ]);
710
711 46
        $data = $serializer->normalize(
712 46
            $method,
713 46
            null,
714 46
            ['skip_null_values' => true, DateTimeNormalizer::FORMAT_KEY => 'U']
715
        );
716
717 46
        return [$data, $files];
718
    }
719
}
720