Completed
Push — master ( b1578e...6e5d14 )
by Tomáš
04:02
created

Balikobot::orderB2AShipment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
rs 9.7
ccs 9
cts 9
cp 1
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Inspirum\Balikobot\Services;
4
5
use DateTime;
6
use Inspirum\Balikobot\Contracts\RequesterInterface;
7
use Inspirum\Balikobot\Definitions\Option;
8
use Inspirum\Balikobot\Definitions\Shipper;
9
use Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection;
10
use Inspirum\Balikobot\Model\Aggregates\PackageCollection;
11
use Inspirum\Balikobot\Model\Values\Branch;
12
use Inspirum\Balikobot\Model\Values\OrderedPackage;
13
use Inspirum\Balikobot\Model\Values\OrderedShipment;
14
use Inspirum\Balikobot\Model\Values\Package;
15
use Inspirum\Balikobot\Model\Values\PackageStatus;
16
use Inspirum\Balikobot\Model\Values\PostCode;
17
18
class Balikobot
19
{
20
    /**
21
     * Balikobot API
22
     *
23
     * @var \Inspirum\Balikobot\Services\Client
24
     */
25
    private $client;
26
27
    /**
28
     * Balikobot constructor
29
     *
30
     * @param \Inspirum\Balikobot\Contracts\RequesterInterface $requester
31
     */
32 48
    public function __construct(RequesterInterface $requester)
33
    {
34 48
        $this->client = new Client($requester);
35 48
    }
36
37
    /**
38
     * All supported shipper services
39
     *
40
     * @return string[]
41
     */
42 1
    public function getShippers(): array
43
    {
44 1
        return Shipper::all();
45
    }
46
47
    /**
48
     * Add packages
49
     *
50
     * @param \Inspirum\Balikobot\Model\Aggregates\PackageCollection $packages
51
     *
52
     * @return \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection|\Inspirum\Balikobot\Model\Values\OrderedPackage[]
53
     *
54
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
55
     */
56 3
    public function addPackages(PackageCollection $packages): OrderedPackageCollection
57
    {
58 3
        $response = $this->client->addPackages($packages->getShipper(), $packages->toArray());
59
60
        // create return value object
61 3
        $orderedPackages = new OrderedPackageCollection();
62
63 3
        foreach ($response as $i => $package) {
64 3
            $orderedPackages->add(OrderedPackage::newInstanceFromData(
65 3
                $packages->getShipper(),
66 3
                $packages->getEID(),
67 3
                $package
68
            ));
69
        }
70
71 3
        return $orderedPackages;
72
    }
73
74
    /**
75
     * Exports order into Balikobot system
76
     *
77
     * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages
78
     *
79
     * @return void
80
     *
81
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
82
     */
83 1
    public function dropPackages(OrderedPackageCollection $packages): void
84
    {
85 1
        $this->client->dropPackages($packages->getShipper(), $packages->getPackageIds());
86 1
    }
87
88
    /**
89
     * Exports order into Balikobot system
90
     *
91
     * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package
92
     *
93
     * @return void
94
     *
95
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
96
     */
97 1
    public function dropPackage(OrderedPackage $package): void
98
    {
99 1
        $this->client->dropPackage($package->getShipper(), $package->getPackageId());
100 1
    }
101
102
    /**
103
     * Order shipment
104
     *
105
     * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages
106
     * @param \DateTime|null                                                $date
107
     *
108
     * @return \Inspirum\Balikobot\Model\Values\OrderedShipment
109
     *
110
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
111
     */
112 2
    public function orderShipment(OrderedPackageCollection $packages, DateTime $date = null): OrderedShipment
113
    {
114 2
        if ($date !== null) {
115 2
            $date->setTime(0, 0, 0);
116
        }
117
118 2
        $response = $this->client->orderShipment($packages->getShipper(), $packages->getPackageIds(), $date);
119
120 2
        $orderedShipment = OrderedShipment::newInstanceFromData(
121 2
            $packages->getShipper(),
122 2
            $packages->getPackageIds(),
123 2
            $response,
124 2
            $date
125
        );
126
127 2
        return $orderedShipment;
128
    }
129
130
    /**
131
     * Track package
132
     *
133
     * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package
134
     *
135
     * @return \Inspirum\Balikobot\Model\Values\PackageStatus[]
136
     *
137
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
138
     */
139 2
    public function trackPackage(OrderedPackage $package): array
140
    {
141 2
        $response = $this->client->trackPackage($package->getShipper(), $package->getCarrierId());
142
143 2
        $statuses = [];
144
145 2
        foreach ($response as $status) {
146 2
            $statuses[] = PackageStatus::newInstanceFromData($status);
147
        }
148
149 2
        return $statuses;
150
    }
151
152
    /**
153
     * Track package last status
154
     *
155
     * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package
156
     *
157
     * @return \Inspirum\Balikobot\Model\Values\PackageStatus
158
     *
159
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
160
     */
161 2
    public function trackPackageLastStatus(OrderedPackage $package): PackageStatus
162
    {
163 2
        $response = $this->client->trackPackageLastStatus($package->getShipper(), $package->getCarrierId());
164
165 2
        $status = PackageStatus::newInstanceFromData($response);
166
167 2
        return $status;
168
    }
169
170
    /**
171
     * Get overview for given shipper
172
     *
173
     * @param string $shipper
174
     *
175
     * @return \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection
176
     *
177
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
178
     */
179 2
    public function getOverview(string $shipper): OrderedPackageCollection
180
    {
181 2
        $response = $this->client->getOverview($shipper);
182
183
        // create return value object
184 2
        $orderedPackages = new OrderedPackageCollection();
185
186 2
        foreach ($response as $i => $package) {
187 1
            $orderedPackages->add(OrderedPackage::newInstanceFromData(
188 1
                $shipper,
189 1
                $package['eshop_id'],
190 1
                $package
191
            ));
192
        }
193
194 2
        return $orderedPackages;
195
    }
196
197
    /**
198
     * Get labels for orders
199
     *
200
     * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages
201
     *
202
     * @return string
203
     *
204
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
205
     */
206 2
    public function getLabels(OrderedPackageCollection $packages): string
207
    {
208 2
        $response = $this->client->getLabels($packages->getShipper(), $packages->getPackageIds());
209
210 2
        return $response;
211
    }
212
213
    /**
214
     * Gets complete information about a package
215
     *
216
     * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package
217
     *
218
     * @return \Inspirum\Balikobot\Model\Values\Package
219
     *
220
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
221
     */
222 2
    public function getPackageInfo(OrderedPackage $package): Package
223
    {
224 2
        $response = $this->client->getPackageInfo($package->getShipper(), $package->getPackageId());
225
226 2
        $options              = $response;
227 2
        $options[Option::EID] = $package->getBatchId();
228
229 2
        unset($options['package_id']);
230 2
        unset($options['eshop_id']);
231 2
        unset($options['carrier_id']);
232 2
        unset($options['track_url']);
233 2
        unset($options['label_url']);
234 2
        unset($options['carrier_id_swap']);
235 2
        unset($options['pieces']);
236
237 2
        $package = new Package($options);
238
239 2
        return $package;
240
    }
241
242
    /**
243
     * Gets complete information about a package
244
     *
245
     * @param string $shipper
246
     * @param int    $orderId
247
     *
248
     * @return \Inspirum\Balikobot\Model\Values\OrderedShipment
249
     *
250
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
251
     */
252 2
    public function getOrder(string $shipper, int $orderId): OrderedShipment
253
    {
254 2
        $response = $this->client->getOrder($shipper, $orderId);
255
256 2
        $orderedShipment = OrderedShipment::newInstanceFromData(
257 2
            $shipper,
258 2
            $response['package_ids'],
259 2
            $response
260
        );
261
262 2
        return $orderedShipment;
263
    }
264
265
    /**
266
     * Returns available services for the given shipper
267
     *
268
     * @param string $shipper
269
     *
270
     * @return string[]
271
     *
272
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
273
     */
274 4
    public function getServices(string $shipper): array
275
    {
276 4
        $services = $this->client->getServices($shipper);
277
278 3
        return $services;
279
    }
280
281
    /**
282
     * Returns available manipulation units for the given shipper
283
     *
284
     * @param string $shipper
285
     *
286
     * @return string[]
287
     *
288
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
289
     */
290 2
    public function getManipulationUnits(string $shipper): array
291
    {
292 2
        $units = $this->client->getManipulationUnits($shipper);
293
294 2
        return $units;
295
    }
296
297
    /**
298
     * Get all available branches
299
     *
300
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
301
     *
302
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
303
     */
304 1
    public function getBranches(): iterable
305
    {
306
        // get all shipper service codes
307 1
        $shippers = $this->getShippers();
308
309 1
        foreach ($shippers as $shipper) {
310 1
            yield from $this->getBranchesForShipper($shipper);
311
        }
312 1
    }
313
314
    /**
315
     * Get all available branches for given shipper
316
     *
317
     * @param string $shipper
318
     *
319
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
320
     *
321
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
322
     */
323 2
    public function getBranchesForShipper(string $shipper): iterable
324
    {
325
        // get all services for shipper service
326 2
        $services = array_keys($this->getServices($shipper));
327
328
        // support shipper withou service type
329 2
        if (empty($services)) {
330 1
            $services = [null];
331
        }
332
333
        // get branches for all services
334 2
        foreach ($services as $service) {
335 2
            yield from $this->getBranchesForShipperService($shipper, $service);
336
        }
337 2
    }
338
339
    /**
340
     * Get all available branches for given shipper and service type
341
     *
342
     * @param string      $shipper
343
     * @param string|null $service
344
     *
345
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
346
     *
347
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
348
     */
349 2
    public function getBranchesForShipperService(string $shipper, ?string $service): iterable
350
    {
351 2
        $fullData = Shipper::hasFullBranchesSupport($shipper, $service);
352 2
        $branches = $this->client->getBranches($shipper, $service, $fullData);
353
354 2
        foreach ($branches as $branch) {
355 1
            yield Branch::newInstanceFromData($shipper, $service, $branch);
356
        }
357 2
    }
358
359
    /**
360
     * Get all available branches for given shipper
361
     *
362
     * @param string      $shipper
363
     * @param string      $country
364
     * @param string      $city
365
     * @param string|null $postcode
366
     * @param string|null $street
367
     * @param int|null    $maxResults
368
     * @param float|null  $radius
369
     * @param string|null $type
370
     *
371
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
372
     *
373
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
374
     */
375 3
    public function getBranchesForLocation(
376
        string $shipper,
377
        string $country,
378
        string $city,
379
        string $postcode = null,
380
        string $street = null,
381
        int $maxResults = null,
382
        float $radius = null,
383
        string $type = null
384
    ): iterable {
385 3
        $branches = $this->client->getBranchesForLocation(
386 3
            $shipper,
387 3
            $country,
388 3
            $city,
389 3
            $postcode,
390 3
            $street,
391 3
            $maxResults,
392 3
            $radius,
393 3
            $type
394
        );
395
396 3
        foreach ($branches as $branch) {
397 1
            yield Branch::newInstanceFromData($shipper, null, $branch);
398
        }
399 3
    }
400
401
    /**
402
     * Returns list of countries where service with cash-on-delivery payment type is available in
403
     *
404
     * @param string $shipper
405
     *
406
     * @return array[]
407
     *
408
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
409
     */
410 2
    public function getCodCountries(string $shipper): array
411
    {
412 2
        $countries = $this->client->getCodCountries($shipper);
413
414 2
        return $countries;
415
    }
416
417
    /**
418
     * Returns list of countries where service is available in
419
     *
420
     * @param string $shipper
421
     *
422
     * @return array[]
423
     *
424
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
425
     */
426 2
    public function getCountries(string $shipper): array
427
    {
428 2
        $countries = $this->client->getCountries($shipper);
429
430 2
        return $countries;
431
    }
432
433
    /**
434
     * Returns available branches for the given shipper and its service
435
     *
436
     * @param string      $shipper
437
     * @param string      $service
438
     * @param string|null $country
439
     *
440
     * @return \Generator|\Inspirum\Balikobot\Model\Values\PostCode[]
441
     *
442
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
443
     */
444 2
    public function getPostCodes(string $shipper, string $service, string $country = null): iterable
445
    {
446 2
        $postcodes = $this->client->getPostCodes($shipper, $service, $country);
447
448 2
        foreach ($postcodes as $postcode) {
449 1
            yield PostCode::newInstanceFromData($shipper, $service, $postcode);
450
        }
451 2
    }
452
453
    /**
454
     * Check package(s) data
455
     *
456
     * @param \Inspirum\Balikobot\Model\Aggregates\PackageCollection $packages
457
     *
458
     * @return void
459
     *
460
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
461
     */
462 1
    public function checkPackages(PackageCollection $packages): void
463
    {
464 1
        $this->client->checkPackages($packages->getShipper(), $packages->toArray());
465 1
    }
466
467
    /**
468
     * Returns available manipulation units for the given shipper
469
     *
470
     * @param string $shipper
471
     *
472
     * @return string[]
473
     *
474
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
475
     */
476 2
    public function getAdrUnits(string $shipper): array
477
    {
478 2
        $units = $this->client->getAdrUnits($shipper);
479
480 2
        return $units;
481
    }
482
483
    /**
484
     * Returns available activated services for the given shipper
485
     *
486
     * @param string $shipper
487
     *
488
     * @return array
489
     *
490
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
491
     */
492 2
    public function getActivatedServices(string $shipper): array
493
    {
494 2
        $units = $this->client->getActivatedServices($shipper);
495
496 2
        return $units;
497
    }
498
499
    /**
500
     * Order shipments from place B (typically supplier / previous consignee) to place A (shipping point)
501
     *
502
     * @param \Inspirum\Balikobot\Model\Aggregates\PackageCollection $packages
503
     *
504
     * @return \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection|\Inspirum\Balikobot\Model\Values\OrderedPackage[]
505
     *
506
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
507
     */
508 3
    public function orderB2AShipment(PackageCollection $packages): OrderedPackageCollection
509
    {
510 3
        $response = $this->client->orderB2AShipment($packages->getShipper(), $packages->toArray());
511
512
        // create return value object
513 3
        $orderedPackages = new OrderedPackageCollection();
514
515 3
        foreach ($response as $i => $package) {
516 3
            $orderedPackages->add(OrderedPackage::newInstanceFromData(
517 3
                $packages->getShipper(),
518 3
                $packages->getEID(),
519 3
                $package
520
            ));
521
        }
522
523 3
        return $orderedPackages;
524
    }
525
}
526