Passed
Push — draft ( 117fb3...f0f317 )
by Maxim
02:16
created

BotApi::getUserProfilePhotos()   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
     * BotApi constructor.
66
     *
67
     * @param string             $botKey
68
     * @param ApiClientInterface $apiClient
69
     * @param string             $endPoint
70
     */
71 4
    public function __construct(
72
        string $botKey,
73
        ApiClientInterface $apiClient,
74
        string $endPoint = 'https://api.telegram.org'
75
    ) {
76 4
        $this->botKey = $botKey;
77 4
        $this->apiClient = $apiClient;
78 4
        $this->endPoint = $endPoint;
79
80 4
        $this->apiClient->setBotKey($botKey);
81 4
        $this->apiClient->setEndpoint($endPoint);
82 4
    }
83
84
    /**
85
     * @param $method
86
     * @param $type
87
     *
88
     * @throws ResponseException
89
     *
90
     * @return mixed
91
     */
92 4
    public function call($method, $type)
93
    {
94 4
        list($data, $files) = $this->encode($method);
95
96 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

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