Passed
Push — master ( c05883...1d5c29 )
by Nikolay
02:30
created

BotApi::sendContact()   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\GetChatMethod;
21
use TgBotApi\BotApiBase\Method\GetFileMethod;
22
use TgBotApi\BotApiBase\Method\GetMeMethod;
23
use TgBotApi\BotApiBase\Method\GetUpdatesMethod;
24
use TgBotApi\BotApiBase\Method\GetUserProfilePhotosMethod;
25
use TgBotApi\BotApiBase\Method\KickChatMemberMethod;
26
use TgBotApi\BotApiBase\Method\SendAnimationMethod;
27
use TgBotApi\BotApiBase\Method\SendAudioMethod;
28
use TgBotApi\BotApiBase\Method\SendContactMethod;
29
use TgBotApi\BotApiBase\Method\SendDocumentMethod;
30
use TgBotApi\BotApiBase\Method\SendLocationMethod;
31
use TgBotApi\BotApiBase\Method\SendMediaGroupMethod;
32
use TgBotApi\BotApiBase\Method\SendMessageMethod;
33
use TgBotApi\BotApiBase\Method\SendPhotoMethod;
34
use TgBotApi\BotApiBase\Method\SendVenueMethod;
35
use TgBotApi\BotApiBase\Method\SendVideoMethod;
36
use TgBotApi\BotApiBase\Method\SendVideoNoteMethod;
37
use TgBotApi\BotApiBase\Method\SendVoiceMethod;
38
use TgBotApi\BotApiBase\Normalizer\AnswerInlineQueryNormalizer;
39
use TgBotApi\BotApiBase\Normalizer\InputFileNormalizer;
40
use TgBotApi\BotApiBase\Normalizer\InputMediaNormalizer;
41
use TgBotApi\BotApiBase\Normalizer\JsonSerializableNormalizer;
42
use TgBotApi\BotApiBase\Normalizer\MediaGroupNormalizer;
43
use TgBotApi\BotApiBase\Normalizer\UserProfilePhotosNormalizer;
44
use TgBotApi\BotApiBase\Type\ChatMemberType;
45
use TgBotApi\BotApiBase\Type\ChatType;
46
use TgBotApi\BotApiBase\Type\FileType;
47
use TgBotApi\BotApiBase\Type\MessageType;
48
use TgBotApi\BotApiBase\Type\UpdateType;
49
use TgBotApi\BotApiBase\Type\UserProfilePhotosType;
50
use TgBotApi\BotApiBase\Type\UserType;
51
52
/**
53
 * Class BotApi.
54
 */
55
class BotApi implements BotApiInterface
56
{
57
    /**
58
     * @var string
59
     */
60
    private $botKey;
61
62
    /**
63
     * @var ApiClientInterface
64
     */
65
    private $apiClient;
66
67
    /**
68
     * @var string
69
     */
70
    private $endPoint;
71
72 5
    /**
73
     * BotApi constructor.
74
     *
75
     * @param string             $botKey
76
     * @param ApiClientInterface $apiClient
77 5
     * @param string             $endPoint
78 5
     */
79 5
    public function __construct(
80
        string $botKey,
81 5
        ApiClientInterface $apiClient,
82 5
        string $endPoint = 'https://api.telegram.org'
83 5
    ) {
84
        $this->botKey = $botKey;
85
        $this->apiClient = $apiClient;
86
        $this->endPoint = $endPoint;
87
88
        $this->apiClient->setBotKey($botKey);
89
        $this->apiClient->setEndpoint($endPoint);
90
    }
91
92
    /**
93 5
     * @param $method
94
     * @param $type
95 5
     *
96
     * @throws ResponseException
97 5
     *
98
     * @return mixed
99 5
     */
100
    public function call($method, $type = null)
101
    {
102
        list($data, $files) = $this->encode($method);
103 5
104
        $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

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