Passed
Push — master ( d9432c...9cc4e5 )
by
unknown
47s queued 12s
created

IsmpApi::lkDocumentsShipmentCreate()   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 16
    public function __construct(ClientInterface $client, SerializerInterface $serializer)
40
    {
41 16
        $this->client = $client;
42 16
        $this->serializer = $serializer;
43 16
    }
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 View Code Duplication
    public function authCert(AuthCertRequest $request): AuthCertResponse
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function facadeOrder(string $token, FacadeOrderRequest $request): FacadeOrderResponse
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function facadeOrderDetails(string $token, string $orderId): FacadeOrderDetailsResponse
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 2
    public function facadeDocBody(string $token, string $docId, int $limit = null): FacadeDocBodyResponse
88
    {
89 2
        $query = null;
90 2
        if ($limit !== null) {
91 1
            $query = ['limit' => $limit];
92
        }
93
94 2
        $result = $this->request('GET', sprintf('/api/v3/facade/doc/%s/body', $docId), null, $query, $token);
95
96
        /* @noinspection PhpIncompatibleReturnTypeInspection */
97 2
        return $this->serializer->deserialize(FacadeDocBodyResponse::class, $result);
98
    }
99
100 1 View Code Duplication
    public function facadeCisList(string $token, FacadeCisListRequest $request): FacadeCisListResponse
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102 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...
103 1
        $response = $this->request('POST', '/api/v3/facade/cis/cis_list', $body, null, $token);
104
105
        /* @noinspection PhpIncompatibleReturnTypeInspection */
106 1
        return $this->serializer->deserialize(FacadeCisListResponse::class, $response);
107
    }
108
109 View Code Duplication
    public function facadeMarkedProducts(string $token, string $cis): FacadeMarkedProductsResponse
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        $response = $this->request('GET', '/api/v3/facade/marked_products/info', null, ['cis' => $cis], $token);
112
113
        /** @noinspection PhpIncompatibleReturnTypeInspection */
114
        return $this->serializer->deserialize(FacadeMarkedProductsResponse::class, $response);
115
    }
116
117 1
    public function lkDocumentsCreate(string $token, DocumentCreateRequest $request): string
118
    {
119 1
        assert($request->getType() !== null, 'Document type is required for lkDocumentsCreate');
120 1
        assert($request->getProductGroup() !== null, 'Product group is required for lkDocumentsCreate');
121
122 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...
123
124 1
        return $this->request(
125 1
            'POST',
126 1
            '/api/v3/lk/documents/create',
127 1
            $body,
128 1
            ['pg' => $request->getProductGroup()],
129 1
            $token
130
        );
131
    }
132
133 1
    public function lkImportSend(string $token, DocumentCreateRequest $request): string
134
    {
135 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...
136
137 1
        return $this->request('POST', '/api/v3/lk/import/send', $body, null, $token);
138
    }
139
140 1
    public function lkReceiptSend(string $token, DocumentCreateRequest $request): string
141
    {
142 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...
143
144 1
        return $this->request('POST', '/api/v3/lk/receipt/send', $body, null, $token);
145
    }
146
147 1
    public function lkDocumentsShipmentCreate(string $token, DocumentCreateRequest $request): string
148
    {
149 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...
150
151 1
        return $this->request('POST', '/api/v3/lk/documents/shipment/create', $body, null, $token);
152
    }
153
154 1
    public function lkDocumentsAcceptanceCreate(string $token, DocumentCreateRequest $request): string
155
    {
156 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...
157
158 1
        return $this->request('POST', '/api/v3/lk/documents/acceptance/create', $body, null, $token);
159
    }
160
161 1
    public function productInfo(string $token, array $gtins): ProductInfoResponse
162
    {
163 1
        $gtinsList = implode(',', $gtins);
164
165 1
        $result = $this->request('GET', 'api/v3/product/info', null, [
166 1
            'gtins' => $gtinsList,
167 1
        ], $token);
168
169
        /* @noinspection PhpIncompatibleReturnTypeInspection */
170 1
        return $this->serializer->deserialize(ProductInfoResponse::class, $result);
171
    }
172
173 16
    private function request(string $method, string $uri, $body = null, $query = null, string $token = null): string
174
    {
175
        $options = [
176 16
            RequestOptions::BODY => $body,
177
            RequestOptions::HEADERS => [
178
                'Content-Type' => 'application/json',
179
            ],
180
            RequestOptions::HTTP_ERRORS => true,
181 16
            RequestOptions::QUERY => $query,
182
        ];
183
184 16
        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...
185 12
            $options[RequestOptions::HEADERS]['Authorization'] = 'Bearer ' . $token;
186
        }
187
188 16
        $uri = ltrim($uri, '/');
189
190
        try {
191 16
            $result = $this->client->request($method, $uri, $options);
192 2
        } catch (\Throwable $exception) {
193
            /* @noinspection PhpUnhandledExceptionInspection */
194 2
            throw $this->handleRequestException($exception);
195
        }
196
197 14
        return (string)$result->getBody();
198
    }
199
200 2
    private function handleRequestException(\Throwable $exception): \Throwable
201
    {
202 2
        if ($exception instanceof BadResponseException) {
203 1
            $response = $exception->getResponse();
204 1
            $responseBody = $response ? (string)$response->getBody() : '';
205 1
            $responseCode = $response ? $response->getStatusCode() : 0;
206
207 1
            return IsmpRequestErrorException::becauseOfErrorResponse($responseCode, $responseBody, $exception);
208
        }
209
210 1
        return IsmpGeneralErrorException::becauseOfError($exception);
211
    }
212
}
213