Completed
Push — master ( 112fc6...03a809 )
by Nikolay
01:47
created

BotApi::restrictChatMember()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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\SendContactMethod;
35
use TgBotApi\BotApiBase\Method\SendDocumentMethod;
36
use TgBotApi\BotApiBase\Method\SendLocationMethod;
37
use TgBotApi\BotApiBase\Method\SendMediaGroupMethod;
38
use TgBotApi\BotApiBase\Method\SendMessageMethod;
39
use TgBotApi\BotApiBase\Method\SendPhotoMethod;
40
use TgBotApi\BotApiBase\Method\SendVenueMethod;
41
use TgBotApi\BotApiBase\Method\SendVideoMethod;
42
use TgBotApi\BotApiBase\Method\SendVideoNoteMethod;
43
use TgBotApi\BotApiBase\Method\SendVoiceMethod;
44
use TgBotApi\BotApiBase\Normalizer\AnswerInlineQueryNormalizer;
45
use TgBotApi\BotApiBase\Normalizer\InputFileNormalizer;
46
use TgBotApi\BotApiBase\Normalizer\InputMediaNormalizer;
47
use TgBotApi\BotApiBase\Normalizer\JsonSerializableNormalizer;
48
use TgBotApi\BotApiBase\Normalizer\MediaGroupNormalizer;
49
use TgBotApi\BotApiBase\Normalizer\UserProfilePhotosNormalizer;
50
use TgBotApi\BotApiBase\Type\ChatMemberType;
51
use TgBotApi\BotApiBase\Type\ChatType;
52
use TgBotApi\BotApiBase\Type\FileType;
53
use TgBotApi\BotApiBase\Type\MessageType;
54
use TgBotApi\BotApiBase\Type\UpdateType;
55
use TgBotApi\BotApiBase\Type\UserProfilePhotosType;
56
use TgBotApi\BotApiBase\Type\UserType;
57
use TgBotApi\BotApiBase\Type\WebhookInfoType;
58
59
/**
60
 * Class BotApi.
61
 */
62
class BotApi implements BotApiInterface
63
{
64
    /**
65
     * @var string
66
     */
67
    private $botKey;
68
69
    /**
70
     * @var ApiClientInterface
71
     */
72
    private $apiClient;
73
74
    /**
75
     * @var string
76
     */
77
    private $endPoint;
78
79
    /**
80
     * BotApi constructor.
81
     *
82
     * @param string             $botKey
83
     * @param ApiClientInterface $apiClient
84 63
     * @param string             $endPoint
85
     */
86
    public function __construct(
87
        string $botKey,
88
        ApiClientInterface $apiClient,
89 63
        string $endPoint = 'https://api.telegram.org'
90 63
    ) {
91 63
        $this->botKey = $botKey;
92
        $this->apiClient = $apiClient;
93 63
        $this->endPoint = $endPoint;
94 63
95 63
        $this->apiClient->setBotKey($botKey);
96
        $this->apiClient->setEndpoint($endPoint);
97
    }
98
99
    /**
100
     * @param $method
101
     * @param $type
102
     *
103
     * @throws ResponseException
104
     *
105 63
     * @return mixed
106
     */
107 63
    public function call($method, $type = null)
108
    {
109 63
        list($data, $files) = $this->encode($method);
110
111 63
        $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

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