Passed
Push — master ( db0ae1...f66061 )
by Agaletskiy
01:56
created

IsmpApi::facadeCisList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 7
loc 7
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\FacadeCisListResponse;
18
use Lamoda\IsmpClient\V3\Dto\FacadeDocBodyResponse;
19
use Lamoda\IsmpClient\V3\Dto\FacadeDocListV2Query;
20
use Lamoda\IsmpClient\V3\Dto\FacadeDocListV2Response;
21
use Lamoda\IsmpClient\V3\Dto\FacadeMarkedProductsResponse;
22
use Lamoda\IsmpClient\V3\Dto\FacadeOrderDetailsResponse;
23
use Lamoda\IsmpClient\V3\Dto\FacadeOrderRequest;
24
use Lamoda\IsmpClient\V3\Dto\FacadeOrderResponse;
25
use Lamoda\IsmpClient\V3\Dto\ProductInfoResponse;
26
27
final class IsmpApi implements IsmpApiInterface
28
{
29
    /**
30
     * @var ClientInterface
31
     */
32
    private $client;
33
    /**
34
     * @var SerializerInterface
35
     */
36
    private $serializer;
37
38 16
    public function __construct(ClientInterface $client, SerializerInterface $serializer)
39
    {
40 16
        $this->client = $client;
41 16
        $this->serializer = $serializer;
42 16
    }
43
44 3
    public function authCertKey(): AuthCertKeyResponse
45
    {
46 3
        $result = $this->request('GET', '/api/v3/auth/cert/key');
47
48
        /* @noinspection PhpIncompatibleReturnTypeInspection */
49 1
        return $this->serializer->deserialize(AuthCertKeyResponse::class, $result);
50
    }
51
52 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...
53
    {
54 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...
55 1
        $result = $this->request('POST', '/api/v3/auth/cert/', $body);
56
57
        /* @noinspection PhpIncompatibleReturnTypeInspection */
58 1
        return $this->serializer->deserialize(AuthCertResponse::class, $result);
59
    }
60
61 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...
62
    {
63 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...
64 1
        $result = $this->request('POST', '/api/v3/facade/order/', $body, null, $token);
65
66
        /* @noinspection PhpIncompatibleReturnTypeInspection */
67 1
        return $this->serializer->deserialize(FacadeOrderResponse::class, $result);
68
    }
69
70 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...
71
    {
72 1
        $result = $this->request('GET', sprintf('/api/v3/facade/order/%s/details', $orderId), null, null, $token);
73
74
        /* @noinspection PhpIncompatibleReturnTypeInspection */
75 1
        return $this->serializer->deserialize(FacadeOrderDetailsResponse::class, $result);
76
    }
77
78 1
    public function facadeDocListV2(string $token, FacadeDocListV2Query $query): FacadeDocListV2Response
79
    {
80 1
        $result = $this->request('GET', '/api/v3/facade/doc/listV2', null, $query->toQueryArray(), $token);
81
82
        /* @noinspection PhpIncompatibleReturnTypeInspection */
83 1
        return $this->serializer->deserialize(FacadeDocListV2Response::class, $result);
84
    }
85
86 2
    public function facadeDocBody(string $token, string $docId, int $limit = null): FacadeDocBodyResponse
87
    {
88 2
        $query = null;
89 2
        if ($limit !== null) {
90 1
            $query = ['limit' => $limit];
91
        }
92
93 2
        $result = $this->request('GET', sprintf('/api/v3/facade/doc/%s/body', $docId), null, $query, $token);
94
95
        /* @noinspection PhpIncompatibleReturnTypeInspection */
96 2
        return $this->serializer->deserialize(FacadeDocBodyResponse::class, $result);
97
    }
98
99 1 View Code Duplication
    public function facadeCisList(string $token, string $cis): 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...
100
    {
101 1
        $response = $this->request('GET', '/api/v3/facade/cis/cis_list', null, ['cis' => $cis], $token);
102
103
        /* @noinspection PhpIncompatibleReturnTypeInspection */
104 1
        return $this->serializer->deserialize(FacadeCisListResponse::class, $response);
105
    }
106
107 View Code Duplication
    public function facadeMarkedProducts(string $token, string $uit): 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...
108
    {
109
        $response = $this->request('GET', '/api/v3/facade/marked_products', null, ['uit' => $uit], $token);
110
111
        /** @noinspection PhpIncompatibleReturnTypeInspection */
112
        return $this->serializer->deserialize(FacadeMarkedProductsResponse::class, $response);
113
    }
114
115 1
    public function lkDocumentsCreate(string $token, DocumentCreateRequest $request): string
116
    {
117 1
        assert($request->getType() !== null, 'Document type is required for lkDocumentsCreate');
118 1
        assert($request->getProductGroup() !== null, 'Product group is required for lkDocumentsCreate');
119
120 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...
121
122 1
        return $this->request(
123 1
            'POST',
124 1
            '/api/v3/lk/documents/create',
125 1
            $body,
126 1
            ['pg' => $request->getProductGroup()],
127 1
            $token
128
        );
129
    }
130
131 1
    public function lkImportSend(string $token, DocumentCreateRequest $request): string
132
    {
133 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...
134
135 1
        return $this->request('POST', '/api/v3/lk/import/send', $body, null, $token);
136
    }
137
138 1
    public function lkReceiptSend(string $token, DocumentCreateRequest $request): string
139
    {
140 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...
141
142 1
        return $this->request('POST', '/api/v3/lk/receipt/send', $body, null, $token);
143
    }
144
145 1
    public function lkDocumentsShipmentCreate(string $token, DocumentCreateRequest $request): string
146
    {
147 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...
148
149 1
        return $this->request('POST', '/api/v3/lk/documents/shipment/create', $body, null, $token);
150
    }
151
152 1
    public function lkDocumentsAcceptanceCreate(string $token, DocumentCreateRequest $request): string
153
    {
154 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...
155
156 1
        return $this->request('POST', '/api/v3/lk/documents/acceptance/create', $body, null, $token);
157
    }
158
159 1
    public function productInfo(string $token, array $gtins): ProductInfoResponse
160
    {
161 1
        $gtinsList = implode(',', $gtins);
162
163 1
        $result = $this->request('GET', 'api/v3/product/info', null, [
164 1
            'gtins' => $gtinsList,
165 1
        ], $token);
166
167
        /* @noinspection PhpIncompatibleReturnTypeInspection */
168 1
        return $this->serializer->deserialize(ProductInfoResponse::class, $result);
169
    }
170
171 16
    private function request(string $method, string $uri, $body = null, $query = null, string $token = null): string
172
    {
173
        $options = [
174 16
            RequestOptions::BODY => $body,
175
            RequestOptions::HEADERS => [
176
                'Content-Type' => 'application/json',
177
            ],
178
            RequestOptions::HTTP_ERRORS => true,
179 16
            RequestOptions::QUERY => $query,
180
        ];
181
182 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...
183 12
            $options[RequestOptions::HEADERS]['Authorization'] = 'Bearer ' . $token;
184
        }
185
186 16
        $uri = ltrim($uri, '/');
187
188
        try {
189 16
            $result = $this->client->request($method, $uri, $options);
190 2
        } catch (\Throwable $exception) {
191
            /* @noinspection PhpUnhandledExceptionInspection */
192 2
            throw $this->handleRequestException($exception);
193
        }
194
195 14
        return (string)$result->getBody();
196
    }
197
198 2
    private function handleRequestException(\Throwable $exception): \Throwable
199
    {
200 2
        if ($exception instanceof BadResponseException) {
201 1
            $response = $exception->getResponse();
202 1
            $responseBody = $response ? (string)$response->getBody() : '';
203 1
            $responseCode = $response ? $response->getStatusCode() : 0;
204
205 1
            return IsmpRequestErrorException::becauseOfErrorResponse($responseCode, $responseBody, $exception);
206
        }
207
208 1
        return IsmpGeneralErrorException::becauseOfError($exception);
209
    }
210
}
211