Test Failed
Push — main ( d030ca...7189b9 )
by Vasil
05:52
created

Speedy::destination()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

429
    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...
430
    {
431
        return new TrackResponse();
432
    }
433
434
    /**
435 1
     * @param PrintRequest $request
436
     * @return PrintResponse
437 1
     */
438
    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

438
    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...
439
    {
440
        return new PrintResponse();
441
    }
442
443
    /**
444 1
     * @param CreateShipmentRequest $request
445
     * @return CreateShipmentResponse
446 1
     */
447
    public function createShipment(CreateShipmentRequest $request): CreateShipmentResponse
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

447
    public function createShipment(/** @scrutinizer ignore-unused */ CreateShipmentRequest $request): CreateShipmentResponse

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...
448
    {
449
        return new CreateShipmentResponse();
450
    }
451
452
    /**
453
     * @throws ClientExceptionInterface
454
     */
455
    public function destination(DestinationServicesRequest $object): DestinationServicesResponse
456
    {
457
        $payload = $this->createPayload($object->toArray());
458
459
        $request = $this->createRequest(
460
            RequestMethodInterface::METHOD_POST,
461
            self::API_URL . '/services/destination',
462
            $payload
463
        );
464
465
        $json = $this->getContents($request);
466
467
        return (new DestinationServicesResponseFactory())($json);
468
    }
469
}
470