Passed
Push — main ( 2611c0...fe37b5 )
by Vasil
03:26
created

Speedy::createRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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

453
    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...
454
    {
455 1
        return new PrintResponse();
456
    }
457
458
    /**
459
     * @param CreateShipmentRequest $object
460
     * @return CreateShipmentResponse
461
     * @throws ClientExceptionInterface
462
     */
463 1
    public function createShipment(CreateShipmentRequest $object): CreateShipmentResponse
464
    {
465 1
        $payload = $this->createPayload($object->toArray());
466
467
468 1
        $request = $this->createRequest(
469 1
            RequestMethodInterface::METHOD_POST,
470 1
            self::API_URL . '/shipment',
471 1
            $payload
472 1
        );
473
474 1
        $json = $this->getContents($request);
475
476 1
        return (new CreateShipmentResponseFactory())($json);
477
    }
478
479
    /**
480
     * @param CancelShipmentRequest $object
481
     * @return CancelShipmentResponse
482
     * @throws ClientExceptionInterface
483
     */
484
    public function cancelShipment(CancelShipmentRequest $object): CancelShipmentResponse
485
    {
486
        $payload = $this->createPayload($object->toArray());
487
488
        $request = $this->createRequest(
489
            RequestMethodInterface::METHOD_POST,
490
            self::API_URL . '/shipment/cancel',
491
            $payload
492
        );
493
494
        $json = $this->getContents($request);
495
496
        /** @var CancelShipmentResponse */
497
        return (new CancelShipmentResponseFactory())($json);
498
    }
499
500
    /**
501
     * @throws ClientExceptionInterface
502
     */
503
    public function destination(DestinationServicesRequest $object): DestinationServicesResponse
504
    {
505
        $payload = $this->createPayload($object->toArray());
506
507
        $request = $this->createRequest(
508
            RequestMethodInterface::METHOD_POST,
509
            self::API_URL . '/services/destination',
510
            $payload
511
        );
512
513
        $json = $this->getContents($request);
514
515
        return (new DestinationServicesResponseFactory())($json);
516
    }
517
}
518