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\Intakes; |
13
|
|
|
use CdekSDK2\Actions\LocationCities; |
14
|
|
|
use CdekSDK2\Actions\LocationRegions; |
15
|
|
|
use CdekSDK2\Actions\Offices; |
16
|
|
|
use CdekSDK2\Actions\Orders; |
17
|
|
|
use CdekSDK2\Actions\Webhooks; |
18
|
|
|
use CdekSDK2\Exceptions\AuthException; |
19
|
|
|
use CdekSDK2\Exceptions\ParsingException; |
20
|
|
|
use CdekSDK2\Http\Api; |
21
|
|
|
use CdekSDK2\Http\ApiResponse; |
22
|
|
|
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy; |
23
|
|
|
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy; |
24
|
|
|
use JMS\Serializer\Serializer; |
25
|
|
|
use JMS\Serializer\SerializerBuilder; |
26
|
|
|
use Psr\Http\Client\ClientInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class Client |
30
|
|
|
* @package CdekSDK2 |
31
|
|
|
*/ |
32
|
|
|
class Client |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Объект для взаимодействия с API СДЭК |
36
|
|
|
* @var Api |
37
|
|
|
*/ |
38
|
|
|
private $http_client; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Serializer |
42
|
|
|
*/ |
43
|
|
|
private $serializer; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Orders |
47
|
|
|
*/ |
48
|
|
|
private $orders; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var Intakes |
52
|
|
|
*/ |
53
|
|
|
private $intakes; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var Webhooks |
57
|
|
|
*/ |
58
|
|
|
private $webhooks; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var Offices |
62
|
|
|
*/ |
63
|
|
|
private $offices; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var LocationRegions |
67
|
|
|
*/ |
68
|
|
|
private $regions; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var LocationCities |
72
|
|
|
*/ |
73
|
|
|
private $cities; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Client constructor. |
77
|
|
|
* @param ClientInterface $http |
78
|
|
|
* @param string|null $account |
79
|
|
|
* @param string|null $secure |
80
|
|
|
* @psalm-suppress PropertyTypeCoercion |
81
|
|
|
*/ |
82
|
31 |
|
public function __construct(ClientInterface $http, string $account = null, string $secure = null) |
83
|
|
|
{ |
84
|
31 |
|
$this->http_client = new Api($http, $account, $secure); |
85
|
31 |
|
$this->serializer = SerializerBuilder::create()->setPropertyNamingStrategy( |
86
|
31 |
|
new SerializedNameAnnotationStrategy( |
87
|
31 |
|
new IdenticalPropertyNamingStrategy() |
88
|
|
|
) |
89
|
31 |
|
)->build(); |
90
|
31 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
3 |
|
public function getAccount(): string |
96
|
|
|
{ |
97
|
3 |
|
return $this->http_client->getAccount(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $account |
102
|
|
|
* @return self |
103
|
|
|
*/ |
104
|
2 |
|
public function setAccount(string $account): self |
105
|
|
|
{ |
106
|
2 |
|
$this->http_client->setAccount($account); |
107
|
2 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
2 |
|
public function getToken(): string |
114
|
|
|
{ |
115
|
2 |
|
return $this->http_client->getToken(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $token |
120
|
|
|
* @return self |
121
|
|
|
*/ |
122
|
1 |
|
public function setToken(string $token): self |
123
|
|
|
{ |
124
|
1 |
|
$this->http_client->setToken($token); |
125
|
1 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
2 |
|
public function getSecure(): string |
132
|
|
|
{ |
133
|
2 |
|
return $this->http_client->getSecure(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $secure |
138
|
|
|
* @return self |
139
|
|
|
*/ |
140
|
1 |
|
public function setSecure(string $secure): self |
141
|
|
|
{ |
142
|
1 |
|
$this->http_client->setSecure($secure); |
143
|
1 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
2 |
|
public function isTest(): bool |
150
|
|
|
{ |
151
|
2 |
|
return $this->http_client->isTest(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param bool $test |
156
|
|
|
* @return self |
157
|
|
|
*/ |
158
|
19 |
|
public function setTest(bool $test): self |
159
|
|
|
{ |
160
|
19 |
|
$this->http_client->setTest($test); |
161
|
19 |
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
1 |
|
public function isExpired(): bool |
168
|
|
|
{ |
169
|
1 |
|
return $this->http_client->isExpired(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return int |
174
|
|
|
*/ |
175
|
1 |
|
public function getExpire(): int |
176
|
|
|
{ |
177
|
1 |
|
return $this->http_client->getExpire(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Авторизация клиента в сервисе Интеграции |
182
|
|
|
* @return bool |
183
|
|
|
* @throws AuthException |
184
|
|
|
* @throws Exceptions\RequestException |
185
|
|
|
*/ |
186
|
1 |
|
public function authorize(): bool |
187
|
|
|
{ |
188
|
1 |
|
return $this->http_client->authorize(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @return Intakes |
193
|
|
|
*/ |
194
|
4 |
|
public function intakes(): Intakes |
195
|
|
|
{ |
196
|
4 |
|
if ($this->intakes === null) { |
197
|
4 |
|
$this->intakes = new Intakes($this->http_client, $this->serializer); |
198
|
|
|
} |
199
|
4 |
|
return $this->intakes; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return Orders |
204
|
|
|
*/ |
205
|
4 |
|
public function orders(): Orders |
206
|
|
|
{ |
207
|
4 |
|
if ($this->orders === null) { |
208
|
4 |
|
$this->orders = new Orders($this->http_client, $this->serializer); |
209
|
|
|
} |
210
|
4 |
|
return $this->orders; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return Offices |
215
|
|
|
*/ |
216
|
3 |
|
public function offices(): Offices |
217
|
|
|
{ |
218
|
3 |
|
if ($this->offices === null) { |
219
|
3 |
|
$this->offices = new Offices($this->http_client, $this->serializer); |
220
|
|
|
} |
221
|
3 |
|
return $this->offices; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return LocationRegions |
226
|
|
|
*/ |
227
|
2 |
|
public function regions(): LocationRegions |
228
|
|
|
{ |
229
|
2 |
|
if ($this->regions === null) { |
230
|
2 |
|
$this->regions = new LocationRegions($this->http_client, $this->serializer); |
231
|
|
|
} |
232
|
2 |
|
return $this->regions; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @return LocationCities |
237
|
|
|
*/ |
238
|
2 |
|
public function cities(): LocationCities |
239
|
|
|
{ |
240
|
2 |
|
if ($this->cities === null) { |
241
|
2 |
|
$this->cities = new LocationCities($this->http_client, $this->serializer); |
242
|
|
|
} |
243
|
2 |
|
return $this->cities; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return Webhooks |
248
|
|
|
*/ |
249
|
6 |
|
public function webhooks(): Webhooks |
250
|
|
|
{ |
251
|
6 |
|
if ($this->webhooks === null) { |
252
|
6 |
|
$this->webhooks = new Webhooks($this->http_client, $this->serializer); |
253
|
|
|
} |
254
|
6 |
|
return $this->webhooks; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param ApiResponse $response |
259
|
|
|
* @param string $className |
260
|
|
|
* @return mixed |
261
|
|
|
* @throws \Exception |
262
|
|
|
*/ |
263
|
3 |
|
public function formatResponse(ApiResponse $response, string $className) |
264
|
|
|
{ |
265
|
3 |
|
if (class_exists($className)) { |
266
|
2 |
|
$result = $this->serializer->deserialize($response->getBody(), $className, 'json'); |
267
|
2 |
|
return $result; |
268
|
|
|
} else { |
269
|
1 |
|
throw new ParsingException('Class ' . $className . ' not found'); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|