Completed
Push — draft ( 9bc211...9e3e7e )
by Nikolay
14:08
created

BotApi   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 344
Duplicated Lines 0 %

Test Coverage

Coverage 96.34%

Importance

Changes 0
Metric Value
wmc 27
eloc 59
dl 0
loc 344
ccs 79
cts 82
cp 0.9634
rs 10
c 0
b 0
f 0

26 Methods

Rating   Name   Duplication   Size   Complexity  
A sendMessage() 0 3 1
A getAbsoluteFilePath() 0 3 1
A getChat() 0 3 1
A getMethodName() 0 3 1
A sendMediaGroup() 0 3 1
A getUserProfilePhotos() 0 3 1
A getChatAdministrators() 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 denormalize() 0 17 1
A sendAudio() 0 3 1
A sendForwardMessage() 0 3 1
A call() 0 14 2
A __construct() 0 8 1
A sendDocument() 0 3 1
A sendPhoto() 0 3 1
A sendLocation() 0 3 1
A getMe() 0 3 1
A encode() 0 16 1
A sendVideoNote() 0 3 1
A getChatMember() 0 3 1
A sendVideo() 0 3 1
A sendContact() 0 3 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\Type\ChatMemberType;
29
use Greenplugin\TelegramBot\Type\ChatType;
30
use Greenplugin\TelegramBot\Type\FileType;
31
use Greenplugin\TelegramBot\Type\MessageType;
32
use Greenplugin\TelegramBot\Type\UpdateType;
33
use Greenplugin\TelegramBot\Type\UserProfilePhotosType;
34
use Greenplugin\TelegramBot\Type\UserType;
35
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
36
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
37
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
38
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
39
use Symfony\Component\Serializer\Serializer;
40
41
class BotApi implements BotApiInterface
42
{
43
    /**
44
     * @var HttpClientInterface
45
     */
46
    private $httpClient;
47
48
    private $key;
49
50
    private $endPoint;
51
52
    /**
53
     * Create a new Skeleton Instance.
54
     *
55
     * @param HttpClientInterface $httpClient
56
     * @param string              $key
57
     * @param string              $endPoint
58
     */
59 4
    public function __construct(
60
        HttpClientInterface $httpClient,
61
        string $key,
62
        string $endPoint = 'https://api.telegram.org'
63
    ) {
64 4
        $this->httpClient = $httpClient;
65 4
        $this->key = $key;
66 4
        $this->endPoint = $endPoint;
67 4
    }
68
69
    /**
70
     * @param $method
71
     * @param $type
72
     *
73
     * @throws ResponseException
74
     *
75
     * @return mixed
76
     */
77 3
    public function call($method, $type)
78
    {
79 3
        $data = $this->encode($method);
80 3
        $json = $this->httpClient->post(
81 3
            $this->endPoint .
82 3
            '/bot' . $this->key . '/' . $this->getMethodName($method),
83 3
            $data
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\...ClientInterface::post() 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

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