Passed
Push — master ( b6cc98...f3d222 )
by Agaletskiy
01:51
created

OmsApi::request()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23

Duplication

Lines 23
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 2.0185

Importance

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