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