Passed
Push — master ( 321509...140bc5 )
by Agaletskiy
02:43
created

IsmpApi::facadeDocListV2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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