Passed
Push — main ( 5489db...717dd1 )
by Vasil
15:04
created

Speedy   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 444
Duplicated Lines 0 %

Test Coverage

Coverage 90.09%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 225
dl 0
loc 444
ccs 100
cts 111
cp 0.9009
rs 10
c 4
b 0
f 0
wmc 17

17 Methods

Rating   Name   Duplication   Size   Complexity  
A createPayload() 0 5 1
A print() 0 3 1
A findStreet() 0 13 1
A getContents() 0 5 1
A getContractClient() 0 14 1
A findSite() 0 13 1
A findOffice() 0 13 1
A createRequest() 0 9 1
A __construct() 0 8 1
A cancelShipment() 0 3 1
A findState() 0 14 1
A createShipment() 0 14 1
A findComplex() 0 13 1
A findCountry() 0 14 1
A destination() 0 13 1
A track() 0 3 1
A calculation() 0 13 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VasilDakov\Speedy;
6
7
use Fig\Http\Message\RequestMethodInterface;
8
use Psr\Http\Client\ClientExceptionInterface;
9
use Psr\Http\Client\ClientInterface;
10
use Psr\Http\Message\RequestFactoryInterface;
11
use Psr\Http\Message\RequestInterface;
12
use Throwable;
13
use VasilDakov\Speedy\Service\Calculation\CalculationRequest;
14
use VasilDakov\Speedy\Service\Calculation\CalculationResponse;
15
use VasilDakov\Speedy\Service\Calculation\CalculationResponseFactory;
16
use VasilDakov\Speedy\Service\Client\GetContractClientsRequest;
17
use VasilDakov\Speedy\Service\Client\GetContractClientsResponse;
18
use VasilDakov\Speedy\Service\Client\GetContractClientsResponseFactory;
19
use VasilDakov\Speedy\Service\Location;
20
use VasilDakov\Speedy\Service\Location\Complex\FindComplexRequest;
21
use VasilDakov\Speedy\Service\Location\Complex\FindComplexResponse;
22
use VasilDakov\Speedy\Service\Location\Office\FindOfficeRequest;
23
use VasilDakov\Speedy\Service\Location\Office\FindOfficeResponse;
24
use VasilDakov\Speedy\Service\Location\Site\FindSiteResponse;
25
use VasilDakov\Speedy\Service\Printing\PrintRequest;
26
use VasilDakov\Speedy\Service\Printing\PrintResponse;
27
use VasilDakov\Speedy\Service\Service\DestinationServicesRequest;
28
use VasilDakov\Speedy\Service\Service\DestinationServicesResponse;
29
use VasilDakov\Speedy\Service\Service\DestinationServicesResponseFactory;
30
use VasilDakov\Speedy\Service\Shipment\CancelShipmentRequest;
31
use VasilDakov\Speedy\Service\Shipment\CancelShipmentResponse;
32
use VasilDakov\Speedy\Service\Shipment\CreateShipmentRequest;
33
use VasilDakov\Speedy\Service\Shipment\CreateShipmentResponse;
34
use VasilDakov\Speedy\Service\Shipment\CreateShipmentResponseFactory;
35
use VasilDakov\Speedy\Service\Track\TrackRequest;
36
use VasilDakov\Speedy\Service\Track\TrackResponse;
37
38
use function Amp\Promise\rethrow;
39
use function array_merge;
40
use function json_encode;
41
42
/**
43
 * Class Speedy
44
 *
45
 * @author    Vasil Dakov <[email protected]>
46
 * @copyright 2009-2022 Neutrino.bg
47
 * @version   1.0
48
 */
