OmsApi::createOrderForEmissionIC()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
ccs 10
cts 10
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\Exception\OmsSignerErrorException;
14
use Lamoda\OmsClient\Serializer\SerializerInterface;
15
use Lamoda\OmsClient\V2\Dto\CloseICArrayResponse;
16
use Lamoda\OmsClient\V2\Dto\CreateOrderForEmissionICRequest;
17
use Lamoda\OmsClient\V2\Dto\CreateOrderForEmissionICResponse;
18
use Lamoda\OmsClient\V2\Dto\GetICBufferStatusResponse;
19
use Lamoda\OmsClient\V2\Dto\GetICsFromOrderResponse;
20
use Lamoda\OmsClient\V2\Signer\SignerInterface;
21
22
final class OmsApi
23
{
24
    /**
25
     * @var ClientInterface
26
     */
27
    private $client;
28
    /**
29
     * @var SerializerInterface
30
     */
31
    private $serializer;
32
    /**
33
     * @var RequestToExtension
34
     */
35
    private $requestToExtension;
36
37 10
    public function __construct(ClientInterface $client, SerializerInterface $serializer)
38
    {
39 10
        $this->requestToExtension = new RequestToExtension();
40 10
        $this->client = $client;
41 10
        $this->serializer = $serializer;
42 10
    }
43
44 3
    public function createOrderForEmissionIC(
45
        string $token,
46
        string $omsId,
47
        CreateOrderForEmissionICRequest $request,
48
        SignerInterface $signer = null
49
    ): CreateOrderForEmissionICResponse {
50 3
        $extension = $this->requestToExtension->getExtensionByCreateOrderForEmissionICRequest($request);
51
52 3
        $url = sprintf('/api/v2/%s/orders', (string)$extension);
53 3
        $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...
54
55 3
        $headers = [];
56 3
        $headers = $this->appendSignatureHeader($headers, $body, $signer);
57
58 2
        $result = $this->request($token, 'POST', $url, [
59 2
            'omsId' => $omsId,
60 2
        ], $body, $headers);
61
62
        /* @noinspection PhpIncompatibleReturnTypeInspection */
63 2
        return $this->serializer->deserialize(CreateOrderForEmissionICResponse::class, $result);
64
    }
65
66 5
    public function getICBufferStatus(
67
        Extension $extension,
68
        string $token,
69
        string $omsId,
70
        string $orderId,
71
        string $gtin
72
    ): GetICBufferStatusResponse {
73 5
        $url = sprintf('/api/v2/%s/buffer/status', (string)$extension);
74 5
        $result = $this->request($token, 'GET', $url, [
75 5
            'omsId' => $omsId,
76 5
            'orderId' => $orderId,
77 5
            'gtin' => $gtin,
78
        ]);
79
80
        /* @noinspection PhpIncompatibleReturnTypeInspection */
81 3
        return $this->serializer->deserialize(GetICBufferStatusResponse::class, $result);
82
    }
83
84 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...
85
        Extension $extension,
86
        string $token,
87
        string $omsId,
88
        string $orderId,
89
        string $gtin,
90
        int $quantity,
91
        string $lastBlockId = '0'
92
    ): GetICsFromOrderResponse {
93 1
        $url = sprintf('/api/v2/%s/codes', (string)$extension);
94 1
        $result = $this->request($token, 'GET', $url, [
95 1
            'omsId' => $omsId,
96 1
            'orderId' => $orderId,
97 1
            'gtin' => $gtin,
98 1
            'quantity' => $quantity,
99 1
            'lastBlockId' => $lastBlockId,
100
        ]);
101
102
        /* @noinspection PhpIncompatibleReturnTypeInspection */
103 1
        return $this->serializer->deserialize(GetICsFromOrderResponse::class, $result);
104
    }
105
106 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...
107
        Extension $extension,
108
        string $token,
109
        string $omsId,
110
        string $orderId,
111
        string $gtin,
112
        string $lastBlockId
113
    ): CloseICArrayResponse {
114 1
        $url = sprintf('/api/v2/%s/buffer/close', (string)$extension);
115 1
        $result = $this->request($token, 'POST', $url, [
116 1
            'omsId' => $omsId,
117 1
            'orderId' => $orderId,
118 1
            'gtin' => $gtin,
119 1
            'lastBlockId' => $lastBlockId,
120
        ]);
121
122
        /* @noinspection PhpIncompatibleReturnTypeInspection */
123 1
        return $this->serializer->deserialize(CloseICArrayResponse::class, $result);
124
    }
125
126
    /**
127
     * @throws OmsRequestErrorException
128
     */
129 9
    private function request(
130
        string $token,
131
        string $method,
132
        string $uri,
133
        array $query = [],
134
        $body = null,
135
        $headers = []
136
    ): string {
137
        $options = [
138 9
            RequestOptions::BODY => $body,
139 9
            RequestOptions::HEADERS => array_merge($headers, [
140 9
                'Content-Type' => 'application/json',
141 9
                'clientToken' => $token,
142
            ]),
143 9
            RequestOptions::QUERY => $query,
144 9
            RequestOptions::HTTP_ERRORS => true,
145
        ];
146
147 9
        $uri = ltrim($uri, '/');
148
149
        try {
150 9
            $result = $this->client->request($method, $uri, $options);
151 2
        } catch (\Throwable $exception) {
152
            /* @noinspection PhpUnhandledExceptionInspection */
153 2
            throw $this->handleRequestException($exception);
154
        }
155
156 7
        return (string)$result->getBody();
157
    }
158
159 2
    private function handleRequestException(\Throwable $exception): OmsClientExceptionInterface
160
    {
161 2
        if ($exception instanceof BadResponseException) {
162 1
            $response = $exception->getResponse();
163 1
            $responseBody = $response ? (string)$response->getBody() : '';
164 1
            $responseCode = $response ? $response->getStatusCode() : 0;
165
166 1
            return OmsRequestErrorException::becauseOfError($responseCode, $responseBody, $exception);
167
        }
168
169 1
        return OmsGeneralErrorException::becauseOfError($exception);
170
    }
171
172 3
    private function appendSignatureHeader(array $headers, string $data, SignerInterface $signer = null): array
173
    {
174 3
        if ($signer === null) {
175 1
            return $headers;
176
        }
177
178
        try {
179 2
            $headers['X-Signature'] = $signer->sign($data);
180 1
        } catch (\Throwable $exception) {
181 1
            throw OmsSignerErrorException::becauseOfError($exception);
182
        }
183
184 1
        return $headers;
185
    }
186
}
187