Completed
Pull Request — master (#2)
by Agaletskiy
03:05
created

OmsApi::createOrderForEmissionIC()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
ccs 7
cts 7
cp 1
cc 1
nc 1
nop 4
crap 1
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\CreateOrderForEmissionICRequest;
16
use Lamoda\OmsClient\V2\Dto\CreateOrderForEmissionICResponse;
17
use Lamoda\OmsClient\V2\Dto\GetICBufferStatusResponse;
18
use Lamoda\OmsClient\V2\Dto\GetICsFromOrderResponse;
19
use Throwable;
20
21
final class OmsApi
22
{
23
    /**
24
     * @var ClientInterface
25
     */
26
    private $client;
27
    /**
28
     * @var SerializerInterface
29
     */
30
    private $serializer;
31
32 8
    public function __construct(ClientInterface $client, SerializerInterface $serializer)
33
    {
34 8
        $this->client = $client;
35 8
        $this->serializer = $serializer;
36 8
    }
37
38 1
    public function createOrderForEmissionIC(
39
        Extension $extension,
40
        string $token,
41
        string $omsId,
42
        CreateOrderForEmissionICRequest $request
43
    ): CreateOrderForEmissionICResponse {
44 1
        $url = sprintf('/api/v2/%s/orders', (string)$extension);
45 1
        $body = $this->serializer->serialize($request);
0 ignored issues
show
Documentation introduced by
$request is of type object<Lamoda\OmsClient\...erForEmissionICRequest>, but the function expects a object<Lamoda\OmsClient\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...
46
47 1
        $result = $this->request($token, 'POST', $url, [
48 1
            'omsId' => $omsId,
49 1
        ], $body);
50
51
        /* @noinspection PhpIncompatibleReturnTypeInspection */
52 1
        return $this->serializer->deserialize(CreateOrderForEmissionICResponse::class, $result);
53
    }
54
55
    /**
56
     * @throws OmsRequestErrorException
57
     */
58 8 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...
59
    {
60
        $options = [
61 8
            RequestOptions::BODY => $body,
62 8
            RequestOptions::HEADERS => [
63 8
                'Content-Type' => 'application/json',
64 8
                'clientToken' => $token,
65
            ],
66 8
            RequestOptions::QUERY => $query,
67 8
            RequestOptions::HTTP_ERRORS => true,
68
        ];
69
70 8
        $uri = ltrim($uri, '/');
71
72
        try {
73 8
            $result = $this->client->request($method, $uri, $options);
74 2
        } catch (Throwable $exception) {
75
            /* @noinspection PhpUnhandledExceptionInspection */
76 2
            throw $this->handleRequestException($exception);
77
        }
78
79 6
        return (string)$result->getBody();
80
    }
81
82 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...
83
    {
84 2
        if ($exception instanceof BadResponseException) {
85 1
            $response = $exception->getResponse();
86 1
            $responseBody = $response ? (string)$response->getBody() : '';
87 1
            $responseCode = $response ? $response->getStatusCode() : 0;
88
89 1
            return OmsRequestErrorException::becauseOfError($responseCode, $responseBody, $exception);
90
        }
91
92 1
        return OmsGeneralErrorException::becauseOfError($exception);
93
    }
94
95 5
    public function getICBufferStatus(
96
        Extension $extension,
97
        string $token,
98
        string $omsId,
99
        string $orderId,
100
        string $gtin
101
    ): GetICBufferStatusResponse {
102 5
        $url = sprintf('/api/v2/%s/buffer/status', (string)$extension);
103 5
        $result = $this->request($token, 'GET', $url, [
104 5
            'omsId' => $omsId,
105 5
            'orderId' => $orderId,
106 5
            'gtin' => $gtin,
107
        ]);
108
109
        /* @noinspection PhpIncompatibleReturnTypeInspection */
110 3
        return $this->serializer->deserialize(GetICBufferStatusResponse::class, $result);
111
    }
112
113 1 View Code Duplication
    public function getICsFromOrder(
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...
114
        Extension $extension,
115
        string $token,
116
        string $omsId,
117
        string $orderId,
118
        string $gtin,
119
        int $quantity,
120
        string $lastBlockId = '0'
121
    ): GetICsFromOrderResponse {
122 1
        $url = sprintf('/api/v2/%s/codes', (string)$extension);
123 1
        $result = $this->request($token, 'GET', $url, [
124 1
            'omsId' => $omsId,
125 1
            'orderId' => $orderId,
126 1
            'gtin' => $gtin,
127 1
            'quantity' => $quantity,
128 1
            'lastBlockId' => $lastBlockId,
129
        ]);
130
131
        /* @noinspection PhpIncompatibleReturnTypeInspection */
132 1
        return $this->serializer->deserialize(GetICsFromOrderResponse::class, $result);
133
    }
134
135 1 View Code Duplication
    public function closeICArray(
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...
136
        Extension $extension,
137
        string $token,
138
        string $omsId,
139
        string $orderId,
140
        string $gtin,
141
        string $lastBlockId
142
    ): CloseICArrayResponse {
143 1
        $url = sprintf('/api/v2/%s/buffer/close', (string)$extension);
144 1
        $result = $this->request($token, 'POST', $url, [
145 1
            'omsId' => $omsId,
146 1
            'orderId' => $orderId,
147 1
            'gtin' => $gtin,
148 1
            'lastBlockId' => $lastBlockId,
149
        ]);
150
151
        /* @noinspection PhpIncompatibleReturnTypeInspection */
152 1
        return $this->serializer->deserialize(CloseICArrayResponse::class, $result);
153
    }
154
}
155