49
final class Speedy
50
{
51
    public const API_URL = 'https://api.speedy.bg/v1';
52
    public const USER_NAME = 'userName';
53
    public const PASSWORD  = 'password';
54
    public const LANGUAGE  = 'language';
55
    public const CLIENT_SYSTEM_ID = 'clientSystemId';
56
    public const SENDER = 'sender';
57
    public const CLIENT_ID = 'clientId';
58
    public const PRIVATE_PERSON = 'privatePerson';
59
    public const DROP_OFF_OFFICE_ID = 'dropoffOfficeId';
60
    public const PICKUP_OFFICE_ID = 'pickupOfficeId';
61
    public const ADDRESS_LOCATION = 'addressLocation';
62
    public const COUNTRY_ID = 'countryId';
63
    public const STATE_ID = 'stateId';
64
    public const SITE_ID = 'siteId';
65
    public const SITE_TYPE = 'siteType';
66
    public const SITE_NAME = 'siteName';
67
    public const POST_CODE = 'postCode';
68
    public const RECIPIENT = 'recipient';
69
    public const SERVICE = 'service';
70
    public const PICKUP_DATE = 'pickupDate';
71
    public const AUTO_ADJUST_PICKUP_DATE = 'autoAdjustPickupDate';
72
    public const SERVICE_IDS = 'serviceIds';
73
    public const DEFERRED_DAYS = 'deferredDays';
74
    public const SATURDAY_DELIVERY = 'saturdayDelivery';
75
    public const ADDITIONAL_SERVICES = 'additionalServices';
76
    public const FIXED_TIME_DELIVERY = 'fixedTimeDelivery';
77
    public const SPECIAL_DELIVERY_ID = 'specialDeliveryId';
78
    public const DELIVERY_TO_FLOOR = 'deliveryToFloor';
79
    public const COD = 'cod';
80
    public const AMOUNT = 'amount';
81
    public const CURRENCY_CODE = 'currencyCode';
82
    public const PROCESSING_TYPE = 'processingType';
83
    public const PAYOUT_TO_THIRD_PARTY = 'payoutToThirdParty';
84
    public const INCLUDE_SHIPPING_PRICE = 'includeShippingPrice';
85
    public const OBP_DETAILS = 'obpDetails';
86
    public const OPTION = 'option';
87
    public const RETURN_SHIPMENT_SERVICE_ID = 'returnShipmentServiceId';
88
    public const RETURN_SHIPMENT_PAYER = 'returnShipmentPayer';
89
    public const DECLARED_VALUE = 'declaredValue';
90
    public const FRAGILE = 'fragile';
91
    public const IGNORE_IF_NOT_APPLICABLE = 'ignoreIfNotApplicable';
92
    public const RETURNS = 'returns';
93
    public const ROD = 'rod';
94
    public const ENABLED = 'enabled';
95
    public const COMMENT = 'comment';
96
    public const RETURN_TO_CLIENT_ID = 'returnToClientId';
97
    public const RETURN_TO_OFFICE_ID = 'returnToOfficeId';
98
    public const THIRD_PARTY_PAYER = 'thirdPartyPayer';
99
    public const RETURN_RECEIPT = 'returnReceipt';
100
    public const SWAP = 'swap';
101
    public const SERVICE_ID = 'serviceId';
102
    public const PARCELS_COUNT = 'parcelsCount';
103
    public const ROP = 'rop';
104
    public const PALLETS = 'pallets';
105
    public const RETURN_VOUCHER = 'returnVoucher';
106
    public const PAYER = 'payer';
107
    public const CONTENT = 'content';
108
    public const TOTAL_WEIGHT = 'totalWeight';
109
    public const DOCUMENTS = 'documents';
110
    public const PALLETIZED = 'palletized';
111
    public const PARCELS = 'parcels';
112
    public const ID = 'id';
113
    public const SEQ_NO = 'seqNo';
114
    public const PACKAGE_UNIQUE_NUMBER = 'packageUniqueNumber';
115
    public const WEIGHT = 'weight';
116
    public const EXTERNAL_CARRIER_PARCEL_NUMBER = 'externalCarrierParcelNumber';
117
    public const SIZE = 'size';
118
    public const WIDTH = 'width';
119
    public const DEPTH = 'depth';
120
    public const HEIGHT = 'height';
121
    public const PAYMENT = 'payment';
122
    public const COURIER_SERVICE_PAYER = 'courierServicePayer';
123
    public const DECLARED_VALUE_PAYER = 'declaredValuePayer';
124
    public const PACKAGE_PAYER = 'packagePayer';
125
    public const THIRD_PARTY_CLIENT_ID = 'thirdPartyClientId';
126
    public const DISCOUNT_CARD_ID = 'discountCardId';
127
    public const CONTRACT_ID = 'contractId';
128
    public const CARD_ID = 'cardId';
129
    public const NAME = 'name';
130
    public const TYPE = 'type';
131
    public const MUNICIPALITY = 'municipality';
132
    public const REGION = 'region';
133
    public const LIMIT = 'limit';
134
    public const ISO_ALPHA_3 = 'isoAlpha3';
135
    public const ISO_ALPHA_2 = 'isoAlpha2';
136
    public const ADDRESS = 'address';
137
    public const STREET_ID = 'streetId';
138
    public const STREET_TYPE = 'streetType';
139
    public const STREET_NAME = 'streetName';
140
    public const STREET_NO = 'streetNo';
141
    public const COMPLEX_ID = 'complexId';
142
    public const COMPLEX_TYPE = 'complexType';
143
    public const COMPLEX_NAME = 'complexName';
144
    public const BLOCK_NO = 'blockNo';
145
    public const ENTRANCE_NO = 'entranceNo';
146
    public const FLOOR_NO = 'floorNo';
147
    public const APARTMENT_NO = 'apartmentNo';
148
    public const POI_ID = 'poiId';
149
    public const ADDRESS_NOTE = 'addressNote';
150
    public const ADDRESS_LINE_1 = 'addressLine1';
151
    public const ADDRESS_LINE_2 = 'addressLine2';
152
    public const X = 'x';
153
    public const Y = 'y';
154
    public const SHIPMENT_NOTE = 'shipmentNote';
155
    public const REF_1 = 'ref1';
156
    public const REF_2 = 'ref2';
157
    public const PHONE_1 = 'phone1';
158
    public const PHONE_2 = 'phone2';
159
    public const PHONE_3 = 'phone3';
160
    public const NUMBER = 'number';
161
    public const EXTENSION = 'extension';
162
    public const CLIENT_NAME = 'clientName';
163
    public const CONTACT_NAME = 'contactName';
164
    public const EMAIL = 'email';
165
    public const OBJECT_NAME = 'objectName';
166
    public const CONTENTS = 'contents';
167
    public const PACKAGE = 'package';
168
    public const PENDING_PARCELS = 'pendingParcels';
169
    public const STARTING_DATE = 'startingDate';
170
    public const SENDER_HAS_PAYMENT = 'senderHasPayment';
171
    public const SHIPMENT_ID = 'shipmentId';
172
    public const FORMAT = 'format';
173
    public const PAPER_SIZE = 'paperSize';
174
    public const PRINTER_NAME = 'printerName';
175
    public const DPI = 'dpi';
176
    public const PARCEL = 'parcel';
177
    public const ADDITIONAL_BARCODE = 'additionalBarcode';
178
    public const VALUE = 'value';
179
    public const LABEL = 'label';
180
    public const SHIPMENT_IDS = 'shipmentIds';
181
    public const LAST_OPERATION_ONLY = 'lastOperationOnly';
182
    public const CONSOLIDATION_REF = 'consolidationRef';
183
    public const REQUIRE_UNSUCCESSFUL_DELIVERY_STICKER_IMAGE = 'requireUnsuccessfulDeliveryStickerImage';
184
    public const EXCISE_GOODS = 'exciseGoods';
185
    public const SENDER_BANK_ACCOUNT = 'senderBankAccount';
186
    public const IBAN = 'iban';
187
    public const ACCOUNT_HOLDER = 'accountHolder';
188
189
    /**
190
     * @var Configuration
191
     */
192
    private Configuration $configuration;
193
194
    /**
195
     * PSR-18: HTTP Client
196
     * @var ClientInterface
197
     */
198
    private ClientInterface $client;
199
200
    /**
201
     * PSR-17: HTTP Factories
202
     * @var RequestFactoryInterface
203
     */
204
    private RequestFactoryInterface $factory;
205
206
    /**
207
     * @param Configuration $configuration
208
     * @param ClientInterface $client
209
     * @param RequestFactoryInterface $factory
210
     */
211 12
    public function __construct(
212
        Configuration $configuration,
213
        ClientInterface $client,
214
        RequestFactoryInterface $factory
215
    ) {
216 12
        $this->configuration = $configuration;
217 12
        $this->client  = $client;
218 12
        $this->factory = $factory;
219
    }
220
221
    /**
222
     * @param string $method
223
     * @param string $uri
224
     * @param array $data
225
     * @return RequestInterface
226
     */
227 9
    private function createRequest(string $method, string $uri, array $data): RequestInterface
228
    {
229 9
        $request = $this->factory->createRequest($method, $uri);
230
231 9
        $request = $request->withAddedHeader('Content-Type', 'application/json');
232
233 9
        $request->getBody()->write(json_encode($data));
234
235 9
        return $request;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $request returns the type Psr\Http\Message\MessageInterface which includes types incompatible with the type-hinted return Psr\Http\Message\RequestInterface.
Loading history...
236
    }
237
238
    /**
239
     * @param array $data
240
     * @return array
241
     */
242 9
    private function createPayload(array $data): array
243
    {
244 9
        $config = $this->configuration->toArray();
245
246 9
        return array_merge($config, $data);
247
    }
248
249
    /**
250
     * @param RequestInterface $request
251
     * @return string
252
     * @throws ClientExceptionInterface
253
     */
254 9
    private function getContents(RequestInterface $request): string
255
    {
256 9
        $response = $this->client->sendRequest($request);
257
258 9
        return $response->getBody()->getContents();
259
    }
260
261
262
    /**
263
     * @param GetContractClientsRequest $object
264
     * @return GetContractClientsResponse
265
     * @throws ClientExceptionInterface
266
     * @throws Throwable
267
     */
268 1
    public function getContractClient(
269
        GetContractClientsRequest $object
270
    ): GetContractClientsResponse {
271 1
        $payload = $this->createPayload($object->toArray());
272
273 1
        $request = $this->createRequest(
274 1
            RequestMethodInterface::METHOD_POST,
275 1
            self::API_URL . '/client/contract',
276 1
            $payload
277 1
        );
278
279 1
        $json = $this->getContents($request);
280
281 1
        return (new GetContractClientsResponseFactory())($json);
282
    }
283
284
    /**
285
     * @param Location\Country\FindCountryRequest $object
286
     * @return Location\Country\FindCountryResponse
287
     * @throws ClientExceptionInterface
288
     */
289 1
    public function findCountry(
290
        Location\Country\FindCountryRequest $object
291
    ): Location\Country\FindCountryResponse {
292 1
        $payload = $this->createPayload($object->toArray());
293
294 1
        $request = $this->createRequest(
295 1
            RequestMethodInterface::METHOD_POST,
296 1
            self::API_URL . '/location/country',
297 1
            $payload
298 1
        );
299
300 1
        $json = $this->getContents($request);
301
302 1
        return (new Location\Country\FindCountryResponseFactory())($json);
303
    }
304
305
    /**
306
     * @param Location\State\FindStateRequest $request
307
     * @return Location\State\FindStateResponse
308
     * @throws ClientExceptionInterface
309
     */
310 1
    public function findState(
311
        Location\State\FindStateRequest $request
312
    ): Location\State\FindStateResponse {
313 1
        $payload = $this->createPayload($request->toArray());
314
315 1
        $request = $this->createRequest(
316 1
            RequestMethodInterface::METHOD_POST,
317 1
            self::API_URL . '/location/state',
318 1
            $payload
319 1
        );
320
321 1
        $json = $this->getContents($request);
322
323 1
        return (new Location\State\FindStateResponseFactory())($json);
324
    }
325
326
327
    /**
328
     * @param Location\Site\FindSiteRequest $object
329
     * @return FindSiteResponse
330
     * @throws ClientExceptionInterface
331
     */
332 1
    public function findSite(Location\Site\FindSiteRequest $object): Location\Site\FindSiteResponse
333
    {
334 1
        $payload = $this->createPayload($object->toArray());
335
336 1
        $request = $this->createRequest(
337 1
            RequestMethodInterface::METHOD_POST,
338 1
            self::API_URL . '/location/site',
339 1
            $payload
340 1
        );
341
342 1
        $json = $this->getContents($request);
343
344 1
        return (new Location\Site\FindSiteResponseFactory())($json);
345
    }
346
347
348
    /**
349
     * @param FindOfficeRequest $object
350
     * @return FindOfficeResponse
351
     * @throws ClientExceptionInterface
352
     */
353 1
    public function findOffice(Location\Office\FindOfficeRequest $object): FindOfficeResponse
354
    {
355 1
        $payload = $this->createPayload($object->toArray());
356
357 1
        $request = $this->createRequest(
358 1
            RequestMethodInterface::METHOD_POST,
359 1
            self::API_URL . '/location/office',
360 1
            $payload
361 1
        );
362
363 1
        $json = $this->getContents($request);
364
365 1
        return (new Location\Office\FindOfficeResponseFactory())($json);
366
    }
367
368
369
    /**
370
     * @param FindComplexRequest $object
371
     * @return FindComplexResponse
372
     * @throws ClientExceptionInterface
373
     */
374 1
    public function findComplex(Location\Complex\FindComplexRequest $object): Location\Complex\FindComplexResponse
375
    {
376 1
        $payload = $this->createPayload($object->toArray());
377
378 1
        $request = $this->createRequest(
379 1
            RequestMethodInterface::METHOD_POST,
380 1
            self::API_URL . '/location/complex',
381 1
            $payload
382 1
        );
383
384 1
        $json = $this->getContents($request);
385
386 1
        return (new Location\Complex\FindComplexResponseFactory())($json);
387
    }
388
389
    /**
390
     * @param Location\Street\FindStreetRequest $object
391
     * @return Location\Street\FindStreetResponse
392
     * @throws ClientExceptionInterface
393
     */
394 1
    public function findStreet(Location\Street\FindStreetRequest $object): Location\Street\FindStreetResponse
395
    {
396 1
        $payload = $this->createPayload($object->toArray());
397
398 1
        $request = $this->createRequest(
399 1
            RequestMethodInterface::METHOD_POST,
400 1
            self::API_URL . '/location/street',
401 1
            $payload
402 1
        );
403
404 1
        $json = $this->getContents($request);
405
406 1
        return (new Location\Street\FindStreetResponseFactory())($json);
407
    }
408
409
    /**
410
     * @param CalculationRequest $object
411
     * @return CalculationResponse
412
     * @throws ClientExceptionInterface
413
     */
414 1
    public function calculation(CalculationRequest $object): CalculationResponse
415
    {
416 1
        $payload = $this->createPayload($object->toArray());
417
418 1
        $request = $this->createRequest(
419 1
            RequestMethodInterface::METHOD_POST,
420 1
            self::API_URL . '/calculate',
421 1
            $payload
422 1
        );
423
424 1
        $json = $this->getContents($request);
425
426 1
        return (new CalculationResponseFactory())($json);
427
    }
428
429
    /**
430
     * @param TrackRequest $request
431
     * @return TrackResponse
432
     */
433 1
    public function track(TrackRequest $request): TrackResponse
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

433
    public function track(/** @scrutinizer ignore-unused */ TrackRequest $request): TrackResponse

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
434
    {
435 1
        return new TrackResponse();
436
    }
437
438
    /**
439
     * @param PrintRequest $request
440
     * @return PrintResponse
441
     */
442 1
    public function print(PrintRequest $request): PrintResponse
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

442
    public function print(/** @scrutinizer ignore-unused */ PrintRequest $request): PrintResponse

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
443
    {
444 1
        return new PrintResponse();
445
    }
446
447
    /**
448
     * @param CreateShipmentRequest $object
449
     * @return CreateShipmentResponse
450
     * @throws ClientExceptionInterface
451
     */
452 1
    public function createShipment(CreateShipmentRequest $object): CreateShipmentResponse
453
    {
454 1
        $payload = $this->createPayload($object->toArray());
455
456
457 1
        $request = $this->createRequest(
458 1
            RequestMethodInterface::METHOD_POST,
459 1
            self::API_URL . '/shipment',
460 1
            $payload
461 1
        );
462
463 1
        $json = $this->getContents($request);
464
465 1
        return (new CreateShipmentResponseFactory())($json);
466
    }
467
468
    /**
469
     * @param CancelShipmentRequest $object
470
     * @return CancelShipmentResponse
471
     */
472
    public function cancelShipment(CancelShipmentRequest $object): CancelShipmentResponse
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

472
    public function cancelShipment(/** @scrutinizer ignore-unused */ CancelShipmentRequest $object): CancelShipmentResponse

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
473
    {
474
        return new CancelShipmentResponse();
475
    }
476
477
    /**
478
     * @throws ClientExceptionInterface
479
     */
480
    public function destination(DestinationServicesRequest $object): DestinationServicesResponse
481
    {
482
        $payload = $this->createPayload($object->toArray());
483
484
        $request = $this->createRequest(
485
            RequestMethodInterface::METHOD_POST,
486
            self::API_URL . '/services/destination',
487
            $payload
488
        );
489
490
        $json = $this->getContents($request);
491
492
        return (new DestinationServicesResponseFactory())($json);
493
    }
494
}
495