Client::getSecure()   A
last analyzed

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