Passed
Push — master ( 1fc8aa...45e169 )
by Anton
02:50
created

Client::barcodes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CdekSDK2;
6
7
use CdekSDK2\Actions\Barcodes;
8
use CdekSDK2\Actions\Calculator;
9
use CdekSDK2\Actions\Intakes;
10
use CdekSDK2\Actions\Invoices;
11
use CdekSDK2\Actions\LocationCities;
12
use CdekSDK2\Actions\LocationRegions;
13
use CdekSDK2\Actions\Offices;
14
use CdekSDK2\Actions\Orders;
15
use CdekSDK2\Actions\Webhooks;
16
use CdekSDK2\Dto\CityList;
17
use CdekSDK2\Dto\RegionList;
18
use CdekSDK2\Dto\Tariff;
19
use CdekSDK2\Dto\TariffList;
20
use CdekSDK2\Dto\WebHookList;
21
use CdekSDK2\Dto\PickupPointList;
22
use CdekSDK2\Dto\Response;
23
use CdekSDK2\Exceptions\AuthException;
24
use CdekSDK2\Exceptions\ParsingException;
25
use CdekSDK2\Http\Api;
26
use CdekSDK2\Http\ApiResponse;
27
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
28
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
29
use JMS\Serializer\Serializer;
30
use JMS\Serializer\SerializerBuilder;
31
use Psr\Http\Client\ClientInterface;
32
33
/**
34
 * Class Client
35
 * @package CdekSDK2
36
 */
