IsmpApi::facadeDocBody()   A
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 12
cts 12
cp 1
rs 9.2728
c 0
b 0
f 0
cc 5
nc 16
nop 6
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lamoda\IsmpClient\V4;
6
7
use GuzzleHttp\ClientInterface;
8
use GuzzleHttp\Exception\BadResponseException;
9
use GuzzleHttp\RequestOptions;
10
use Lamoda\IsmpClient\Exception\IsmpGeneralErrorException;
11
use Lamoda\IsmpClient\Exception\IsmpRequestErrorException;
12
use Lamoda\IsmpClient\Serializer\SerializerInterface;
13
use Lamoda\IsmpClient\V3\Dto\AuthCertKeyResponse;
14
use Lamoda\IsmpClient\V3\Dto\AuthCertRequest;
15
use Lamoda\IsmpClient\V3\Dto\AuthCertResponse;
16
use Lamoda\IsmpClient\V3\Dto\DocumentCreateRequest;
17
use Lamoda\IsmpClient\V3\Dto\FacadeDocListV2Query;
18
use Lamoda\IsmpClient\V3\Dto\FacadeDocListV2Response;
19
use Lamoda\IsmpClient\V3\Dto\FacadeMarkedProductsResponse;
20
use Lamoda\IsmpClient\V3\Dto\ProductInfoResponse;
21
use Lamoda\IsmpClient\V3\IsmpApi as IsmpApiV3;
22
use Lamoda\IsmpClient\V3\IsmpApiInterface as IsmpApiV3Interface;
23
use Lamoda\IsmpClient\V4\Dto\FacadeCisListRequest;
24
use Lamoda\IsmpClient\V4\Dto\FacadeCisListResponse;
25
use Lamoda\IsmpClient\V4\Dto\FacadeDocBodyResponse;
26
27
class IsmpApi implements IsmpApiInterface
28
{
29
    /**
30
     * @var ClientInterface
31
     */
32
    private $client;
33
    /**
34
     * @var SerializerInterface
35
     */
36
    private $serializer;
37
    /**
38
     * @var IsmpApiV3|IsmpApiV3Interface
39
     */
40
    private $ismpApiV3;
41
42 15
    public function __construct(ClientInterface $client, SerializerInterface $serializer, ?IsmpApiV3Interface $ismpApiV3 = null)
43
    {
44 15
        $this->client = $client;
45 15
        $this->serializer = $serializer;
46 15
        $this->ismpApiV3 = $ismpApiV3 ?? new IsmpApiV3($client, $serializer);
47 15
    }
48
49 6
    public function facadeDocBody(string $token, string $docId, ?int $limit = null, ?string $orderColumn = null, ?string $orderedColumnValue = null, ?string $pageDir = null): FacadeDocBodyResponse
50
    {
51 6
        $query = null;
52 6
        if ($limit !== null) {
53 2
            $query['limit'] = $limit;
54
        }
55 6
        if ($orderColumn !== null) {
56 2
            $query['orderColumn'] = $orderColumn;
57
        }
58 6
        if ($orderedColumnValue !== null) {
59 2
            $query['orderedColumnValue'] = $orderedColumnValue;
60
        }
61 6
        if ($pageDir !== null) {
62 2
            $query['pageDir'] = $pageDir;
63
        }
64
65 6
        $result = $this->request('GET', sprintf('/api/v4/facade/doc/%s/body', $docId), null, $query, $token);
66
67
        /* @noinspection PhpIncompatibleReturnTypeInspection */
68 6
        return $this->serializer->deserialize(FacadeDocBodyResponse::class, $result);
69
    }
70
71 1
    public function facadeCisList(string $token, FacadeCisListRequest $request): FacadeCisListResponse
72
    {
73 1
        $body = $this->serializer->serialize($request);
0 ignored issues
show
Documentation introduced by
$request is of type object<Lamoda\IsmpClient...o\FacadeCisListRequest>, but the function expects a object<Lamoda\IsmpClient\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...
74 1
        $response = $this->request('POST', '/api/v4/facade/cis/cis_list', $body, null, $token);
75
76
        /* @noinspection PhpIncompatibleReturnTypeInspection */
77 1
        return $this->serializer->deserialize(FacadeCisListResponse::class, $response);
78
    }
79
80 3
    public function authCertKey(): AuthCertKeyResponse
81
    {
82 3
        return $this->ismpApiV3->authCertKey();
83
    }
84
85 2
    public function authCert(AuthCertRequest $request, ?string $connection = null): AuthCertResponse
86
    {
87 2
        return $this->ismpApiV3->authCert($request, $connection);
88
    }
89
90 1
    public function facadeDocListV2(string $token, FacadeDocListV2Query $query): FacadeDocListV2Response
91
    {
92 1
        return $this->ismpApiV3->facadeDocListV2($token, $query);
93
    }
94
95 1
    public function lkDocumentsCreate(string $token, DocumentCreateRequest $request): string
96
    {
97 1
        return $this->ismpApiV3->lkDocumentsCreate($token, $request);
98
    }
99
100 1
    public function productInfo(string $token, array $gtins): ProductInfoResponse
101
    {
102 1
        return $this->ismpApiV3->productInfo($token, $gtins);
103
    }
104
105
    public function facadeMarkedProducts(string $token, string $cis): FacadeMarkedProductsResponse
106
    {
107
        return $this->ismpApiV3->facadeMarkedProducts($token, $cis);
108
    }
109
110 7
    private function request(string $method, string $uri, $body = null, $query = null, string $token = null): string
111
    {
112
        $options = [
113 7
            RequestOptions::BODY => $body,
114
            RequestOptions::HEADERS => [
115
                'Content-Type' => 'application/json',
116
            ],
117
            RequestOptions::HTTP_ERRORS => true,
118 7
            RequestOptions::QUERY => $query,
119
        ];
120
121 7
        if ($token) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $token of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
122 7
            $options[RequestOptions::HEADERS]['Authorization'] = 'Bearer ' . $token;
123
        }
124
125 7
        $uri = ltrim($uri, '/');
126
127
        try {
128 7
            $result = $this->client->request($method, $uri, $options);
129
        } catch (\Throwable $exception) {
130
            /* @noinspection PhpUnhandledExceptionInspection */
131
            throw $this->handleRequestException($exception);
132
        }
133
134 7
        return (string)$result->getBody();
135
    }
136
137
    private function handleRequestException(\Throwable $exception): \Throwable
138
    {
139
        if ($exception instanceof BadResponseException) {
140
            $response = $exception->getResponse();
141
            $responseBody = $response ? (string)$response->getBody() : '';
142
            $responseCode = $response ? $response->getStatusCode() : 0;
143
144
            return IsmpRequestErrorException::becauseOfErrorResponse($responseCode, $responseBody, $exception);
145
        }
146
147
        return IsmpGeneralErrorException::becauseOfError($exception);
148
    }
149
}
150