|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2019. CDEK-IT. All rights reserved. |
|
5
|
|
|
* See LICENSE.md for license details. |
|
6
|
|
|
* |
|
7
|
|
|
* @author Chizhekov Viktor |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace CdekSDK2; |
|
11
|
|
|
|
|
12
|
|
|
use CdekSDK2\Actions\Barcodes; |
|
13
|
|
|
use CdekSDK2\Actions\Intakes; |
|
14
|
|
|
use CdekSDK2\Actions\Invoices; |
|
15
|
|
|
use CdekSDK2\Actions\LocationCities; |
|
16
|
|
|
use CdekSDK2\Actions\LocationRegions; |
|
17
|
|
|
use CdekSDK2\Actions\Offices; |
|
18
|
|
|
use CdekSDK2\Actions\Orders; |
|
19
|
|
|
use CdekSDK2\Actions\Webhooks; |
|
20
|
|
|
use CdekSDK2\BaseTypes\CityList; |
|
21
|
|
|
use CdekSDK2\BaseTypes\PickupPointList; |
|
22
|
|
|
use CdekSDK2\BaseTypes\RegionList; |
|
23
|
|
|
use CdekSDK2\BaseTypes\Response; |
|
24
|
|
|
use CdekSDK2\BaseTypes\WebHookList; |
|
25
|
|
|
use CdekSDK2\Exceptions\AuthException; |
|
26
|
|
|
use CdekSDK2\Exceptions\ParsingException; |
|
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 Webhooks |
|
64
|
|
|
*/ |
|
65
|
|
|
private $webhooks; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var Offices |
|
69
|
|
|
*/ |
|
70
|
|
|
private $offices; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @var Barcodes |
|
74
|
|
|
*/ |
|
75
|
|
|
private $barcodes; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @var Invoices |
|
79
|
|
|
*/ |
|
80
|
|
|
private $invoices; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @var LocationRegions |
|
84
|
|
|
*/ |
|
85
|
|
|
private $regions; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @var LocationCities |
|
89
|
|
|
*/ |
|
90
|
|
|
private $cities; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Client constructor. |
|
94
|
|
|
* @param ClientInterface $http |
|
95
|
|
|
* @param string|null $account |
|
96
|
|
|
* @param string|null $secure |
|
97
|
|
|
* @psalm-suppress PropertyTypeCoercion |
|
98
|
|
|
*/ |
|
99
|
44 |
|
public function __construct(ClientInterface $http, string $account = null, string $secure = null) |
|
100
|
|
|
{ |
|
101
|
44 |
|
$this->http_client = new Api($http, $account, $secure); |
|
102
|
44 |
|
$this->serializer = SerializerBuilder::create()->setPropertyNamingStrategy( |
|
103
|
44 |
|
new SerializedNameAnnotationStrategy( |
|
104
|
44 |
|
new IdenticalPropertyNamingStrategy() |
|
105
|
|
|
) |
|
106
|
44 |
|
)->build(); |
|
107
|
44 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
3 |
|
public function getAccount(): string |
|
113
|
|
|
{ |
|
114
|
3 |
|
return $this->http_client->getAccount(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param string $account |
|
119
|
|
|
* @return self |
|
120
|
|
|
*/ |
|
121
|
2 |
|
public function setAccount(string $account): self |
|
122
|
|
|
{ |
|
123
|
2 |
|
$this->http_client->setAccount($account); |
|
124
|
2 |
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
2 |
|
public function getToken(): string |
|
131
|
|
|
{ |
|
132
|
2 |
|
return $this->http_client->getToken(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param string $token |
|
137
|
|
|
* @return self |
|
138
|
|
|
*/ |
|
139
|
1 |
|
public function setToken(string $token): self |
|
140
|
|
|
{ |
|
141
|
1 |
|
$this->http_client->setToken($token); |
|
142
|
1 |
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return string |
|
147
|
|
|
*/ |
|
148
|
2 |
|
public function getSecure(): string |
|
149
|
|
|
{ |
|
150
|
2 |
|
return $this->http_client->getSecure(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param string $secure |
|
155
|
|
|
* @return self |
|
156
|
|
|
*/ |
|
157
|
1 |
|
public function setSecure(string $secure): self |
|
158
|
|
|
{ |
|
159
|
1 |
|
$this->http_client->setSecure($secure); |
|
160
|
1 |
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return bool |
|
165
|
|
|
*/ |
|
166
|
2 |
|
public function isTest(): bool |
|
167
|
|
|
{ |
|
168
|
2 |
|
return $this->http_client->isTest(); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @param bool $test |
|
173
|
|
|
* @return self |
|
174
|
|
|
*/ |
|
175
|
29 |
|
public function setTest(bool $test): self |
|
176
|
|
|
{ |
|
177
|
29 |
|
$this->http_client->setTest($test); |
|
178
|
29 |
|
return $this; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @return bool |
|
183
|
|
|
*/ |
|
184
|
1 |
|
public function isExpired(): bool |
|
185
|
|
|
{ |
|
186
|
1 |
|
return $this->http_client->isExpired(); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @return int |
|
191
|
|
|
*/ |
|
192
|
2 |
|
public function getExpire(): int |
|
193
|
|
|
{ |
|
194
|
2 |
|
return $this->http_client->getExpire(); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @param int $timestamp |
|
199
|
|
|
* @return self |
|
200
|
|
|
*/ |
|
201
|
1 |
|
public function setExpire(int $timestamp): self |
|
202
|
|
|
{ |
|
203
|
1 |
|
$this->http_client->setExpire($timestamp); |
|
204
|
1 |
|
return $this; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Авторизация клиента в сервисе Интеграции |
|
209
|
|
|
* @return bool |
|
210
|
|
|
* @throws AuthException |
|
211
|
|
|
* @throws Exceptions\RequestException |
|
212
|
|
|
*/ |
|
213
|
1 |
|
public function authorize(): bool |
|
214
|
|
|
{ |
|
215
|
1 |
|
return $this->http_client->authorize(); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @return Intakes |
|
220
|
|
|
*/ |
|
221
|
4 |
|
public function intakes(): Intakes |
|
222
|
|
|
{ |
|
223
|
4 |
|
if ($this->intakes === null) { |
|
224
|
4 |
|
$this->intakes = new Intakes($this->http_client, $this->serializer); |
|
225
|
|
|
} |
|
226
|
4 |
|
return $this->intakes; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @return Orders |
|
231
|
|
|
*/ |
|
232
|
4 |
|
public function orders(): Orders |
|
233
|
|
|
{ |
|
234
|
4 |
|
if ($this->orders === null) { |
|
235
|
4 |
|
$this->orders = new Orders($this->http_client, $this->serializer); |
|
236
|
|
|
} |
|
237
|
4 |
|
return $this->orders; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* @return Offices |
|
242
|
|
|
*/ |
|
243
|
6 |
|
public function offices(): Offices |
|
244
|
|
|
{ |
|
245
|
6 |
|
if ($this->offices === null) { |
|
246
|
6 |
|
$this->offices = new Offices($this->http_client, $this->serializer); |
|
247
|
|
|
} |
|
248
|
6 |
|
return $this->offices; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* @return LocationRegions |
|
253
|
|
|
*/ |
|
254
|
2 |
|
public function regions(): LocationRegions |
|
255
|
|
|
{ |
|
256
|
2 |
|
if ($this->regions === null) { |
|
257
|
2 |
|
$this->regions = new LocationRegions($this->http_client, $this->serializer); |
|
258
|
|
|
} |
|
259
|
2 |
|
return $this->regions; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* @return LocationCities |
|
264
|
|
|
*/ |
|
265
|
2 |
|
public function cities(): LocationCities |
|
266
|
|
|
{ |
|
267
|
2 |
|
if ($this->cities === null) { |
|
268
|
2 |
|
$this->cities = new LocationCities($this->http_client, $this->serializer); |
|
269
|
|
|
} |
|
270
|
2 |
|
return $this->cities; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* @return Webhooks |
|
275
|
|
|
*/ |
|
276
|
7 |
|
public function webhooks(): Webhooks |
|
277
|
|
|
{ |
|
278
|
7 |
|
if ($this->webhooks === null) { |
|
279
|
7 |
|
$this->webhooks = new Webhooks($this->http_client, $this->serializer); |
|
280
|
|
|
} |
|
281
|
7 |
|
return $this->webhooks; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* @return Invoices |
|
286
|
|
|
*/ |
|
287
|
3 |
|
public function invoice(): Invoices |
|
288
|
|
|
{ |
|
289
|
3 |
|
if ($this->invoices === null) { |
|
290
|
3 |
|
$this->invoices = new Invoices($this->http_client, $this->serializer); |
|
291
|
|
|
} |
|
292
|
3 |
|
return $this->invoices; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* @return Barcodes |
|
297
|
|
|
*/ |
|
298
|
3 |
|
public function barcodes(): Barcodes |
|
299
|
|
|
{ |
|
300
|
3 |
|
if ($this->barcodes === null) { |
|
301
|
3 |
|
$this->barcodes = new Barcodes($this->http_client, $this->serializer); |
|
302
|
|
|
} |
|
303
|
3 |
|
return $this->barcodes; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* @param ApiResponse $response |
|
308
|
|
|
* @param string $className |
|
309
|
|
|
* @return Response |
|
310
|
|
|
* @throws \Exception |
|
311
|
|
|
*/ |
|
312
|
2 |
|
public function formatResponse(ApiResponse $response, string $className): Response |
|
313
|
|
|
{ |
|
314
|
2 |
|
if (class_exists($className)) { |
|
315
|
|
|
/* @var $result Response */ |
|
316
|
1 |
|
$result = $this->serializer->deserialize($response->getBody(), Response::class, 'json'); |
|
317
|
1 |
|
$result->entity = null; |
|
318
|
|
|
|
|
319
|
1 |
|
$array_response = json_decode($response->getBody(), true); |
|
320
|
1 |
|
$entity = $this->serializer->deserialize(json_encode($array_response['entity']), $className, 'json'); |
|
321
|
1 |
|
$result->entity = $entity; |
|
322
|
|
|
|
|
323
|
1 |
|
return $result; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
1 |
|
throw new ParsingException('Class ' . $className . ' not found'); |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
/** |
|
330
|
|
|
* @param ApiResponse $response |
|
331
|
|
|
* @param string $className |
|
332
|
|
|
* @return CityList|RegionList|PickupPointList|WebHookList |
|
333
|
|
|
* @throws \Exception |
|
334
|
|
|
*/ |
|
335
|
2 |
|
public function formatResponseList(ApiResponse $response, string $className) |
|
336
|
|
|
{ |
|
337
|
2 |
|
if (class_exists($className)) { |
|
338
|
1 |
|
$body = '{"items":' . $response->getBody() . '}'; |
|
339
|
1 |
|
$result = $this->serializer->deserialize($body, $className, 'json'); |
|
340
|
1 |
|
return $result; |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
1 |
|
throw new ParsingException('Class ' . $className . ' not found'); |
|
344
|
|
|
} |
|
345
|
|
|
} |
|
346
|
|
|
|