37
class Client
38
{
39
    /**
40
     * Объект для взаимодействия с API СДЭК
41
     * @var Api
42
     */
43
    private $http_client;
44
45
    /**
46
     * @var Serializer
47
     */
48
    private $serializer;
49
50
    /**
51
     * @var Orders
52
     */
53
    private $orders;
54
55
    /**
56
     * @var Intakes
57
     */
58
    private $intakes;
59
60
    /**
61
     * @var Calculator
62
     */
63
    private $calculator;
64
65
    /**
66
     * @var Webhooks
67
     */
68
    private $webhooks;
69
70
    /**
71
     * @var Offices
72
     */
73
    private $offices;
74
75
    /**
76
     * @var Barcodes
77
     */
78
    private $barcodes;
79
80
    /**
81
     * @var Invoices
82
     */
83
    private $invoices;
84
85
    /**
86
     * @var LocationRegions
87
     */
88
    private $regions;
89
90
    /**
91
     * @var LocationCities
92
     */
93
    private $cities;
94
95
    /**
96
     * Client constructor.
97
     * @param ClientInterface $http
98
     * @param string|null $account
99
     * @param string|null $secure
100
     * @psalm-suppress PropertyTypeCoercion
101
     */
102 49
    public function __construct(ClientInterface $http, string $account = null, string $secure = null)
103
    {
104 49
        $this->http_client = new Api($http, $account, $secure);
105 49
        $this->serializer = SerializerBuilder::create()->setPropertyNamingStrategy(
106 49
            new SerializedNameAnnotationStrategy(
107 49
                new IdenticalPropertyNamingStrategy()
108 49
            )
109 49
        )->build();
110
    }
111
112
    /**
113
     * @return string
114
     */
115 3
    public function getAccount(): string
116
    {
117 3
        return $this->http_client->getAccount();
118
    }
119
120
    /**
121
     * @param string $account
122
     * @return self
123
     */
124 2
    public function setAccount(string $account): self
125
    {
126 2
        $this->http_client->setAccount($account);
127 2
        return $this;
128
    }
129
130
    /**
131
     * @return string
132
     */
133 2
    public function getToken(): string
134
    {
135 2
        return $this->http_client->getToken();
136
    }
137
138
    /**
139
     * @param string $token
140
     * @return self
141
     */
142 1
    public function setToken(string $token): self
143
    {
144 1
        $this->http_client->setToken($token);
145 1
        return $this;
146
    }
147
148
    /**
149
     * @return string
150
     */
151 2
    public function getSecure(): string
152
    {
153 2
        return $this->http_client->getSecure();
154
    }
155
156
    /**
157
     * @param string $secure
158
     * @return self
159
     */
160 1
    public function setSecure(string $secure): self
161
    {
162 1
        $this->http_client->setSecure($secure);
163 1
        return $this;
164
    }
165
166
    /**
167
     * @return bool
168
     */
169 2
    public function isTest(): bool
170
    {
171 2
        return $this->http_client->isTest();
172
    }
173
174
    /**
175
     * @param bool $test
176
     * @return self
177
     */
178 32
    public function setTest(bool $test): self
179
    {
180 32
        $this->http_client->setTest($test);
181 32
        return $this;
182
    }
183
184
    /**
185
     * @return bool
186
     */
187 1
    public function isExpired(): bool
188
    {
189 1
        return $this->http_client->isExpired();
190
    }
191
192
    /**
193
     * @return int
194
     */
195 2
    public function getExpire(): int
196
    {
197 2
        return $this->http_client->getExpire();
198
    }
199
200
    /**
201
     * @param int $timestamp
202
     * @return self
203
     */
204 1
    public function setExpire(int $timestamp): self
205
    {
206 1
        $this->http_client->setExpire($timestamp);
207 1
        return $this;
208
    }
209
210
    /**
211
     * Авторизация клиента в сервисе Интеграции
212
     * @return bool
213
     * @throws AuthException
214
     * @throws Exceptions\RequestException
215
     */
216 1
    public function authorize(): bool
217
    {
218 1
        return $this->http_client->authorize();
219
    }
220
221
    /**
222
     * @return Intakes
223
     */
224 4
    public function intakes(): Intakes
225
    {
226 4
        if ($this->intakes === null) {
227 4
            $this->intakes = new Intakes($this->http_client, $this->serializer);
228
        }
229 4
        return $this->intakes;
230
    }
231
232
    /**
233
     * @return Calculator
234
     */
235 2
    public function calculator(): Calculator
236
    {
237 2
        if ($this->calculator === null) {
238 2
            $this->calculator = new Calculator($this->http_client, $this->serializer);
239
        }
240 2
        return $this->calculator;
241
    }
242
243
    /**
244
     * @return Orders
245
     */
246 4
    public function orders(): Orders
247
    {
248 4
        if ($this->orders === null) {
249 4
            $this->orders = new Orders($this->http_client, $this->serializer);
250
        }
251 4
        return $this->orders;
252
    }
253
254
    /**
255
     * @return Offices
256
     */
257 6
    public function offices(): Offices
258
    {
259 6
        if ($this->offices === null) {
260 6
            $this->offices = new Offices($this->http_client, $this->serializer);
261
        }
262 6
        return $this->offices;
263
    }
264
265
    /**
266
     * @return LocationRegions
267
     */
268 2
    public function regions(): LocationRegions
269
    {
270 2
        if ($this->regions === null) {
271 2
            $this->regions = new LocationRegions($this->http_client, $this->serializer);
272
        }
273 2
        return $this->regions;
274
    }
275
276
    /**
277
     * @return LocationCities
278
     */
279 2
    public function cities(): LocationCities
280
    {
281 2
        if ($this->cities === null) {
282 2
            $this->cities = new LocationCities($this->http_client, $this->serializer);
283
        }
284 2
        return $this->cities;
285
    }
286
287
    /**
288
     * @return Webhooks
289
     */
290 8
    public function webhooks(): Webhooks
291
    {
292 8
        if ($this->webhooks === null) {
293 8
            $this->webhooks = new Webhooks($this->http_client, $this->serializer);
294
        }
295 8
        return $this->webhooks;
296
    }
297
298
    /**
299
     * @return Invoices
300
     */
301 3
    public function invoice(): Invoices
302
    {
303 3
        if ($this->invoices === null) {
304 3
            $this->invoices = new Invoices($this->http_client, $this->serializer);
305
        }
306 3
        return $this->invoices;
307
    }
308
309
    /**
310
     * @return Barcodes
311
     */
312 3
    public function barcodes(): Barcodes
313
    {
314 3
        if ($this->barcodes === null) {
315 3
            $this->barcodes = new Barcodes($this->http_client, $this->serializer);
316
        }
317 3
        return $this->barcodes;
318
    }
319
320
    /**
321
     * @param ApiResponse $response
322
     * @param string $className
323
     * @return Response
324
     * @throws \Exception
325
     */
326 2
    public function formatResponse(ApiResponse $response, string $className): Response
327
    {
328 2
        if (class_exists($className)) {
329
            /* @var $result Response */
330 1
            $result = $this->serializer->deserialize($response->getBody(), Response::class, 'json');
331 1
            $result->entity = null;
332
333 1
            $array_response = json_decode($response->getBody(), true);
334 1
            $entity = $this->serializer->deserialize(json_encode($array_response['entity']), $className, 'json');
335 1
            $result->entity = $entity;
336
337 1
            return $result;
338
        }
339
340 1
        throw new ParsingException('Class ' . $className . ' not found');
341
    }
342
343
    /**
344
     * Пока что этот метод возвращет только Tariff, так как нужен только для одного запроса
345
     * @param ApiResponse $response
346
     * @param string $className
347
     * @return Tariff
348
     * @throws \Exception
349
     */
350 1
    public function formatBaseResponse(ApiResponse $response, string $className): Tariff
351
    {
352 1
        if (class_exists($className)) {
353 1
            return $this->serializer->deserialize($response->getBody(), $className, 'json');
354
        }
355
356
        throw new ParsingException('Class ' . $className . ' not found');
357
    }
358
359
    /**
360
     * @param ApiResponse $response
361
     * @param string $className
362
     * @return CityList|RegionList|PickupPointList|WebHookList|TariffList
363
     * @throws \Exception
364
     */
365 4
    public function formatResponseList(ApiResponse $response, string $className)
366
    {
367 4
        if (class_exists($className)) {
368 3
            if ((new \ReflectionClass($className))->getShortName() == 'TariffList') {
369 1
                $result = $this->serializer->deserialize($response->getBody(), $className, 'json');
370 1
                return $result;
371
            } else {
372 2
                $body = '{"items":' . $response->getBody() . '}';
373 2
                $result = $this->serializer->deserialize($body, $className, 'json');
374 2
                return $result;
375
            }
376
        }
377
378 1
        throw new ParsingException('Class ' . $className . ' not found');
379
    }
380
}
381