Passed
Push — master ( 7b8966...1286dc )
by
unknown
55s queued 11s
created

IsmpApi::lkImportSend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 10
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 2
crap 1
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 20
    public function __construct(ClientInterface $client, SerializerInterface $serializer)
40
    {
41 20
        $this->client = $client;
42 20
        $this->serializer = $serializer;
43 20
    }
44
45 3
    public function authCertKey(): AuthCertKeyResponse
46
    {
47 3
        $result = $this->request('GET', '/api/v3/auth/cert/key');
48
49
        /* @noinspection PhpIncompatibleReturnTypeInspection */
50 1
        return $this->serializer->deserialize(AuthCertKeyResponse::class, $result);
51
    }
52
53 1
    public function authCert(AuthCertRequest $request): AuthCertResponse
54
    {
55 1
        $body = $this->serializer->serialize($request);
0 ignored issues
show
Documentation introduced by
$request is of type object<Lamoda\IsmpClient\V3\Dto\AuthCertRequest>, but the function expects a object<Lamoda\IsmpClient\Serializer\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
56 1
        $result = $this->request('POST', '/api/v3/auth/cert/', $body);
57
58
        /* @noinspection PhpIncompatibleReturnTypeInspection */
59 1
        return $this->serializer->deserialize(AuthCertResponse::class, $result);
60
    }
61
62 1
    public function facadeOrder(string $token, FacadeOrderRequest $request): FacadeOrderResponse
63
    {
64 1
        $body = $this->serializer->serialize($request);
0 ignored issues
show
Documentation introduced by
$request is of type object<Lamoda\IsmpClient...Dto\FacadeOrderRequest>, but the function expects a object<Lamoda\IsmpClient\Serializer\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
65 1
        $result = $this->request('POST', '/api/v3/facade/order/', $body, null, $token);
66
67
        /* @noinspection PhpIncompatibleReturnTypeInspection */
68 1
        return $this->serializer->deserialize(FacadeOrderResponse::class, $result);
69
    }
70
71 1
    public function facadeOrderDetails(string $token, string $orderId): FacadeOrderDetailsResponse
72
    {
73 1
        $result = $this->request('GET', sprintf('/api/v3/facade/order/%s/details', $orderId), null, null, $token);
74
75
        /* @noinspection PhpIncompatibleReturnTypeInspection */
76 1
        return $this->serializer->deserialize(FacadeOrderDetailsResponse::class, $result);
77
    }
78
79 1
    public function facadeDocListV2(string $token, FacadeDocListV2Query $query): FacadeDocListV2Response
80
    {
81 1
        $result = $this->request('GET', '/api/v3/facade/doc/listV2', null, $query->toQueryArray(), $token);
82
83
        /* @noinspection PhpIncompatibleReturnTypeInspection */
84 1
        return $this->serializer->deserialize(FacadeDocListV2Response::class, $result);
85
    }
86
87 6
    public function facadeDocBody(
88
        string $token,
89
        string $docId,
90
        ?int $limit = null,
91
        ?string $orderColumn = null,
92
        ?string $orderedColumnValue = null,
93
        ?string $pageDir = null
94
    ): FacadeDocBodyResponse {
95 6
        $query = null;
96 6
        if ($limit !== null) {
97 2
            $query['limit'] = $limit;
98
        }
99 6
        if ($orderColumn !== null) {
100 2
            $query['orderColumn'] = $orderColumn;
101
        }
102 6
        if ($orderedColumnValue !== null) {
103 2
            $query['orderedColumnValue'] = $orderedColumnValue;
104
        }
105 6
        if ($pageDir !== null) {
106 2
            $query['pageDir'] = $pageDir;
107
        }
108
109 6
        $result = $this->request('GET', sprintf('/api/v3/facade/doc/%s/body', $docId), null, $query, $token);
110
111
        /* @noinspection PhpIncompatibleReturnTypeInspection */
112 6
        return $this->serializer->deserialize(FacadeDocBodyResponse::class, $result);
113
    }
114
115 1
    public function facadeCisList(string $token, FacadeCisListRequest $request): FacadeCisListResponse
116
    {
117 1
        $body = $this->serializer->serialize($request);
0 ignored issues
show
Documentation introduced by
$request is of type object<Lamoda\IsmpClient...o\FacadeCisListRequest>, but the function expects a object<Lamoda\IsmpClient\Serializer\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
118 1
        $response = $this->request('POST', '/api/v3/facade/cis/cis_list', $body, null, $token);
119
120
        /* @noinspection PhpIncompatibleReturnTypeInspection */
121 1
        return $this->serializer->deserialize(FacadeCisListResponse::class, $response);
122
    }
123
124
    public function facadeMarkedProducts(string $token, string $cis): FacadeMarkedProductsResponse
125
    {
126
        $response = $this->request('GET', '/api/v3/facade/marked_products/info', null, ['cis' => $cis], $token);
127
128
        /** @noinspection PhpIncompatibleReturnTypeInspection */
129
        return $this->serializer->deserialize(FacadeMarkedProductsResponse::class, $response);
130
    }
