Passed
Push — master ( 9d78ee...ff666c )
by Agaletskiy
40s queued 10s
created

OmsApi::appendSignatureHeader()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

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