BotApi   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 375
Duplicated Lines 0 %

Test Coverage

Coverage 96.63%

Importance

Changes 0
Metric Value
wmc 29
eloc 65
dl 0
loc 375
ccs 86
cts 89
cp 0.9663
rs 10
c 0
b 0
f 0

27 Methods

Rating   Name   Duplication   Size   Complexity  
A sendMessage() 0 3 1
A getAbsoluteFilePath() 0 3 1
A getChat() 0 3 1
A getMethodName() 0 6 1
A sendMediaGroup() 0 3 1
A getUserProfilePhotos() 0 3 1
A getChatAdministrators() 0 3 1
A kickChatMember() 0 3 1
A sendAnimation() 0 3 1
A getUpdates() 0 3 1
A sendVoice() 0 3 1
A sendVenue() 0 3 1
A getFile() 0 3 1
A sendAudio() 0 3 1
A sendForwardMessage() 0 3 1
A __construct() 0 11 1
A sendDocument() 0 3 1
A sendPhoto() 0 3 1
A sendLocation() 0 3 1
A getMe() 0 3 1
A sendVideoNote() 0 3 1
A getChatMember() 0 3 1
A sendVideo() 0 3 1
A sendContact() 0 3 1
A denormalize() 0 17 1
A call() 0 11 3
A encode() 0 20 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Greenplugin\TelegramBot;
6
7
use Greenplugin\TelegramBot\Exception\ResponseException;
8
use Greenplugin\TelegramBot\Method\ForwardMessageMethod;
9
use Greenplugin\TelegramBot\Method\GetChatAdministratorsMethod;
10
use Greenplugin\TelegramBot\Method\GetChatMemberMethod;
11
use Greenplugin\TelegramBot\Method\GetChatMethod;
12
use Greenplugin\TelegramBot\Method\GetFileMethod;
13
use Greenplugin\TelegramBot\Method\GetMeMethod;
14
use Greenplugin\TelegramBot\Method\GetUpdatesMethod;
15
use Greenplugin\TelegramBot\Method\GetUserProfilePhotosMethod;
16
use Greenplugin\TelegramBot\Method\KickChatMemberMethod;
17
use Greenplugin\TelegramBot\Method\SendAnimationMethod;
18
use Greenplugin\TelegramBot\Method\SendAudioMethod;
19
use Greenplugin\TelegramBot\Method\SendContactMethod;
20
use Greenplugin\TelegramBot\Method\SendDocumentMethod;
21
use Greenplugin\TelegramBot\Method\SendLocationMethod;
22
use Greenplugin\TelegramBot\Method\SendMediaGroupMethod;
23
use Greenplugin\TelegramBot\Method\SendMessageMethod;
24
use Greenplugin\TelegramBot\Method\SendPhotoMethod;
25
use Greenplugin\TelegramBot\Method\SendVenueMethod;
26
use Greenplugin\TelegramBot\Method\SendVideoMethod;
27
use Greenplugin\TelegramBot\Method\SendVideoNoteMethod;
28
use Greenplugin\TelegramBot\Method\SendVoiceMethod;
29
use Greenplugin\TelegramBot\Normalizer\InputFileNormalizer;
30
use Greenplugin\TelegramBot\Normalizer\InputMediaNormalizer;
31
use Greenplugin\TelegramBot\Normalizer\KeyboardNormalizer;
32
use Greenplugin\TelegramBot\Normalizer\MediaGroupNormalizer;
33
use Greenplugin\TelegramBot\Normalizer\UserProfilePhotosNormalizer;
34
use Greenplugin\TelegramBot\Type\ChatMemberType;
35
use Greenplugin\TelegramBot\Type\ChatType;
36
use Greenplugin\TelegramBot\Type\FileType;
37
use Greenplugin\TelegramBot\Type\MessageType;
38
use Greenplugin\TelegramBot\Type\UpdateType;
39
use Greenplugin\TelegramBot\Type\UserProfilePhotosType;
40
use Greenplugin\TelegramBot\Type\UserType;
41
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
42
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
43
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
44
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
45
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
46
use Symfony\Component\Serializer\Serializer;
47
48
class BotApi implements BotApiInterface
49
{
50
    /**
51
     * @var string
52
     */
53
    private $botKey;
54
55
    /**
56
     * @var ApiClientInterface
57
     */
58
    private $apiClient;
59
60
    /**
61
     * @var string
62
     */
63
    private $endPoint;
64
65
    /**
66
     * BotApi constructor.
67
     *
68
     * @param string             $botKey
69
     * @param ApiClientInterface $apiClient
70
     * @param string             $endPoint
71
     */
72 5
    public function __construct(
73
        string $botKey,
74
        ApiClientInterface $apiClient,
75
        string $endPoint = 'https://api.telegram.org'
76
    ) {
77 5
        $this->botKey = $botKey;
78 5
        $this->apiClient = $apiClient;
79 5
        $this->endPoint = $endPoint;
80
81 5
        $this->apiClient->setBotKey($botKey);
82 5
        $this->apiClient->setEndpoint($endPoint);
83 5
    }
84
85
    /**
86
     * @param $method
87
     * @param $type
88
     *
89
     * @throws ResponseException
90
     *
91
     * @return mixed
92
     */
93 5
    public function call($method, $type = null)
94
    {
95 5
        list($data, $files) = $this->encode($method);
96
97 5
        $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 Greenplugin\TelegramBot\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

97
        $json = $this->apiClient->send($this->getMethodName($method), /** @scrutinizer ignore-type */ $data, $files);
Loading history...
98
99 5
        if (true !== $json->ok) {
100
            throw new ResponseException($json->description);
101
        }
102
103 5
        return $type ? $this->denormalize($json, $type) : $json->result;
104
    }
105
106
    /**
107
     * @param GetUpdatesMethod $method
108
     *
109
     * @throws ResponseException
110
     *
111
     * @return UpdateType[]
112
     */
113 1
    public function getUpdates(GetUpdatesMethod $method): array
114
    {
115 1
        return $this->call($method, UpdateType::class . '[]');
116
    }
117
118
    /**
119
     * @param GetMeMethod $method
120
     *
121
     * @throws ResponseException
122
     *
123
     * @return UserType
124
     */
125 2
    public function getMe(GetMeMethod $method): UserType
126
    {
127 2
        return $this->call($method, UserType::class);
128
    }
129
130
    /**
131
     * @param SendMessageMethod $method
132
     *
133
     * @throws ResponseException
134
     *
135
     * @return MessageType
136
     */
137 2
    public function sendMessage(SendMessageMethod $method): MessageType
138
    {
139 2
        return $this->call($method, MessageType::class);
140
    }
141
142
    /**
143
     * @param ForwardMessageMethod $method
144
     *
145
     * @throws ResponseException
146
     *
147
     * @return MessageType
148
     */
149 1
    public function sendForwardMessage(ForwardMessageMethod $method): MessageType
150
    {
151 1
        return $this->call($method, MessageType::class);
152
    }
153
154
    /**
155
     * @param SendPhotoMethod $method
156
     *
157
     * @throws ResponseException
158
     *
159
     * @return MessageType
160
     */
161 1
    public function sendPhoto(SendPhotoMethod $method): MessageType
162
    {
163 1
        return $this->call($method, MessageType::class);
164
    }
165
166
    /**
167
     * @param SendAudioMethod $method
168
     *
169
     * @throws ResponseException
170
     *
171
     * @return MessageType
172
     */
173 1
    public function sendAudio(SendAudioMethod $method): MessageType
174
    {
175 1
        return $this->call($method, MessageType::class);
176
    }
177
178
    /**
179
     * @param SendDocumentMethod $method
180
     *
181
     * @throws ResponseException
182
     *
183
     * @return MessageType
184
     */
185 1
    public function sendDocument(SendDocumentMethod $method): MessageType
186
    {
187 1
        return $this->call($method, MessageType::class);
188
    }
189
190
    /**
191
     * @param SendVideoMethod $method
192
     *
193
     * @throws ResponseException
194
     *
195
     * @return MessageType
196
     */
197 1
    public function sendVideo(SendVideoMethod $method): MessageType
198
    {
199 1
        return $this->call($method, MessageType::class);
200
    }
201
202
    /**
203
     * @param SendAnimationMethod $method
204
     *
205
     * @throws ResponseException
206
     *
207
     * @return MessageType
208
     */
209 1
    public function sendAnimation(SendAnimationMethod $method): MessageType
210
    {
211 1
        return $this->call($method, MessageType::class);
212
    }
213
214
    /**
215
     * @param SendVoiceMethod $method
216
     *
217
     * @throws ResponseException
218
     *
219
     * @return MessageType
220
     */
221 1
    public function sendVoice(SendVoiceMethod $method): MessageType
222
    {
223 1
        return $this->call($method, MessageType::class);
224
    }
225
226
    /**
227
     * @param SendVideoNoteMethod $method
228
     *
229
     * @throws ResponseException
230
     *
231
     * @return MessageType
232
     */
233 1
    public function sendVideoNote(SendVideoNoteMethod $method): MessageType
234
    {
235 1
        return $this->call($method, MessageType::class);
236
    }
237
238
    /**
239
     * @param SendMediaGroupMethod $method
240
     *
241
     * @throws ResponseException
242
     *
243
     * @return MessageType[]
244
     */
245 1
    public function sendMediaGroup(SendMediaGroupMethod $method): array
246
    {
247 1
        return $this->call($method, MessageType::class . '[]');
248
    }
249
250
    /**
251
     * @param SendLocationMethod $method
252
     *
253
     * @throws ResponseException
254
     *
255
     * @return MessageType
256
     */
257 1
    public function sendLocation(SendLocationMethod $method): MessageType
258
    {
259 1
        return $this->call($method, MessageType::class);
260
    }
261
262
    /**
263
     * @param SendVenueMethod $method
264
     *
265
     * @throws ResponseException
266
     *
267
     * @return MessageType
268
     */
269 1
    public function sendVenue(SendVenueMethod $method): MessageType
270
    {
271 1
        return $this->call($method, MessageType::class);
272
    }
273
274
    /**
275
     * @param SendContactMethod $method
276
     *
277
     * @throws ResponseException
278
     *
279
     * @return MessageType
280
     */
281 1
    public function sendContact(SendContactMethod $method): MessageType
282
    {
283 1
        return $this->call($method, MessageType::class);
284
    }
285
286
    /**
287
     * @param GetUserProfilePhotosMethod $method
288
     *
289
     * @throws ResponseException
290
     *
291
     * @return UserProfilePhotosType
292
     */
293 1
    public function getUserProfilePhotos(GetUserProfilePhotosMethod $method): UserProfilePhotosType
294
    {
295 1
        return $this->call($method, UserProfilePhotosType::class);
296
    }
297
298
    /**
299
     * @todo fix this is bad
300
     *
301
     * @param GetFileMethod $method
302
     *
303
     * @throws ResponseException
304
     *
305
     * @return FileType
306
     */
307 1
    public function getFile(GetFileMethod $method): FileType
308
    {
309 1
        return $this->call($method, FileType::class);
310
    }
311
312
    /**
313
     * @param FileType $file
314
     *
315
     * @return string
316
     */
317
    public function getAbsoluteFilePath(FileType $file): string
318
    {
319
        return \sprintf('%s/file/bot%s/%s', $this->endPoint, $this->botKey, $file->filePath);
320
    }
321
322
    /**
323
     * @param GetChatMethod $method
324
     *
325
     * @throws ResponseException
326
     *
327
     * @return ChatType
328
     */
329 1
    public function getChat(GetChatMethod $method): ChatType
330
    {
331 1
        return $this->call($method, ChatType::class);
332
    }
333
334
    /**
335
     * @param GetChatAdministratorsMethod $method
336
     *
337
     * @throws ResponseException
338
     *
339
     * @return ChatMemberType[]
340
     */
341 1
    public function getChatAdministrators(GetChatAdministratorsMethod $method): array
342
    {
343 1
        return $this->call($method, ChatMemberType::class . '[]');
344
    }
345
346
    /**
347
     * @param GetChatMemberMethod $method
348
     *
349
     * @throws ResponseException
350
     *
351
     * @return ChatMemberType
352
     */
353 1
    public function getChatMember(GetChatMemberMethod $method): ChatMemberType
354
    {
355 1
        return $this->call($method, ChatMemberType::class);
356
    }
357
358
    /**
359
     * @param KickChatMemberMethod $method
360
     *
361
     * @throws ResponseException
362
     *
363
     * @return bool
364
     */
365 1
    public function kickChatMember(KickChatMemberMethod $method): bool
366
    {
367 1
        return $this->call($method);
368
    }
369
370
//    public function answerInlineQuery(AnswerInlineQueryMethod $method)
371
//    {
372
//        return $this->call($method, '');
373
//    }
374
375 5
    private function getMethodName($method)
376
    {
377 5
        return \lcfirst(\substr(
378 5
            \get_class($method),
379 5
            \strrpos(\get_class($method), '\\') + 1,
380 5
            -1 * \strlen('Method')
381
        ));
382
    }
383
384 4
    private function denormalize($data, $type)
385
    {
386 4
        $normalizer = new ObjectNormalizer(
387 4
            null,
388 4
            new CamelCaseToSnakeCaseNameConverter(),
389 4
            null,
390 4
            new PhpDocExtractor()
391
        );
392 4
        $arrayNormalizer = new ArrayDenormalizer();
393 4
        $serializer = new Serializer([
394 4
            new UserProfilePhotosNormalizer($normalizer, $arrayNormalizer),
395 4
            new DateTimeNormalizer(),
396 4
            $normalizer,
397 4
            $arrayNormalizer,
398
        ]);
399
400 4
        return $serializer->denormalize($data->result, $type, null, [DateTimeNormalizer::FORMAT_KEY => 'U']);
401
    }
402
403 5
    private function encode($method)
404
    {
405 5
        $files = [];
406
407 5
        $objectNormalizer = new ObjectNormalizer(null, new CamelCaseToSnakeCaseNameConverter());
408 5
        $serializer = new Serializer([
409 5
            new InputFileNormalizer($files),
410 5
            new MediaGroupNormalizer(new InputMediaNormalizer($objectNormalizer, $files), $objectNormalizer),
411 5
            new KeyboardNormalizer($objectNormalizer),
412 5
            new DateTimeNormalizer(),
413 5
            $objectNormalizer,
414
        ]);
415
416 5
        $data = $serializer->normalize(
417 5
            $method,
418 5
            null,
419 5
            ['skip_null_values' => true, DateTimeNormalizer::FORMAT_KEY => 'U']
420
        );
421
422 5
        return [$data, $files];
423
    }
424
}
425