131
132 1
    public function lkDocumentsCreate(string $token, DocumentCreateRequest $request): string
133
    {
134 1
        assert($request->getType() !== null, 'Document type is required for lkDocumentsCreate');
135 1
        assert($request->getProductGroup() !== null, 'Product group is required for lkDocumentsCreate');
136
137 1
        $body = $this->serializer->serialize($request);
0 ignored issues
show
Documentation introduced by
$request is of type object<Lamoda\IsmpClient...\DocumentCreateRequest>, but the function expects a object<Lamoda\IsmpClient\Serializer\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
138
139 1
        return $this->request(
140 1
            'POST',
141 1
            '/api/v3/lk/documents/create',
142 1
            $body,
143 1
            ['pg' => $request->getProductGroup()],
144 1
            $token
145
        );
146
    }
147
148 1
    public function lkImportSend(string $token, DocumentCreateRequest $request): string
149
    {
150 1
        $body = $this->serializer->serialize($request);
0 ignored issues
show
Documentation introduced by
$request is of type object<Lamoda\IsmpClient...\DocumentCreateRequest>, but the function expects a object<Lamoda\IsmpClient\Serializer\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
151
152 1
        return $this->request('POST', '/api/v3/lk/import/send', $body, null, $token);
153
    }
154
155 1
    public function lkReceiptSend(string $token, DocumentCreateRequest $request): string
156
    {
157 1
        $body = $this->serializer->serialize($request);
0 ignored issues
show
Documentation introduced by
$request is of type object<Lamoda\IsmpClient...\DocumentCreateRequest>, but the function expects a object<Lamoda\IsmpClient\Serializer\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
158
159 1
        return $this->request('POST', '/api/v3/lk/receipt/send', $body, null, $token);
160
    }
161
162 1
    public function lkDocumentsShipmentCreate(string $token, DocumentCreateRequest $request): string
163
    {
164 1
        $body = $this->serializer->serialize($request);
0 ignored issues
show
Documentation introduced by
$request is of type object<Lamoda\IsmpClient...\DocumentCreateRequest>, but the function expects a object<Lamoda\IsmpClient\Serializer\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
165
166 1
        return $this->request('POST', '/api/v3/lk/documents/shipment/create', $body, null, $token);
167
    }
168
169 1
    public function lkDocumentsAcceptanceCreate(string $token, DocumentCreateRequest $request): string
170
    {
171 1
        $body = $this->serializer->serialize($request);
0 ignored issues
show
Documentation introduced by
$request is of type object<Lamoda\IsmpClient...\DocumentCreateRequest>, but the function expects a object<Lamoda\IsmpClient\Serializer\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
172
173 1
        return $this->request('POST', '/api/v3/lk/documents/acceptance/create', $body, null, $token);
174
    }
175
176 1
    public function productInfo(string $token, array $gtins): ProductInfoResponse
177
    {
178 1
        $gtinsList = implode(',', $gtins);
179
180 1
        $result = $this->request('GET', 'api/v3/product/info', null, [
181 1
            'gtins' => $gtinsList,
182 1
        ], $token);
183
184
        /* @noinspection PhpIncompatibleReturnTypeInspection */
185 1
        return $this->serializer->deserialize(ProductInfoResponse::class, $result);
186
    }
187
188 20
    private function request(string $method, string $uri, $body = null, $query = null, string $token = null): string
189
    {
190
        $options = [
191 20
            RequestOptions::BODY => $body,
192
            RequestOptions::HEADERS => [
193
                'Content-Type' => 'application/json',
194
            ],
195
            RequestOptions::HTTP_ERRORS => true,
196 20
            RequestOptions::QUERY => $query,
197
        ];
198
199 20
        if ($token) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $token of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
200 16
            $options[RequestOptions::HEADERS]['Authorization'] = 'Bearer ' . $token;
201
        }
202
203 20
        $uri = ltrim($uri, '/');
204
205
        try {
206 20
            $result = $this->client->request($method, $uri, $options);
207 2
        } catch (\Throwable $exception) {
208
            /* @noinspection PhpUnhandledExceptionInspection */
209 2
            throw $this->handleRequestException($exception);
210
        }
211
212 18
        return (string)$result->getBody();
213
    }
214
215 2
    private function handleRequestException(\Throwable $exception): \Throwable
216
    {
217 2
        if ($exception instanceof BadResponseException) {
218 1
            $response = $exception->getResponse();
219 1
            $responseBody = $response ? (string)$response->getBody() : '';
220 1
            $responseCode = $response ? $response->getStatusCode() : 0;
221
222 1
            return IsmpRequestErrorException::becauseOfErrorResponse($responseCode, $responseBody, $exception);
223
        }
224
225 1
        return IsmpGeneralErrorException::becauseOfError($exception);
226
    }
227
}
228