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