Passed
Push — master ( 59a435...62cb7b )
by Nikolay
11:22
created

BotApi::getChatMember()   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\SendVenueMethod;
44
use TgBotApi\BotApiBase\Method\SendVideoMethod;
45
use TgBotApi\BotApiBase\Method\SendVideoNoteMethod;
46
use TgBotApi\BotApiBase\Method\SendVoiceMethod;
47
use TgBotApi\BotApiBase\Normalizer\AnswerInlineQueryNormalizer;
48
use TgBotApi\BotApiBase\Normalizer\InputFileNormalizer;
49
use TgBotApi\BotApiBase\Normalizer\InputMediaNormalizer;
50
use TgBotApi\BotApiBase\Normalizer\JsonSerializableNormalizer;
51
use TgBotApi\BotApiBase\Normalizer\MediaGroupNormalizer;
52
use TgBotApi\BotApiBase\Normalizer\UserProfilePhotosNormalizer;
53
use TgBotApi\BotApiBase\Type\ChatMemberType;
54
use TgBotApi\BotApiBase\Type\ChatType;
55
use TgBotApi\BotApiBase\Type\FileType;
56
use TgBotApi\BotApiBase\Type\MessageType;
57
use TgBotApi\BotApiBase\Type\UpdateType;
58
use TgBotApi\BotApiBase\Type\UserProfilePhotosType;
59
use TgBotApi\BotApiBase\Type\UserType;
60
use TgBotApi\BotApiBase\Type\WebhookInfoType;
61
62
/**
63
 * Class BotApi.
64
 */
65
class BotApi implements BotApiInterface
66
{
67
    /**
68
     * @var string
69
     */
70
    private $botKey;
71
72
    /**
73
     * @var ApiClientInterface
74
     */
75
    private $apiClient;
76
77
    /**
78
     * @var string
79
     */
80
    private $endPoint;
81
82
    /**
83
     * BotApi constructor.
84
     *
85
     * @param string             $botKey
86
     * @param ApiClientInterface $apiClient
87
     * @param string             $endPoint
88
     */
89 30
    public function __construct(
90
        string $botKey,
91
        ApiClientInterface $apiClient,
92
        string $endPoint = 'https://api.telegram.org'
93
    ) {
94 30
        $this->botKey = $botKey;
95 30
        $this->apiClient = $apiClient;
96 30
        $this->endPoint = $endPoint;
97
98 30
        $this->apiClient->setBotKey($botKey);
99 30
        $this->apiClient->setEndpoint($endPoint);
100 30
    }
101
102
    /**
103
     * @param $method
104
     * @param $type
105
     *
106
     * @throws ResponseException
107
     *
108
     * @return mixed
109
     */
110 30
    public function call($method, $type = null)
111
    {
112 30
        list($data, $files) = $this->encode($method);
113
114 30
        $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

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