Completed
Push — master ( 2bb4c5...9b3355 )
by Agaletskiy
01:40
created

IsmpApi   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 148
Duplicated Lines 25 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 7
dl 37
loc 148
ccs 61
cts 61
cp 1
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A authCertKey() 0 7 1
A authCert() 8 8 1
A facadeOrder() 8 8 1
A facadeOrderDetails() 7 7 1
A facadeDocListV2() 0 7 1
A facadeDocBody() 7 7 1
A facadeCisList() 7 7 1
A lkImportSend() 0 6 1
A lkDocumentsShipmentCreate() 0 6 1
A lkDocumentsAcceptanceCreate() 0 6 1
A productInfo() 0 11 1
A request() 0 26 3
A handleRequestException() 0 12 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 13
    public function __construct(ClientInterface $client, SerializerInterface $serializer)
38
    {
39 13
        $this->client = $client;
40 13
        $this->serializer = $serializer;
41 13
    }
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 lkImportSend(string $token, DocumentCreateRequest $request): string
102
    {
103 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...
104
105 1
        return $this->request('POST', '/api/v3/lk/import/send', $body, null, $token);
106
    }
107
108 1
    public function lkDocumentsShipmentCreate(string $token, DocumentCreateRequest $request): string
109
    {
110 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...
111
112 1
        return $this->request('POST', '/api/v3/lk/documents/shipment/create', $body, null, $token);
113
    }
114
115 1
    public function lkDocumentsAcceptanceCreate(string $token, DocumentCreateRequest $request): string
116
    {
117 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...
118
119 1
        return $this->request('POST', '/api/v3/lk/documents/acceptance/create', $body, null, $token);
120
    }
121
122 1
    public function productInfo(string $token, array $gtins): ProductInfoResponse
123
    {
124 1
        $gtinsList = implode(',', $gtins);
125
126 1
        $result = $this->request('GET', 'api/v3/product/info', null, [
127 1
            'gtins' => $gtinsList,
128 1
        ], $token);
129
130
        /* @noinspection PhpIncompatibleReturnTypeInspection */
131 1
        return $this->serializer->deserialize(ProductInfoResponse::class, $result);
132
    }
133
134 13
    private function request(string $method, string $uri, $body = null, $query = null, string $token = null): string
135
    {
136
        $options = [
137 13
            RequestOptions::BODY => $body,
138 13
            RequestOptions::HEADERS => [
139
                'Content-Type' => 'application/json',
140
            ],
141 13
            RequestOptions::HTTP_ERRORS => true,
142 13
            RequestOptions::QUERY => $query,
143
        ];
144
145 13
        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...
146 9
            $options[RequestOptions::HEADERS]['Authorization'] = 'Bearer ' . $token;
147
        }
148
149 13
        $uri = ltrim($uri, '/');
150
151
        try {
152 13
            $result = $this->client->request($method, $uri, $options);
153 2
        } catch (\Throwable $exception) {
154
            /* @noinspection PhpUnhandledExceptionInspection */
155 2
            throw $this->handleRequestException($exception);
156
        }
157
158 11
        return (string) $result->getBody();
159
    }
160
161 2
    private function handleRequestException(\Throwable $exception): \Throwable
162
    {
163 2
        if ($exception instanceof BadResponseException) {
164 1
            $response = $exception->getResponse();
165 1
            $responseBody = $response ? (string) $response->getBody() : '';
166 1
            $responseCode = $response ? $response->getStatusCode() : 0;
167
168 1
            return IsmpRequestErrorException::becauseOfErrorResponse($responseCode, $responseBody, $exception);
169
        }
170
171 1
        return IsmpGeneralErrorException::becauseOfError($exception);
172
    }
173
}
174