Passed
Push — main ( 88de3c...8bdffa )
by Vasil
03:17
created

Speedy::createShipment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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

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

435
    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...
436
    {
437 1
        return new PrintResponse();
438
    }
439
440
    /**
441
     * @param CreateShipmentRequest $request
442
     * @return CreateShipmentResponse
443
     */
444 1
    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

444
    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...
445
    {
446 1
        return new CreateShipmentResponse();
447
    }
448
}
449