Completed
Push — master ( f3d222...548fd9 )
by Agaletskiy
01:48
created

OmsApi   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 58.1 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getICBufferStatus() 15 15 1
A getICsFromOrder() 0 19 1
A closeICArray() 11 11 1
A request() 23 23 2
A handleRequestException() 12 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\OmsClient\V2;
6
7
use GuzzleHttp\ClientInterface;
8
use GuzzleHttp\Exception\BadResponseException;
9
use GuzzleHttp\RequestOptions;
10
use Lamoda\OmsClient\Exception\OmsClientExceptionInterface;
11
use Lamoda\OmsClient\Exception\OmsGeneralErrorException;
12
use Lamoda\OmsClient\Exception\OmsRequestErrorException;
13
use Lamoda\OmsClient\Serializer\SerializerInterface;
14
use Lamoda\OmsClient\V2\Dto\CloseICArrayResponse;
15
use Lamoda\OmsClient\V2\Dto\GetICBufferStatusResponse;
16
use Lamoda\OmsClient\V2\Dto\GetICsFromOrderResponse;
17
18
final class OmsApi
19
{
20
    /**
21
     * @var ClientInterface
22
     */
23
    private $client;
24
    /**
25
     * @var SerializerInterface
26
     */
27
    private $serializer;
28
29 5
    public function __construct(ClientInterface $client, SerializerInterface $serializer)
30
    {
31 5
        $this->client = $client;
32 5
        $this->serializer = $serializer;
33 5
    }
34
35 3 View Code Duplication
    public function getICBufferStatus(
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...
36
        string $token,
37
        string $omsId,
38
        string $orderId,
39
        string $gtin
40
    ): GetICBufferStatusResponse {
41 3
        $result = $this->request($token, 'GET', '/api/v2/buffer/status', [
42 3
            'omsId' => $omsId,
43 3
            'orderId' => $orderId,
44 3
            'gtin' => $gtin,
45
        ]);
46
47
        /* @noinspection PhpIncompatibleReturnTypeInspection */
48 1
        return $this->serializer->deserialize(GetICBufferStatusResponse::class, $result);
49
    }
50
51 1
    public function getICsFromOrder(
52
        string $token,
53
        string $omsId,
54
        string $orderId,
55
        string $gtin,
56
        int $quantity,
57
        string $lastBlockId = '0'
58
    ): GetICsFromOrderResponse {
59 1
        $result = $this->request($token, 'GET', '/api/v2/codes', [
60 1
            'omsId' => $omsId,
61 1
            'orderId' => $orderId,
62 1
            'gtin' => $gtin,
63 1
            'quantity' => $quantity,
64 1
            'lastBlockId' => $lastBlockId,
65
        ]);
66
67
        /* @noinspection PhpIncompatibleReturnTypeInspection */
68 1
        return $this->serializer->deserialize(GetICsFromOrderResponse::class, $result);
69
    }
70
71 1 View Code Duplication
    public function closeICArray(string $token, string $omsId, string $orderId, string $gtin): CloseICArrayResponse
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($token, 'GET', '/api/v2/buffer/close', [
74 1
            'omsId' => $omsId,
75 1
            'orderId' => $orderId,
76 1
            'gtin' => $gtin,
77
        ]);
78
79
        /* @noinspection PhpIncompatibleReturnTypeInspection */
80 1
        return $this->serializer->deserialize(CloseICArrayResponse::class, $result);
81
    }
82
83
    /**
84
     * @throws OmsRequestErrorException
85
     */
86 5 View Code Duplication
    private function request(string $token, string $method, string $uri, array $query = [], $body = null): string
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...
87
    {
88
        $options = [
89 5
            RequestOptions::BODY => $body,
90 5
            RequestOptions::HEADERS => [
91 5
                'Content-Type' => 'application/json',
92 5
                'clientToken' => $token,
93
            ],
94 5
            RequestOptions::QUERY => $query,
95 5
            RequestOptions::HTTP_ERRORS => true,
96
        ];
97
98 5
        $uri = ltrim($uri, '/');
99
100
        try {
101 5
            $result = $this->client->request($method, $uri, $options);
102 2
        } catch (\Throwable $exception) {
103
            /* @noinspection PhpUnhandledExceptionInspection */
104 2
            throw $this->handleRequestException($exception);
105
        }
106
107 3
        return (string)$result->getBody();
108
    }
109
110 2 View Code Duplication
    private function handleRequestException(\Throwable $exception): OmsClientExceptionInterface
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...
111
    {
112 2
        if ($exception instanceof BadResponseException) {
113 1
            $response = $exception->getResponse();
114 1
            $responseBody = $response ? (string)$response->getBody() : '';
115 1
            $responseCode = $response ? $response->getStatusCode() : 0;
116
117 1
            return OmsRequestErrorException::becauseOfError($responseCode, $responseBody, $exception);
118
        }
119
120 1
        return OmsGeneralErrorException::becauseOfError($exception);
121
    }
122
}
123