|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Lamoda\OmsClient\V2; |
|
6
|
|
|
|
|
7
|
|
|
use GuzzleHttp\ClientInterface; |
|
8
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
|
9
|
|
|
use GuzzleHttp\RequestOptions; |
|
10
|
|
|
use Lamoda\OmsClient\Exception\OmsClientExceptionInterface; |
|
11
|
|
|
use Lamoda\OmsClient\Exception\OmsGeneralErrorException; |
|
12
|
|
|
use Lamoda\OmsClient\Exception\OmsRequestErrorException; |
|
13
|
|
|
use Lamoda\OmsClient\Serializer\SerializerInterface; |
|
14
|
|
|
use Lamoda\OmsClient\V2\Dto\CloseICArrayResponse; |
|
15
|
|
|
use Lamoda\OmsClient\V2\Dto\CreateOrderForEmissionICRequest; |
|
16
|
|
|
use Lamoda\OmsClient\V2\Dto\CreateOrderForEmissionICResponse; |
|
17
|
|
|
use Lamoda\OmsClient\V2\Dto\GetICBufferStatusResponse; |
|
18
|
|
|
use Lamoda\OmsClient\V2\Dto\GetICsFromOrderResponse; |
|
19
|
|
|
|
|
20
|
|
|
final class OmsApi |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var ClientInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $client; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var SerializerInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $serializer; |
|
30
|
|
|
|
|
31
|
8 |
|
public function __construct(ClientInterface $client, SerializerInterface $serializer) |
|
32
|
|
|
{ |
|
33
|
8 |
|
$this->client = $client; |
|
34
|
8 |
|
$this->serializer = $serializer; |
|
35
|
8 |
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
public function createOrderForEmissionIC( |
|
38
|
|
|
Extension $extension, |
|
39
|
|
|
string $token, |
|
40
|
|
|
string $omsId, |
|
41
|
|
|
CreateOrderForEmissionICRequest $request |
|
42
|
|
|
): CreateOrderForEmissionICResponse { |
|
43
|
1 |
|
$url = sprintf('/api/v2/%s/orders', (string)$extension); |
|
44
|
1 |
|
$body = $this->serializer->serialize($request); |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
1 |
|
$result = $this->request($token, 'POST', $url, [ |
|
47
|
1 |
|
'omsId' => $omsId, |
|
48
|
1 |
|
], $body); |
|
49
|
|
|
|
|
50
|
|
|
/* @noinspection PhpIncompatibleReturnTypeInspection */ |
|
51
|
1 |
|
return $this->serializer->deserialize(CreateOrderForEmissionICResponse::class, $result); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
5 |
|
public function getICBufferStatus( |
|
55
|
|
|
Extension $extension, |
|
56
|
|
|
string $token, |
|
57
|
|
|
string $omsId, |
|
58
|
|
|
string $orderId, |
|
59
|
|
|
string $gtin |
|
60
|
|
|
): GetICBufferStatusResponse { |
|
61
|
5 |
|
$url = sprintf('/api/v2/%s/buffer/status', (string)$extension); |
|
62
|
5 |
|
$result = $this->request($token, 'GET', $url, [ |
|
63
|
5 |
|
'omsId' => $omsId, |
|
64
|
5 |
|
'orderId' => $orderId, |
|
65
|
5 |
|
'gtin' => $gtin, |
|
66
|
|
|
]); |
|
67
|
|
|
|
|
68
|
|
|
/* @noinspection PhpIncompatibleReturnTypeInspection */ |
|
69
|
3 |
|
return $this->serializer->deserialize(GetICBufferStatusResponse::class, $result); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
View Code Duplication |
public function getICsFromOrder( |
|
|
|
|
|
|
73
|
|
|
Extension $extension, |
|
74
|
|
|
string $token, |
|
75
|
|
|
string $omsId, |
|
76
|
|
|
string $orderId, |
|
77
|
|
|
string $gtin, |
|
78
|
|
|
int $quantity, |
|
79
|
|
|
string $lastBlockId = '0' |
|
80
|
|
|
): GetICsFromOrderResponse { |
|
81
|
1 |
|
$url = sprintf('/api/v2/%s/codes', (string)$extension); |
|
82
|
1 |
|
$result = $this->request($token, 'GET', $url, [ |
|
83
|
1 |
|
'omsId' => $omsId, |
|
84
|
1 |
|
'orderId' => $orderId, |
|
85
|
1 |
|
'gtin' => $gtin, |
|
86
|
1 |
|
'quantity' => $quantity, |
|
87
|
1 |
|
'lastBlockId' => $lastBlockId, |
|
88
|
|
|
]); |
|
89
|
|
|
|
|
90
|
|
|
/* @noinspection PhpIncompatibleReturnTypeInspection */ |
|
91
|
1 |
|
return $this->serializer->deserialize(GetICsFromOrderResponse::class, $result); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
View Code Duplication |
public function closeICArray( |
|
|
|
|
|
|
95
|
|
|
Extension $extension, |
|
96
|
|
|
string $token, |
|
97
|
|
|
string $omsId, |
|
98
|
|
|
string $orderId, |
|
99
|
|
|
string $gtin, |
|
100
|
|
|
string $lastBlockId |
|
101
|
|
|
): CloseICArrayResponse { |
|
102
|
1 |
|
$url = sprintf('/api/v2/%s/buffer/close', (string)$extension); |
|
103
|
1 |
|
$result = $this->request($token, 'POST', $url, [ |
|
104
|
1 |
|
'omsId' => $omsId, |
|
105
|
1 |
|
'orderId' => $orderId, |
|
106
|
1 |
|
'gtin' => $gtin, |
|
107
|
1 |
|
'lastBlockId' => $lastBlockId, |
|
108
|
|
|
]); |
|
109
|
|
|
|
|
110
|
|
|
/* @noinspection PhpIncompatibleReturnTypeInspection */ |
|
111
|
1 |
|
return $this->serializer->deserialize(CloseICArrayResponse::class, $result); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @throws OmsRequestErrorException |
|
116
|
|
|
*/ |
|
117
|
8 |
View Code Duplication |
private function request(string $token, string $method, string $uri, array $query = [], $body = null): string |
|
|
|
|
|
|
118
|
|
|
{ |
|
119
|
|
|
$options = [ |
|
120
|
8 |
|
RequestOptions::BODY => $body, |
|
121
|
8 |
|
RequestOptions::HEADERS => [ |
|
122
|
8 |
|
'Content-Type' => 'application/json', |
|
123
|
8 |
|
'clientToken' => $token, |
|
124
|
|
|
], |
|
125
|
8 |
|
RequestOptions::QUERY => $query, |
|
126
|
8 |
|
RequestOptions::HTTP_ERRORS => true, |
|
127
|
|
|
]; |
|
128
|
|
|
|
|
129
|
8 |
|
$uri = ltrim($uri, '/'); |
|
130
|
|
|
|
|
131
|
|
|
try { |
|
132
|
8 |
|
$result = $this->client->request($method, $uri, $options); |
|
133
|
2 |
|
} catch (\Throwable $exception) { |
|
134
|
|
|
/* @noinspection PhpUnhandledExceptionInspection */ |
|
135
|
2 |
|
throw $this->handleRequestException($exception); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
6 |
|
return (string)$result->getBody(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
2 |
View Code Duplication |
private function handleRequestException(\Throwable $exception): OmsClientExceptionInterface |
|
|
|
|
|
|
142
|
|
|
{ |
|
143
|
2 |
|
if ($exception instanceof BadResponseException) { |
|
144
|
1 |
|
$response = $exception->getResponse(); |
|
145
|
1 |
|
$responseBody = $response ? (string)$response->getBody() : ''; |
|
146
|
1 |
|
$responseCode = $response ? $response->getStatusCode() : 0; |
|
147
|
|
|
|
|
148
|
1 |
|
return OmsRequestErrorException::becauseOfError($responseCode, $responseBody, $exception); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
1 |
|
return OmsGeneralErrorException::becauseOfError($exception); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
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: