1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Lamoda\IsmpClient\V4; |
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\FacadeDocListV2Query; |
18
|
|
|
use Lamoda\IsmpClient\V3\Dto\FacadeDocListV2Response; |
19
|
|
|
use Lamoda\IsmpClient\V3\Dto\FacadeMarkedProductsResponse; |
20
|
|
|
use Lamoda\IsmpClient\V3\Dto\ProductInfoResponse; |
21
|
|
|
use Lamoda\IsmpClient\V3\IsmpApi as IsmpApiV3; |
22
|
|
|
use Lamoda\IsmpClient\V3\IsmpApiInterface as IsmpApiV3Interface; |
23
|
|
|
use Lamoda\IsmpClient\V4\Dto\FacadeCisListRequest; |
24
|
|
|
use Lamoda\IsmpClient\V4\Dto\FacadeCisListResponse; |
25
|
|
|
use Lamoda\IsmpClient\V4\Dto\FacadeDocBodyResponse; |
26
|
|
|
|
27
|
|
|
class IsmpApi implements IsmpApiInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var ClientInterface |
31
|
|
|
*/ |
32
|
|
|
private $client; |
33
|
|
|
/** |
34
|
|
|
* @var SerializerInterface |
35
|
|
|
*/ |
36
|
|
|
private $serializer; |
37
|
|
|
/** |
38
|
|
|
* @var IsmpApiV3|IsmpApiV3Interface |
39
|
|
|
*/ |
40
|
|
|
private $ismpApiV3; |
41
|
|
|
|
42
|
15 |
|
public function __construct(ClientInterface $client, SerializerInterface $serializer, ?IsmpApiV3Interface $ismpApiV3 = null) |
43
|
|
|
{ |
44
|
15 |
|
$this->client = $client; |
45
|
15 |
|
$this->serializer = $serializer; |
46
|
15 |
|
$this->ismpApiV3 = $ismpApiV3 ?? new IsmpApiV3($client, $serializer); |
47
|
15 |
|
} |
48
|
|
|
|
49
|
6 |
|
public function facadeDocBody(string $token, string $docId, ?int $limit = null, ?string $orderColumn = null, ?string $orderedColumnValue = null, ?string $pageDir = null): FacadeDocBodyResponse |
50
|
|
|
{ |
51
|
6 |
|
$query = null; |
52
|
6 |
|
if ($limit !== null) { |
53
|
2 |
|
$query['limit'] = $limit; |
54
|
|
|
} |
55
|
6 |
|
if ($orderColumn !== null) { |
56
|
2 |
|
$query['orderColumn'] = $orderColumn; |
57
|
|
|
} |
58
|
6 |
|
if ($orderedColumnValue !== null) { |
59
|
2 |
|
$query['orderedColumnValue'] = $orderedColumnValue; |
60
|
|
|
} |
61
|
6 |
|
if ($pageDir !== null) { |
62
|
2 |
|
$query['pageDir'] = $pageDir; |
63
|
|
|
} |
64
|
|
|
|
65
|
6 |
|
$result = $this->request('GET', sprintf('/api/v4/facade/doc/%s/body', $docId), null, $query, $token); |
66
|
|
|
|
67
|
|
|
/* @noinspection PhpIncompatibleReturnTypeInspection */ |
68
|
6 |
|
return $this->serializer->deserialize(FacadeDocBodyResponse::class, $result); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
public function facadeCisList(string $token, FacadeCisListRequest $request): FacadeCisListResponse |
72
|
|
|
{ |
73
|
1 |
|
$body = $this->serializer->serialize($request); |
|
|
|
|
74
|
1 |
|
$response = $this->request('POST', '/api/v4/facade/cis/cis_list', $body, null, $token); |
75
|
|
|
|
76
|
|
|
/* @noinspection PhpIncompatibleReturnTypeInspection */ |
77
|
1 |
|
return $this->serializer->deserialize(FacadeCisListResponse::class, $response); |
78
|
|
|
} |
79
|
|
|
|
80
|
3 |
|
public function authCertKey(): AuthCertKeyResponse |
81
|
|
|
{ |
82
|
3 |
|
return $this->ismpApiV3->authCertKey(); |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
public function authCert(AuthCertRequest $request, ?string $connection = null): AuthCertResponse |
86
|
|
|
{ |
87
|
2 |
|
return $this->ismpApiV3->authCert($request, $connection); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
public function facadeDocListV2(string $token, FacadeDocListV2Query $query): FacadeDocListV2Response |
91
|
|
|
{ |
92
|
1 |
|
return $this->ismpApiV3->facadeDocListV2($token, $query); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
public function lkDocumentsCreate(string $token, DocumentCreateRequest $request): string |
96
|
|
|
{ |
97
|
1 |
|
return $this->ismpApiV3->lkDocumentsCreate($token, $request); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
public function productInfo(string $token, array $gtins): ProductInfoResponse |
101
|
|
|
{ |
102
|
1 |
|
return $this->ismpApiV3->productInfo($token, $gtins); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function facadeMarkedProducts(string $token, string $cis): FacadeMarkedProductsResponse |
106
|
|
|
{ |
107
|
|
|
return $this->ismpApiV3->facadeMarkedProducts($token, $cis); |
108
|
|
|
} |
109
|
|
|
|
110
|
7 |
|
private function request(string $method, string $uri, $body = null, $query = null, string $token = null): string |
111
|
|
|
{ |
112
|
|
|
$options = [ |
113
|
7 |
|
RequestOptions::BODY => $body, |
114
|
|
|
RequestOptions::HEADERS => [ |
115
|
|
|
'Content-Type' => 'application/json', |
116
|
|
|
], |
117
|
|
|
RequestOptions::HTTP_ERRORS => true, |
118
|
7 |
|
RequestOptions::QUERY => $query, |
119
|
|
|
]; |
120
|
|
|
|
121
|
7 |
|
if ($token) { |
|
|
|
|
122
|
7 |
|
$options[RequestOptions::HEADERS]['Authorization'] = 'Bearer ' . $token; |
123
|
|
|
} |
124
|
|
|
|
125
|
7 |
|
$uri = ltrim($uri, '/'); |
126
|
|
|
|
127
|
|
|
try { |
128
|
7 |
|
$result = $this->client->request($method, $uri, $options); |
129
|
|
|
} catch (\Throwable $exception) { |
130
|
|
|
/* @noinspection PhpUnhandledExceptionInspection */ |
131
|
|
|
throw $this->handleRequestException($exception); |
132
|
|
|
} |
133
|
|
|
|
134
|
7 |
|
return (string)$result->getBody(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
private function handleRequestException(\Throwable $exception): \Throwable |
138
|
|
|
{ |
139
|
|
|
if ($exception instanceof BadResponseException) { |
140
|
|
|
$response = $exception->getResponse(); |
141
|
|
|
$responseBody = $response ? (string)$response->getBody() : ''; |
142
|
|
|
$responseCode = $response ? $response->getStatusCode() : 0; |
143
|
|
|
|
144
|
|
|
return IsmpRequestErrorException::becauseOfErrorResponse($responseCode, $responseBody, $exception); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return IsmpGeneralErrorException::becauseOfError($exception); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
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: