1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Lamoda\IsmpClient\V3; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\ClientInterface; |
8
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
9
|
|
|
use GuzzleHttp\RequestOptions; |
10
|
|
|
use Lamoda\IsmpClient\Exception\IsmpGeneralErrorException; |
11
|
|
|
use Lamoda\IsmpClient\Exception\IsmpRequestErrorException; |
12
|
|
|
use Lamoda\IsmpClient\Serializer\SerializerInterface; |
13
|
|
|
use Lamoda\IsmpClient\V3\Dto\AuthCertKeyResponse; |
14
|
|
|
use Lamoda\IsmpClient\V3\Dto\AuthCertRequest; |
15
|
|
|
use Lamoda\IsmpClient\V3\Dto\AuthCertResponse; |
16
|
|
|
use Lamoda\IsmpClient\V3\Dto\DocumentCreateRequest; |
17
|
|
|
use Lamoda\IsmpClient\V3\Dto\FacadeCisListRequest; |
18
|
|
|
use Lamoda\IsmpClient\V3\Dto\FacadeCisListResponse; |
19
|
|
|
use Lamoda\IsmpClient\V3\Dto\FacadeDocBodyResponse; |
20
|
|
|
use Lamoda\IsmpClient\V3\Dto\FacadeDocListV2Query; |
21
|
|
|
use Lamoda\IsmpClient\V3\Dto\FacadeDocListV2Response; |
22
|
|
|
use Lamoda\IsmpClient\V3\Dto\FacadeMarkedProductsResponse; |
23
|
|
|
use Lamoda\IsmpClient\V3\Dto\FacadeOrderDetailsResponse; |
24
|
|
|
use Lamoda\IsmpClient\V3\Dto\FacadeOrderRequest; |
25
|
|
|
use Lamoda\IsmpClient\V3\Dto\FacadeOrderResponse; |
26
|
|
|
use Lamoda\IsmpClient\V3\Dto\ProductInfoResponse; |
27
|
|
|
|
28
|
|
|
final class IsmpApi implements IsmpApiInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var ClientInterface |
32
|
|
|
*/ |
33
|
|
|
private $client; |
34
|
|
|
/** |
35
|
|
|
* @var SerializerInterface |
36
|
|
|
*/ |
37
|
|
|
private $serializer; |
38
|
|
|
|
39
|
36 |
|
public function __construct(ClientInterface $client, SerializerInterface $serializer) |
40
|
|
|
{ |
41
|
36 |
|
$this->client = $client; |
42
|
36 |
|
$this->serializer = $serializer; |
43
|
36 |
|
} |
44
|
|
|
|
45
|
6 |
|
public function authCertKey(): AuthCertKeyResponse |
46
|
|
|
{ |
47
|
6 |
|
$result = $this->request('GET', '/api/v3/auth/cert/key'); |
48
|
|
|
|
49
|
|
|
/* @noinspection PhpIncompatibleReturnTypeInspection */ |
50
|
2 |
|
return $this->serializer->deserialize(AuthCertKeyResponse::class, $result); |
51
|
|
|
} |
52
|
|
|
|
53
|
4 |
|
public function authCert(AuthCertRequest $request, ?string $connection = null): AuthCertResponse |
54
|
|
|
{ |
55
|
4 |
|
$body = $this->serializer->serialize($request); |
|
|
|
|
56
|
4 |
|
$result = $this->request( |
57
|
4 |
|
'POST', |
58
|
4 |
|
sprintf('/api/v3/auth/cert/%s', $connection), |
59
|
4 |
|
$body |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
/* @noinspection PhpIncompatibleReturnTypeInspection */ |
63
|
4 |
|
return $this->serializer->deserialize(AuthCertResponse::class, $result); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
public function facadeOrder(string $token, FacadeOrderRequest $request): FacadeOrderResponse |
67
|
|
|
{ |
68
|
1 |
|
$body = $this->serializer->serialize($request); |
|
|
|
|
69
|
1 |
|
$result = $this->request('POST', '/api/v3/facade/order/', $body, null, $token); |
70
|
|
|
|
71
|
|
|
/* @noinspection PhpIncompatibleReturnTypeInspection */ |
72
|
1 |
|
return $this->serializer->deserialize(FacadeOrderResponse::class, $result); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
public function facadeOrderDetails(string $token, string $orderId): FacadeOrderDetailsResponse |
76
|
|
|
{ |
77
|
1 |
|
$result = $this->request('GET', sprintf('/api/v3/facade/order/%s/details', $orderId), null, null, $token); |
78
|
|
|
|
79
|
|
|
/* @noinspection PhpIncompatibleReturnTypeInspection */ |
80
|
1 |
|
return $this->serializer->deserialize(FacadeOrderDetailsResponse::class, $result); |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
public function facadeDocListV2(string $token, FacadeDocListV2Query $query): FacadeDocListV2Response |
84
|
|
|
{ |
85
|
2 |
|
$result = $this->request('GET', '/api/v3/facade/doc/listV2', null, $query->toQueryArray(), $token); |
86
|
|
|
|
87
|
|
|
/* @noinspection PhpIncompatibleReturnTypeInspection */ |
88
|
2 |
|
return $this->serializer->deserialize(FacadeDocListV2Response::class, $result); |
89
|
|
|
} |
90
|
|
|
|
91
|
6 |
|
public function facadeDocBody( |
92
|
|
|
string $token, |
93
|
|
|
string $docId, |
94
|
|
|
?int $limit = null, |
95
|
|
|
?string $orderColumn = null, |
96
|
|
|
?string $orderedColumnValue = null, |
97
|
|
|
?string $pageDir = null |
98
|
|
|
): FacadeDocBodyResponse { |
99
|
6 |
|
$query = null; |
100
|
6 |
|
if ($limit !== null) { |
101
|
2 |
|
$query['limit'] = $limit; |
102
|
|
|
} |
103
|
6 |
|
if ($orderColumn !== null) { |
104
|
2 |
|
$query['orderColumn'] = $orderColumn; |
105
|
|
|
} |
106
|
6 |
|
if ($orderedColumnValue !== null) { |
107
|
2 |
|
$query['orderedColumnValue'] = $orderedColumnValue; |
108
|
|
|
} |
109
|
6 |
|
if ($pageDir !== null) { |
110
|
2 |
|
$query['pageDir'] = $pageDir; |
111
|
|
|
} |
112
|
|
|
|
113
|
6 |
|
$result = $this->request('GET', sprintf('/api/v3/facade/doc/%s/body', $docId), null, $query, $token); |
114
|
|
|
|
115
|
|
|
/* @noinspection PhpIncompatibleReturnTypeInspection */ |
116
|
6 |
|
return $this->serializer->deserialize(FacadeDocBodyResponse::class, $result); |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
public function facadeCisList(string $token, FacadeCisListRequest $request): FacadeCisListResponse |
120
|
|
|
{ |
121
|
1 |
|
$body = $this->serializer->serialize($request); |
|
|
|
|
122
|
1 |
|
$response = $this->request('POST', '/api/v3/facade/cis/cis_list', $body, null, $token); |
123
|
|
|
|
124
|
|
|
/* @noinspection PhpIncompatibleReturnTypeInspection */ |
125
|
1 |
|
return $this->serializer->deserialize(FacadeCisListResponse::class, $response); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function facadeMarkedProducts(string $token, string $cis): FacadeMarkedProductsResponse |
129
|
|
|
{ |
130
|
|
|
$response = $this->request('GET', '/api/v3/facade/marked_products/info', null, ['cis' => $cis], $token); |
131
|
|
|
|
132
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
133
|
|
|
return $this->serializer->deserialize(FacadeMarkedProductsResponse::class, $response); |
134
|
|
|
} |
135
|
|
|
|
136
|
2 |
|
public function lkDocumentsCreate(string $token, DocumentCreateRequest $request): string |
137
|
|
|
{ |
138
|
2 |
|
assert($request->getType() !== null, 'Document type is required for lkDocumentsCreate'); |
139
|
2 |
|
assert($request->getProductGroup() !== null, 'Product group is required for lkDocumentsCreate'); |
140
|
|
|
|
141
|
2 |
|
$body = $this->serializer->serialize($request); |
|
|
|
|
142
|
|
|
|
143
|
2 |
|
return $this->request( |
144
|
2 |
|
'POST', |
145
|
2 |
|
'/api/v3/lk/documents/create', |
146
|
2 |
|
$body, |
147
|
2 |
|
['pg' => $request->getProductGroup()], |
148
|
2 |
|
$token |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
public function lkImportSend(string $token, DocumentCreateRequest $request): string |
153
|
|
|
{ |
154
|
1 |
|
$body = $this->serializer->serialize($request); |
|
|
|
|
155
|
|
|
|
156
|
1 |
|
return $this->request('POST', '/api/v3/lk/import/send', $body, null, $token); |
157
|
|
|
} |
158
|
|
|
|
159
|
1 |
|
public function lkReceiptSend(string $token, DocumentCreateRequest $request): string |
160
|
|
|
{ |
161
|
1 |
|
$body = $this->serializer->serialize($request); |
|
|
|
|
162
|
|
|
|
163
|
1 |
|
return $this->request('POST', '/api/v3/lk/receipt/send', $body, null, $token); |
164
|
|
|
} |
165
|
|
|
|
166
|
1 |
|
public function lkDocumentsShipmentCreate(string $token, DocumentCreateRequest $request): string |
167
|
|
|
{ |
168
|
1 |
|
$body = $this->serializer->serialize($request); |
|
|
|
|
169
|
|
|
|
170
|
1 |
|
return $this->request('POST', '/api/v3/lk/documents/shipment/create', $body, null, $token); |
171
|
|
|
} |
172
|
|
|
|
173
|
1 |
|
public function lkDocumentsAcceptanceCreate(string $token, DocumentCreateRequest $request): string |
174
|
|
|
{ |
175
|
1 |
|
$body = $this->serializer->serialize($request); |
|
|
|
|
176
|
|
|
|
177
|
1 |
|
return $this->request('POST', '/api/v3/lk/documents/acceptance/create', $body, null, $token); |
178
|
|
|
} |
179
|
|
|
|
180
|
2 |
|
public function productInfo(string $token, array $gtins): ProductInfoResponse |
181
|
|
|
{ |
182
|
2 |
|
$gtinsList = implode(',', $gtins); |
183
|
|
|
|
184
|
2 |
|
$result = $this->request('GET', 'api/v3/product/info', null, [ |
185
|
2 |
|
'gtins' => $gtinsList, |
186
|
2 |
|
], $token); |
187
|
|
|
|
188
|
|
|
/* @noinspection PhpIncompatibleReturnTypeInspection */ |
189
|
2 |
|
return $this->serializer->deserialize(ProductInfoResponse::class, $result); |
190
|
|
|
} |
191
|
|
|
|
192
|
29 |
|
private function request(string $method, string $uri, $body = null, $query = null, string $token = null): string |
193
|
|
|
{ |
194
|
|
|
$options = [ |
195
|
29 |
|
RequestOptions::BODY => $body, |
196
|
|
|
RequestOptions::HEADERS => [ |
197
|
|
|
'Content-Type' => 'application/json', |
198
|
|
|
], |
199
|
|
|
RequestOptions::HTTP_ERRORS => true, |
200
|
29 |
|
RequestOptions::QUERY => $query, |
201
|
|
|
]; |
202
|
|
|
|
203
|
29 |
|
if ($token) { |
|
|
|
|
204
|
19 |
|
$options[RequestOptions::HEADERS]['Authorization'] = 'Bearer ' . $token; |
205
|
|
|
} |
206
|
|
|
|
207
|
29 |
|
$uri = ltrim($uri, '/'); |
208
|
|
|
|
209
|
|
|
try { |
210
|
29 |
|
$result = $this->client->request($method, $uri, $options); |
211
|
4 |
|
} catch (\Throwable $exception) { |
212
|
|
|
/* @noinspection PhpUnhandledExceptionInspection */ |
213
|
4 |
|
throw $this->handleRequestException($exception); |
214
|
|
|
} |
215
|
|
|
|
216
|
25 |
|
return (string)$result->getBody(); |
217
|
|
|
} |
218
|
|
|
|
219
|
4 |
|
private function handleRequestException(\Throwable $exception): \Throwable |
220
|
|
|
{ |
221
|
4 |
|
if ($exception instanceof BadResponseException) { |
222
|
2 |
|
$response = $exception->getResponse(); |
223
|
2 |
|
$responseBody = $response ? (string)$response->getBody() : ''; |
224
|
2 |
|
$responseCode = $response ? $response->getStatusCode() : 0; |
225
|
|
|
|
226
|
2 |
|
return IsmpRequestErrorException::becauseOfErrorResponse($responseCode, $responseBody, $exception); |
227
|
|
|
} |
228
|
|
|
|
229
|
2 |
|
return IsmpGeneralErrorException::becauseOfError($exception); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: