Completed
Push — master ( 4b7fdd...026556 )
by Nikolay
02:45
created

BotApi::promoteChatMember()   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\KickChatMemberMethod;
28
use TgBotApi\BotApiBase\Method\LeaveChatMethod;
29
use TgBotApi\BotApiBase\Method\PinChatMessageMethod;
30
use TgBotApi\BotApiBase\Method\PromoteChatMemberMethod;
31
use TgBotApi\BotApiBase\Method\RestrictChatMemberMethod;
32
use TgBotApi\BotApiBase\Method\SendAnimationMethod;
33
use TgBotApi\BotApiBase\Method\SendAudioMethod;
34
use TgBotApi\BotApiBase\Method\SendChatActionMethod;
35
use TgBotApi\BotApiBase\Method\SendContactMethod;
36
use TgBotApi\BotApiBase\Method\SendDocumentMethod;
37
use TgBotApi\BotApiBase\Method\SendGameMethod;
38
use TgBotApi\BotApiBase\Method\SendInvoiceMethod;
39
use TgBotApi\BotApiBase\Method\SendLocationMethod;
40
use TgBotApi\BotApiBase\Method\SendMediaGroupMethod;
41
use TgBotApi\BotApiBase\Method\SendMessageMethod;
42
use TgBotApi\BotApiBase\Method\SendPhotoMethod;
43
use TgBotApi\BotApiBase\Method\SendStickerMethod;
44
use TgBotApi\BotApiBase\Method\SendVenueMethod;
45
use TgBotApi\BotApiBase\Method\SendVideoMethod;
46
use TgBotApi\BotApiBase\Method\SendVideoNoteMethod;
47
use TgBotApi\BotApiBase\Method\SendVoiceMethod;
48
use TgBotApi\BotApiBase\Method\SetChatDescriptionMethod;
49
use TgBotApi\BotApiBase\Method\SetChatPhotoMethod;
50
use TgBotApi\BotApiBase\Method\SetChatStickerSetMethod;
51
use TgBotApi\BotApiBase\Method\SetChatTitleMethod;
52
use TgBotApi\BotApiBase\Method\SetGameScoreMethod;
53
use TgBotApi\BotApiBase\Method\SetStickerPositionInSetMethod;
54
use TgBotApi\BotApiBase\Method\SetWebhookMethod;
55
use TgBotApi\BotApiBase\Normalizer\AnswerInlineQueryNormalizer;
56
use TgBotApi\BotApiBase\Normalizer\InputFileNormalizer;
57
use TgBotApi\BotApiBase\Normalizer\InputMediaNormalizer;
58
use TgBotApi\BotApiBase\Normalizer\JsonSerializableNormalizer;
59
use TgBotApi\BotApiBase\Normalizer\MediaGroupNormalizer;
60
use TgBotApi\BotApiBase\Normalizer\UserProfilePhotosNormalizer;
61
use TgBotApi\BotApiBase\Type\ChatMemberType;
62
use TgBotApi\BotApiBase\Type\ChatType;
63
use TgBotApi\BotApiBase\Type\FileType;
64
use TgBotApi\BotApiBase\Type\MessageType;
65
use TgBotApi\BotApiBase\Type\UpdateType;
66
use TgBotApi\BotApiBase\Type\UserProfilePhotosType;
67
use TgBotApi\BotApiBase\Type\UserType;
68
use TgBotApi\BotApiBase\Type\WebhookInfoType;
69
70
/**
71
 * Class BotApi.
72
 */
73
class BotApi implements BotApiInterface
74
{
75
    /**
76
     * @var string
77
     */
78
    private $botKey;
79
80
    /**
81
     * @var ApiClientInterface
82
     */
83
    private $apiClient;
84
85
    /**
86
     * @var string
87
     */
88
    private $endPoint;
89
90
    /**
91
     * BotApi constructor.
92
     *
93
     * @param string             $botKey
94
     * @param ApiClientInterface $apiClient
95
     * @param string             $endPoint
96
     */
97 47
    public function __construct(
98
        string $botKey,
99
        ApiClientInterface $apiClient,
100
        string $endPoint = 'https://api.telegram.org'
101
    ) {
102 47
        $this->botKey = $botKey;
103 47
        $this->apiClient = $apiClient;
104 47
        $this->endPoint = $endPoint;
105
106 47
        $this->apiClient->setBotKey($botKey);
107 47
        $this->apiClient->setEndpoint($endPoint);
108 47
    }
109
110
    /**
111
     * @param $method
112
     * @param $type
113
     *
114
     * @throws ResponseException
115
     *
116
     * @return mixed
117
     */
118 47
    public function call($method, $type = null)
119
    {
120 47
        list($data, $files) = $this->encode($method);
121
122 47
        $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

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