Passed
Branch Helper (0d23ed)
by Nikolay
05:59
created

BotApi::sendContact()   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 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\NormalizationException;
14
use TgBotApi\BotApiBase\Exception\ResponseException;
15
use TgBotApi\BotApiBase\Normalizer\AnswerInlineQueryNormalizer;
16
use TgBotApi\BotApiBase\Normalizer\InputFileNormalizer;
17
use TgBotApi\BotApiBase\Normalizer\InputMediaNormalizer;
18
use TgBotApi\BotApiBase\Normalizer\JsonSerializableNormalizer;
19
use TgBotApi\BotApiBase\Normalizer\MediaGroupNormalizer;
20
use TgBotApi\BotApiBase\Normalizer\UserProfilePhotosNormalizer;
21
22
/**
23
 * Class BotApi.
24
 */
25
class BotApi implements BotApiInterface
26
{
27
    /**
28
     * @var string
29
     */
30
    private $botKey;
31
32
    /**
33
     * @var ApiClientInterface
34
     */
35
    private $apiClient;
36
37
    /**
38
     * @var string
39
     */
40
    private $endPoint;
41
42
    /**
43
     * BotApi constructor.
44
     *
45
     * @param string             $botKey
46
     * @param ApiClientInterface $apiClient
47
     * @param string             $endPoint
48
     */
49 49
    public function __construct(
50
        string $botKey,
51
        ApiClientInterface $apiClient,
52
        string $endPoint = 'https://api.telegram.org'
53
    ) {
54 49
        $this->botKey = $botKey;
55 49
        $this->apiClient = $apiClient;
56 49
        $this->endPoint = $endPoint;
57
58 49
        $this->apiClient->setBotKey($botKey);
59 49
        $this->apiClient->setEndpoint($endPoint);
60 49
    }
61
62
    /**
63
     * @param $method
64
     * @param $type
65
     *
66
     * @throws ResponseException
67
     * @throws NormalizationException
68
     *
69
     * @return mixed
70
     */
71 46
    public function call($method, $type = null)
72
    {
73 46
        list($data, $files) = $this->encode($method);
74
75 46
        $json = $this->apiClient->send($this->getMethodName($method), $data, $files);
76
77 46
        if (true !== $json->ok) {
78
            throw new ResponseException($json->description);
79
        }
80
81 46
        return $type ? $this->denormalize($json, $type) : $json->result;
82
    }
83
84
    /**
85
     * @return string
86
     */
87 2
    public function getBotKey(): string
88
    {
89 2
        return $this->botKey;
90
    }
91
92
    /**
93
     * @return string
94
     */
95 2
    public function getEndPoint(): string
96
    {
97 2
        return $this->endPoint;
98
    }
99
100
    /**
101
     * @param $method
102
     *
103
     * @return string
104
     */
105 46
    private function getMethodName($method): string
106
    {
107 46
        return \lcfirst(\substr(
108 46
            \get_class($method),
109 46
            \strrpos(\get_class($method), '\\') + 1,
110 46
            -1 * \strlen('Method')
111
        ));
112
    }
113
114
    /**
115
     * @param $data
116
     * @param $type
117
     *
118
     * @return object|array
119
     */
120 25
    private function denormalize($data, $type)
121
    {
122 25
        $normalizer = new ObjectNormalizer(
123 25
            null,
124 25
            new CamelCaseToSnakeCaseNameConverter(),
125 25
            null,
126 25
            new PhpDocExtractor()
127
        );
128 25
        $arrayNormalizer = new ArrayDenormalizer();
129 25
        $serializer = new Serializer([
130 25
            new UserProfilePhotosNormalizer($normalizer, $arrayNormalizer),
131 25
            new DateTimeNormalizer(),
132 25
            $normalizer,
133 25
            $arrayNormalizer,
134
        ]);
135
136 25
        return $serializer->denormalize($data->result, $type, null, [DateTimeNormalizer::FORMAT_KEY => 'U']);
137
    }
138
139
    /**
140
     * @param $method
141
     *
142
     * @throws NormalizationException
143
     *
144
     * @return array []
145
     */
146 46
    private function encode($method): array
147
    {
148 46
        $files = [];
149
150 46
        $objectNormalizer = new ObjectNormalizer(null, new CamelCaseToSnakeCaseNameConverter());
151 46
        $serializer = new Serializer([
152 46
            new InputFileNormalizer($files),
153 46
            new MediaGroupNormalizer(new InputMediaNormalizer($objectNormalizer, $files), $objectNormalizer),
154 46
            new JsonSerializableNormalizer($objectNormalizer),
155 46
            new AnswerInlineQueryNormalizer($objectNormalizer),
156 46
            new DateTimeNormalizer(),
157 46
            $objectNormalizer,
158
        ]);
159
160 46
        $data = $serializer->normalize(
161 46
            $method,
162 46
            null,
163 46
            ['skip_null_values' => true, DateTimeNormalizer::FORMAT_KEY => 'U']
164
        );
165
166 46
        if (!\is_array($data)) {
167
            throw new NormalizationException('Normalized data must be array');
168
        }
169
170 46
        return [$data, $files];
171
    }
172
}
173