Completed
Push — master ( d80f87...33b881 )
by Agaletskiy
01:56
created

IsmpApi::lkDocumentsCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

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