Passed
Push — draft ( 541bbe...117fb3 )
by Maxim
02:29
created

BotApi::getChatAdministrators()   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 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\SendAnimationMethod;
17
use Greenplugin\TelegramBot\Method\SendAudioMethod;
18
use Greenplugin\TelegramBot\Method\SendContactMethod;
19
use Greenplugin\TelegramBot\Method\SendDocumentMethod;
20
use Greenplugin\TelegramBot\Method\SendLocationMethod;
21
use Greenplugin\TelegramBot\Method\SendMediaGroupMethod;
22
use Greenplugin\TelegramBot\Method\SendMessageMethod;
23
use Greenplugin\TelegramBot\Method\SendPhotoMethod;
24
use Greenplugin\TelegramBot\Method\SendVenueMethod;
25
use Greenplugin\TelegramBot\Method\SendVideoMethod;
26
use Greenplugin\TelegramBot\Method\SendVideoNoteMethod;
27
use Greenplugin\TelegramBot\Method\SendVoiceMethod;
28
use Greenplugin\TelegramBot\Normalizer\InputFileNormalizer;
29
use Greenplugin\TelegramBot\Normalizer\InputMediaNormalizer;
30
use Greenplugin\TelegramBot\Normalizer\KeyboardNormalizer;
31
use Greenplugin\TelegramBot\Normalizer\MediaGroupNormalizer;
32
use Greenplugin\TelegramBot\Normalizer\UserProfilePhotosNormalizer;
33
use Greenplugin\TelegramBot\Type\ChatMemberType;
34
use Greenplugin\TelegramBot\Type\ChatType;
35
use Greenplugin\TelegramBot\Type\FileType;
36
use Greenplugin\TelegramBot\Type\MessageType;
37
use Greenplugin\TelegramBot\Type\UpdateType;
38
use Greenplugin\TelegramBot\Type\UserProfilePhotosType;
39
use Greenplugin\TelegramBot\Type\UserType;
40
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
41
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
42
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
43
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
44
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
45
use Symfony\Component\Serializer\Serializer;
46
47
class BotApi implements BotApiInterface
48
{
49
    /**
50
     * @var string
51
     */
52
    private $botKey;
53
54
    /**
55
     * @var ApiClientInterface
56
     */
57
    private $apiClient;
58
59
    /**
60
     * @var string
61
     */
62
    private $endPoint;
63
64
    /**
65
     * Create a new Skeleton Instance.
66
     *
67
     * @param ApiClientInterface $client
68
     * @param string             $botKey
69
     * @param ApiClientInterface $apiClient
70
     * @param string             $endPoint
71
     */
72 4
    public function __construct(string $botKey, ApiClientInterface $apiClient, string $endPoint = 'https://api.telegram.org')
73
    {
74 4
        $this->botKey = $botKey;
75 4
        $this->apiClient = $apiClient;
76 4
        $this->endPoint = $endPoint;
77
78 4
        $this->apiClient->setBotKey($botKey);
79 4
        $this->apiClient->setEndpoint($endPoint);
80 4
    }
81
82
    /**
83
     * @param $method
84
     * @param $type
85
     *
86
     * @throws ResponseException
87
     *
88
     * @return mixed
89
     */
90 4
    public function call($method, $type)
91
    {
92 4
        list($data, $files) = $this->encode($method);
93
94 4
        $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

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