Completed
Push — master ( 33b881...505908 )
by Agaletskiy
02:23
created

src/V3/IsmpApi.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lamoda\IsmpClient\V3;
6
7
use GuzzleHttp\ClientInterface;
8
use GuzzleHttp\Exception\BadResponseException;
9
use GuzzleHttp\RequestOptions;
10
use Lamoda\IsmpClient\Exception\IsmpGeneralErrorException;
11
use Lamoda\IsmpClient\V3\Dto\AuthCertKeyResponse;
12
use Lamoda\IsmpClient\V3\Dto\AuthCertRequest;
13
use Lamoda\IsmpClient\V3\Dto\AuthCertResponse;
14
use Lamoda\IsmpClient\V3\Dto\FacadeDocBodyResponse;
15
use Lamoda\IsmpClient\V3\Dto\FacadeCisListResponse;
16
use Lamoda\IsmpClient\V3\Dto\FacadeDocListV2Query;
17
use Lamoda\IsmpClient\V3\Dto\FacadeDocListV2Response;
18
use Lamoda\IsmpClient\V3\Dto\FacadeMarkedProductsResponse;
19
use Lamoda\IsmpClient\V3\Dto\FacadeOrderDetailsResponse;
20
use Lamoda\IsmpClient\V3\Dto\FacadeOrderRequest;
21
use Lamoda\IsmpClient\V3\Dto\FacadeOrderResponse;
22
use Lamoda\IsmpClient\V3\Dto\DocumentCreateRequest;
23
use Lamoda\IsmpClient\Exception\IsmpRequestErrorException;
24
use Lamoda\IsmpClient\Serializer\SerializerInterface;
25
use Lamoda\IsmpClient\V3\Dto\ProductInfoResponse;
26
27
final class IsmpApi implements IsmpApiInterface
28
{
29
    /**
30
     * @var ClientInterface
31
     */
32
    private $client;
33
    /**
34
     * @var SerializerInterface
35
     */
36
    private $serializer;
37
38 15
    public function __construct(ClientInterface $client, SerializerInterface $serializer)
39
    {
40 15
        $this->client = $client;
41 15
        $this->serializer = $serializer;
42 15
    }
43
44 3
    public function authCertKey(): AuthCertKeyResponse
45
    {
46 3
        $result = $this->request('GET', '/api/v3/auth/cert/key');
47
48
        /* @noinspection PhpIncompatibleReturnTypeInspection */
49 1
        return $this->serializer->deserialize(AuthCertKeyResponse::class, $result);
50
    }
51
52 1 View Code Duplication
    public function authCert(AuthCertRequest $request): AuthCertResponse
53
    {
54 1
        $body = $this->serializer->serialize($request);
55 1
        $result = $this->request('POST', '/api/v3/auth/cert/', $body);
56
57
        /* @noinspection PhpIncompatibleReturnTypeInspection */
58 1
        return $this->serializer->deserialize(AuthCertResponse::class, $result);
59
    }
60
61 1 View Code Duplication
    public function facadeOrder(string $token, FacadeOrderRequest $request): FacadeOrderResponse
62
    {
63 1
        $body = $this->serializer->serialize($request);
64 1
        $result = $this->request('POST', '/api/v3/facade/order/', $body, null, $token);
65
66
        /* @noinspection PhpIncompatibleReturnTypeInspection */
67 1
        return $this->serializer->deserialize(FacadeOrderResponse::class, $result);
68
    }
69
70 1 View Code Duplication
    public function facadeOrderDetails(string $token, string $orderId): FacadeOrderDetailsResponse
71
    {
72 1
        $result = $this->request('GET', sprintf('/api/v3/facade/order/%s/details', $orderId), null, null, $token);
73
74
        /* @noinspection PhpIncompatibleReturnTypeInspection */
75 1
        return $this->serializer->deserialize(FacadeOrderDetailsResponse::class, $result);
76
    }
77
78 1
    public function facadeDocListV2(string $token, FacadeDocListV2Query $query): FacadeDocListV2Response
79
    {
80 1
        $result = $this->request('GET', '/api/v3/facade/doc/listV2', null, $query->toQueryArray(), $token);
81
82
        /* @noinspection PhpIncompatibleReturnTypeInspection */
83 1
        return $this->serializer->deserialize(FacadeDocListV2Response::class, $result);
84
    }
85
86 1 View Code Duplication
    public function facadeDocBody(string $token, string $docId): FacadeDocBodyResponse
87
    {
88 1
        $result = $this->request('GET', sprintf('/api/v3/facade/doc/%s/body', $docId), null, null, $token);
89
90
        /* @noinspection PhpIncompatibleReturnTypeInspection */
91 1
        return $this->serializer->deserialize(FacadeDocBodyResponse::class, $result);
92
    }
93
94 1 View Code Duplication
    public function facadeCisList(string $token, string $cis): FacadeCisListResponse
95
    {
96 1
        $response = $this->request('GET', '/api/v3/facade/cis/cis_list', null, ['cis' => $cis], $token);
97
98
        /* @noinspection PhpIncompatibleReturnTypeInspection */
99 1
        return $this->serializer->deserialize(FacadeCisListResponse::class, $response);
100
    }
101
102 View Code Duplication
    public function facadeMarkedProducts(string $token, string $uit): FacadeMarkedProductsResponse
0 ignored issues
show
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...
103
    {
104
        $response = $this->request('GET', '/api/v3/facade/marked_products', null, ['uit' => $uit], $token);
105
106
        /** @noinspection PhpIncompatibleReturnTypeInspection */
107
        return $this->serializer->deserialize(FacadeMarkedProductsResponse::class, $response);
108
    }
109
110 1
    public function lkDocumentsCreate(string $token, DocumentCreateRequest $request): string
111
    {
112 1
        assert($request->getType() !== null, 'Document type is required for lkDocumentsCreate');
113 1
        assert($request->getProductGroup() !== null, 'Product group is required for lkDocumentsCreate');
114
115 1
        $body = $this->serializer->serialize($request);
116
117 1
        return $this->request(
118 1
            'POST',
119 1
            '/api/v3/lk/documents/create',
120 1
            $body,
121 1
            ['pg' => $request->getProductGroup()],
122 1
            $token
123
        );
124
    }
125
126 1
    public function lkImportSend(string $token, DocumentCreateRequest $request): string
127
    {
128 1
        $body = $this->serializer->serialize($request);
129
130 1
        return $this->request('POST', '/api/v3/lk/import/send', $body, null, $token);
131
    }
132
133 1
    public function lkReceiptSend(string $token, DocumentCreateRequest $request): string
134
    {
135 1
        $body = $this->serializer->serialize($request);
136
137 1
        return $this->request('POST', '/api/v3/lk/receipt/send', $body, null, $token);
138
    }
139
140 1
    public function lkDocumentsShipmentCreate(string $token, DocumentCreateRequest $request): string
141
    {
142 1
        $body = $this->serializer->serialize($request);
143
144 1
        return $this->request('POST', '/api/v3/lk/documents/shipment/create', $body, null, $token);
145
    }
146
147 1
    public function lkDocumentsAcceptanceCreate(string $token, DocumentCreateRequest $request): string
148
    {
149 1
        $body = $this->serializer->serialize($request);
150
151 1
        return $this->request('POST', '/api/v3/lk/documents/acceptance/create', $body, null, $token);
152
    }
153
154 1
    public function productInfo(string $token, array $gtins): ProductInfoResponse
155
    {
156 1
        $gtinsList = implode(',', $gtins);
157
158 1
        $result = $this->request('GET', 'api/v3/product/info', null, [
159 1
            'gtins' => $gtinsList,
160 1
        ], $token);
161
162
        /* @noinspection PhpIncompatibleReturnTypeInspection */
163 1
        return $this->serializer->deserialize(ProductInfoResponse::class, $result);
164
    }
165
166 15
    private function request(string $method, string $uri, $body = null, $query = null, string $token = null): string
167
    {
168
        $options = [
169 15
            RequestOptions::BODY => $body,
170 15
            RequestOptions::HEADERS => [
171
                'Content-Type' => 'application/json',
172
            ],
173 15
            RequestOptions::HTTP_ERRORS => true,
174 15
            RequestOptions::QUERY => $query,
175
        ];
176
177 15
        if ($token) {
178 11
            $options[RequestOptions::HEADERS]['Authorization'] = 'Bearer ' . $token;
179
        }
180
181 15
        $uri = ltrim($uri, '/');
182
183
        try {
184 15
            $result = $this->client->request($method, $uri, $options);
185 2
        } catch (\Throwable $exception) {
186
            /* @noinspection PhpUnhandledExceptionInspection */
187 2
            throw $this->handleRequestException($exception);
188
        }
189
190 13
        return (string) $result->getBody();
191
    }
192
193 2
    private function handleRequestException(\Throwable $exception): \Throwable
194
    {
195 2
        if ($exception instanceof BadResponseException) {
196 1
            $response = $exception->getResponse();
197 1
            $responseBody = $response ? (string) $response->getBody() : '';
198 1
            $responseCode = $response ? $response->getStatusCode() : 0;
199
200 1
            return IsmpRequestErrorException::becauseOfErrorResponse($responseCode, $responseBody, $exception);
201
        }
202
203 1
        return IsmpGeneralErrorException::becauseOfError($exception);
204
    }
205
}
206