1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace VasilDakov\Speedy; |
||
6 | |||
7 | use Fig\Http\Message\RequestMethodInterface; |
||
8 | use JMS\Serializer\SerializerInterface; |
||
9 | use Psr\Http\Client\ClientExceptionInterface; |
||
10 | use Psr\Http\Client\ClientInterface; |
||
11 | use Psr\Http\Message\RequestFactoryInterface; |
||
12 | use Psr\Http\Message\RequestInterface; |
||
13 | use VasilDakov\Speedy\Exception\JsonException; |
||
14 | use VasilDakov\Speedy\Serializer\SerializerFactory; |
||
15 | use VasilDakov\Speedy\Service\Calculation\CalculationRequest; |
||
16 | use VasilDakov\Speedy\Service\Client\GetContractClientsRequest; |
||
17 | use VasilDakov\Speedy\Service\Location; |
||
18 | use VasilDakov\Speedy\Service\Location\Complex\FindComplexRequest; |
||
19 | use VasilDakov\Speedy\Service\Location\Office\FindOfficeRequest; |
||
20 | use VasilDakov\Speedy\Service\Location\Site\FindSiteRequest; |
||
21 | use VasilDakov\Speedy\Service\Location\State\FindStateRequest; |
||
22 | use VasilDakov\Speedy\Service\Location\Street\FindStreetRequest; |
||
23 | use VasilDakov\Speedy\Service\Printing\PrintRequest; |
||
24 | use VasilDakov\Speedy\Service\Service\DestinationServicesRequest; |
||
25 | use VasilDakov\Speedy\Service\Shipment\CancelShipmentRequest; |
||
26 | use VasilDakov\Speedy\Service\Shipment\CreateShipmentRequest; |
||
27 | use VasilDakov\Speedy\Service\Track\TrackRequest; |
||
28 | |||
29 | /** |
||
30 | * Class Speedy. |
||
31 | * |
||
32 | * @author Vasil Dakov <[email protected]> |
||
33 | * @copyright 2009-2022 Neutrino.bg |
||
34 | * |
||
35 | * @version 1.0 |
||
36 | */ |
||
37 | final class Speedy implements SpeedyInterface |
||
38 | { |
||
39 | public const API_URL = 'https://api.speedy.bg/v1'; |
||
40 | |||
41 | |||
42 | private Configuration $configuration; |
||
43 | |||
44 | /** |
||
45 | * PSR-18: HTTP Client. |
||
46 | */ |
||
47 | private ClientInterface $client; |
||
48 | |||
49 | /** |
||
50 | * PSR-17: HTTP Factories. |
||
51 | */ |
||
52 | private RequestFactoryInterface $factory; |
||
53 | |||
54 | private SerializerInterface $serializer; |
||
55 | |||
56 | public function __construct( |
||
57 | Configuration $configuration, |
||
58 | ClientInterface $client, |
||
59 | RequestFactoryInterface $factory, |
||
60 | ?SerializerInterface $serializer = null |
||
61 | 13 | ) { |
|
62 | $this->configuration = $configuration; |
||
63 | $this->client = $client; |
||
64 | $this->factory = $factory; |
||
65 | |||
66 | 13 | if (null === $serializer) { |
|
67 | 13 | $serializer = (new SerializerFactory())(); |
|
68 | 13 | } |
|
69 | $this->serializer = $serializer; |
||
70 | } |
||
71 | 12 | ||
72 | private function createRequest(string $method, string $uri, array $data): RequestInterface |
||
73 | 12 | { |
|
74 | 12 | $request = $this->factory->createRequest($method, $uri); |
|
75 | 12 | $request = $request->withAddedHeader('Content-Type', 'application/json'); |
|
76 | $request->getBody()->write(\json_encode($data)); |
||
77 | 12 | ||
78 | return $request; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
79 | } |
||
80 | 12 | ||
81 | private function createPayload(array $data): array |
||
82 | 12 | { |
|
83 | $config = $this->configuration->toArray(); |
||
84 | 12 | ||
85 | return \array_merge($config, $data); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @throws ClientExceptionInterface |
||
90 | * @throws JsonException |
||
91 | 12 | */ |
|
92 | private function getContents(RequestInterface $request): string |
||
93 | 12 | { |
|
94 | $response = $this->client->sendRequest($request); |
||
95 | 12 | ||
96 | 12 | $json = $response->getBody()->getContents(); |
|
97 | |||
98 | if (false === \json_validate($json)) { |
||
99 | throw new JsonException(\json_last_error_msg(), \json_last_error()); |
||
100 | 12 | } |
|
101 | |||
102 | return $json; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | 1 | * @throws ClientExceptionInterface|JsonException |
|
107 | */ |
||
108 | 1 | public function getContractClient(GetContractClientsRequest $req): string |
|
109 | { |
||
110 | 1 | $payload = $this->createPayload($req->toArray()); |
|
111 | 1 | ||
112 | 1 | $request = $this->createRequest( |
|
113 | 1 | RequestMethodInterface::METHOD_POST, |
|
114 | 1 | self::API_URL . '/client/contract', |
|
115 | $payload |
||
116 | 1 | ); |
|
117 | |||
118 | return $this->getContents($request); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | 1 | * @throws ClientExceptionInterface|JsonException |
|
123 | */ |
||
124 | 1 | public function findCountry(Location\Country\FindCountryRequest $req): string |
|
125 | { |
||
126 | 1 | $payload = $this->createPayload($req->toArray()); |
|
127 | 1 | ||
128 | 1 | $request = $this->createRequest( |
|
129 | 1 | RequestMethodInterface::METHOD_POST, |
|
130 | 1 | self::API_URL . '/location/country', |
|
131 | $payload |
||
132 | 1 | ); |
|
133 | |||
134 | return $this->getContents($request); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @param FindStateRequest $req |
||
139 | * |
||
140 | * @return string |
||
141 | * @throws ClientExceptionInterface |
||
142 | 1 | * @throws JsonException |
|
143 | */ |
||
144 | 1 | public function findState(Location\State\FindStateRequest $req): string |
|
145 | { |
||
146 | 1 | $payload = $this->createPayload($req->toArray()); |
|
147 | 1 | ||
148 | 1 | $request = $this->createRequest( |
|
149 | 1 | RequestMethodInterface::METHOD_POST, |
|
150 | 1 | self::API_URL . '/location/state', |
|
151 | $payload |
||
152 | 1 | ); |
|
153 | |||
154 | return $this->getContents($request); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @param FindSiteRequest $req |
||
159 | * |
||
160 | 1 | * @return string |
|
161 | * @throws ClientExceptionInterface |
||
162 | 1 | * @throws JsonException |
|
163 | */ |
||
164 | 1 | public function findSite(Location\Site\FindSiteRequest $req): string |
|
165 | 1 | { |
|
166 | 1 | $payload = $this->createPayload($req->toArray()); |
|
167 | 1 | ||
168 | 1 | $request = $this->createRequest( |
|
169 | RequestMethodInterface::METHOD_POST, |
||
170 | 1 | self::API_URL . '/location/site', |
|
171 | $payload |
||
172 | ); |
||
173 | |||
174 | return $this->getContents($request); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @param FindOfficeRequest $req |
||
179 | * |
||
180 | * @return string |
||
181 | * @throws ClientExceptionInterface |
||
182 | 1 | * @throws JsonException |
|
183 | */ |
||
184 | 1 | public function findOffice(Location\Office\FindOfficeRequest $req): string |
|
185 | { |
||
186 | 1 | $payload = $this->createPayload($req->toArray()); |
|
187 | 1 | ||
188 | 1 | $request = $this->createRequest( |
|
189 | 1 | RequestMethodInterface::METHOD_POST, |
|
190 | 1 | self::API_URL . '/location/office', |
|
191 | $payload |
||
192 | 1 | ); |
|
193 | |||
194 | return $this->getContents($request); |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * @param FindComplexRequest $req |
||
199 | * |
||
200 | * @return string |
||
201 | * @throws ClientExceptionInterface |
||
202 | 1 | * @throws JsonException |
|
203 | */ |
||
204 | 1 | public function findComplex(Location\Complex\FindComplexRequest $req): string |
|
205 | { |
||
206 | 1 | $payload = $this->createPayload($req->toArray()); |
|
207 | 1 | ||
208 | 1 | $request = $this->createRequest( |
|
209 | 1 | RequestMethodInterface::METHOD_POST, |
|
210 | 1 | self::API_URL . '/location/complex', |
|
211 | $payload |
||
212 | 1 | ); |
|
213 | |||
214 | return $this->getContents($request); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @param FindStreetRequest $req |
||
219 | * |
||
220 | * @return string |
||
221 | * @throws ClientExceptionInterface |
||
222 | 1 | * @throws JsonException |
|
223 | */ |
||
224 | 1 | public function findStreet(Location\Street\FindStreetRequest $req): string |
|
225 | { |
||
226 | 1 | $payload = $this->createPayload($req->toArray()); |
|
227 | 1 | ||
228 | 1 | $request = $this->createRequest( |
|
229 | 1 | RequestMethodInterface::METHOD_POST, |
|
230 | 1 | self::API_URL . '/location/street', |
|
231 | $payload |
||
232 | 1 | ); |
|
233 | |||
234 | return $this->getContents($request); |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | 1 | * @throws ClientExceptionInterface|JsonException |
|
239 | */ |
||
240 | 1 | public function calculate(CalculationRequest $object): string |
|
241 | { |
||
242 | 1 | $payload = $this->createPayload($object->toArray()); |
|
243 | 1 | //dd($payload); |
|
244 | 1 | ||
245 | 1 | $request = $this->createRequest( |
|
246 | 1 | RequestMethodInterface::METHOD_POST, |
|
247 | self::API_URL . '/calculate', |
||
248 | 1 | $payload |
|
249 | ); |
||
250 | |||
251 | return $this->getContents($request); |
||
252 | } |
||
253 | |||
254 | 1 | /** |
|
255 | * @throws ClientExceptionInterface|JsonException |
||
256 | 1 | */ |
|
257 | public function track(TrackRequest $object): string |
||
258 | 1 | { |
|
259 | 1 | $payload = $this->createPayload($object->toArray()); |
|
260 | 1 | ||
261 | 1 | $request = $this->createRequest( |
|
262 | 1 | RequestMethodInterface::METHOD_POST, |
|
263 | self::API_URL . '/track', |
||
264 | 1 | $payload |
|
265 | ); |
||
266 | |||
267 | 1 | return $this->getContents($request); |
|
268 | } |
||
269 | 1 | ||
270 | public function print(PrintRequest $object): string |
||
271 | 1 | { |
|
272 | 1 | $payload = $this->createPayload($object->toArray()); |
|
273 | 1 | ||
274 | 1 | $request = $this->createRequest( |
|
275 | 1 | RequestMethodInterface::METHOD_POST, |
|
276 | self::API_URL . '/print', |
||
277 | 1 | $payload |
|
278 | ); |
||
279 | |||
280 | return $this->getContents($request); |
||
281 | } |
||
282 | |||
283 | 1 | /** |
|
284 | * @throws ClientExceptionInterface|JsonException |
||
285 | 1 | */ |
|
286 | public function createShipment(CreateShipmentRequest $req): string |
||
287 | 1 | { |
|
288 | 1 | $payload = $this->createPayload($req->toArray()); |
|
289 | 1 | ||
290 | 1 | $request = $this->createRequest( |
|
291 | 1 | RequestMethodInterface::METHOD_POST, |
|
292 | self::API_URL . '/shipment', |
||
293 | 1 | $payload |
|
294 | ); |
||
295 | |||
296 | return $this->getContents($request); |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * @throws ClientExceptionInterface |
||
301 | */ |
||
302 | public function cancelShipment(CancelShipmentRequest $object): string |
||
303 | { |
||
304 | $payload = $this->createPayload($object->toArray()); |
||
305 | |||
306 | $request = $this->createRequest( |
||
307 | RequestMethodInterface::METHOD_POST, |
||
308 | self::API_URL . '/shipment/cancel', |
||
309 | $payload |
||
310 | ); |
||
311 | |||
312 | return $this->getContents($request); |
||
313 | } |
||
314 | |||
315 | |||
316 | /** |
||
317 | * @throws JsonException |
||
318 | * @throws ClientExceptionInterface |
||
319 | */ |
||
320 | 1 | public function destination(DestinationServicesRequest $object): string |
|
321 | { |
||
322 | 1 | $payload = $this->createPayload($object->toArray()); |
|
323 | |||
324 | 1 | $request = $this->createRequest( |
|
325 | 1 | RequestMethodInterface::METHOD_POST, |
|
326 | 1 | self::API_URL . '/services/destination', |
|
327 | 1 | $payload |
|
328 | 1 | ); |
|
329 | |||
330 | 1 | return $this->getContents($request); |
|
331 | } |
||
332 | } |
||
333 |