Completed
Push — master ( 7d37a2...fea370 )
by Nikolay
06:21
created

BotApi::getChatMember()   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\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 12
    public function __construct(HttpClientInterface $httpClient, string $key, string $endPoint = 'https://api.telegram.org')
60
    {
61 12
        $this->httpClient = $httpClient;
62 12
        $this->key = $key;
63 12
        $this->endPoint = $endPoint;
64 12
    }
65
66
    /**
67
     * @param $method
68
     * @param $type
69
     *
70
     * @throws ResponseException
71
     *
72
     * @return mixed
73
     */
74 9
    public function call($method, $type)
75
    {
76 9
        $data = $this->encode($method);
77 9
        $json = $this->httpClient->post($this->endPoint . '/bot' . $this->key . '/' . $this->getMethodName($method), $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

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