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

Balikobot::getActivatedServices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 10
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 1
crap 1
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 44
    public function __construct(RequesterInterface $requester)
33
    {
34 44
        $this->client = new Client($requester);
35 44
    }
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
     * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package
132
     *
133
     * @return \Inspirum\Balikobot\Model\Values\PackageStatus[]
134
     *
135
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
136
     */
137 2
    public function trackPackage(OrderedPackage $package): array
138
    {
139 2
        $response = $this->client->trackPackage($package->getShipper(), $package->getCarrierId());
140
141 2
        $statuses = [];
142
143 2
        foreach ($response as $status) {
144 2
            $statuses[] = PackageStatus::newInstanceFromData($status);
145
        }
146
147 2
        return $statuses;
148
    }
149
150
    /**
151
     * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package
152
     *
153
     * @return \Inspirum\Balikobot\Model\Values\PackageStatus
154
     *
155
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
156
     */
157 2
    public function trackPackageLastStatus(OrderedPackage $package): PackageStatus
158
    {
159 2
        $response = $this->client->trackPackageLastStatus($package->getShipper(), $package->getCarrierId());
160
161 2
        $status = PackageStatus::newInstanceFromData($response);
162
163 2
        return $status;
164
    }
165
166
    /**
167
     * @param string $shipper
168
     *
169
     * @return \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection
170
     *
171
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
172
     */
173 2
    public function getOverview(string $shipper): OrderedPackageCollection
174
    {
175 2
        $response = $this->client->getOverview($shipper);
176
177
        // create return value object
178 2
        $orderedPackages = new OrderedPackageCollection();
179
180 2
        foreach ($response as $i => $package) {
181 1
            $orderedPackages->add(OrderedPackage::newInstanceFromData(
182 1
                $shipper,
183 1
                $package['eshop_id'],
184 1
                $package
185
            ));
186
        }
187
188 2
        return $orderedPackages;
189
    }
190
191
    /**
192
     * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages
193
     *
194
     * @return string
195
     *
196
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
197
     */
198 2
    public function getLabels(OrderedPackageCollection $packages): string
199
    {
200 2
        $response = $this->client->getLabels($packages->getShipper(), $packages->getPackageIds());
201
202 2
        return $response;
203
    }
204
205
    /**
206
     * Gets complete information about a package
207
     *
208
     * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package
209
     *
210
     * @return \Inspirum\Balikobot\Model\Values\Package
211
     *
212
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
213
     */
214 2
    public function getPackageInfo(OrderedPackage $package): Package
215
    {
216 2
        $response = $this->client->getPackageInfo($package->getShipper(), $package->getPackageId());
217
218 2
        $options              = $response;
219 2
        $options[Option::EID] = $package->getBatchId();
220
221 2
        unset($options['package_id']);
222 2
        unset($options['eshop_id']);
223 2
        unset($options['carrier_id']);
224 2
        unset($options['track_url']);
225 2
        unset($options['label_url']);
226 2
        unset($options['carrier_id_swap']);
227 2
        unset($options['pieces']);
228
229 2
        $package = new Package($options);
230
231 2
        return $package;
232
    }
233
234
    /**
235
     * Gets complete information about a package
236
     *
237
     * @param string $shipper
238
     * @param int    $orderId
239
     *
240
     * @return \Inspirum\Balikobot\Model\Values\OrderedShipment
241
     *
242
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
243
     */
244 2
    public function getOrder(string $shipper, int $orderId): OrderedShipment
245
    {
246 2
        $response = $this->client->getOrder($shipper, $orderId);
247
248 2
        $orderedShipment = OrderedShipment::newInstanceFromData(
249 2
            $shipper,
250 2
            $response['package_ids'],
251 2
            $response
252
        );
253
254 2
        return $orderedShipment;
255
    }
256
257
    /**
258
     * Returns available services for the given shipper
259
     *
260
     * @param string $shipper
261
     *
262
     * @return string[]
263
     *
264
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
265
     */
266 4
    public function getServices(string $shipper): array
267
    {
268 4
        $services = $this->client->getServices($shipper);
269
270 3
        return $services;
271
    }
272
273
    /**
274
     * Returns available manipulation units for the given shipper
275
     *
276
     * @param string $shipper
277
     *
278
     * @return string[]
279
     *
280
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
281
     */
282 2
    public function getManipulationUnits(string $shipper): array
283
    {
284 2
        $units = $this->client->getManipulationUnits($shipper);
285
286 2
        return $units;
287
    }
288
289
    /**
290
     * Get all available branches.
291
     *
292
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
293
     *
294
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
295
     */
296 1
    public function getBranches(): iterable
297
    {
298
        // get all shipper service codes
299 1
        $shippers = $this->getShippers();
300
301 1
        foreach ($shippers as $shipper) {
302 1
            yield from $this->getBranchesForShipper($shipper);
303
        }
304 1
    }
305
306
    /**
307
     * Get all available branches for given shipper.
308
     *
309
     * @param string $shipper
310
     *
311
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
312
     *
313
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
314
     */
315 2
    public function getBranchesForShipper(string $shipper): iterable
316
    {
317
        // get all services for shipper service
318 2
        $services = array_keys($this->getServices($shipper));
319
320
        // support shipper withou service type
321 2
        if (empty($services)) {
322 1
            $services = [null];
323
        }
324
325
        // get branches for all services
326 2
        foreach ($services as $service) {
327 2
            yield from $this->getBranchesForShipperService($shipper, $service);
328
        }
329 2
    }
330
331
    /**
332
     * Get all available branches for given shipper and service type.
333
     *
334
     * @param string      $shipper
335
     * @param string|null $service
336
     *
337
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
338
     *
339
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
340
     */
341 2
    public function getBranchesForShipperService(string $shipper, ?string $service): iterable
342
    {
343 2
        $fullData = Shipper::hasFullBranchesSupport($shipper, $service);
344 2
        $branches = $this->client->getBranches($shipper, $service, $fullData);
345
346 2
        foreach ($branches as $branch) {
347 1
            yield Branch::newInstanceFromData($shipper, $service, $branch);
348
        }
349 2
    }
350
351
    /**
352
     * Get all available branches for given shipper.
353
     *
354
     * @param string      $shipper
355
     * @param string      $country
356
     * @param string      $city
357
     * @param string|null $postcode
358
     * @param string|null $street
359
     * @param int|null    $maxResults
360
     * @param float|null  $radius
361
     *
362
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
363
     *
364
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
365
     */
366 2
    public function getBranchesForLocation(
367
        string $shipper,
368
        string $country,
369
        string $city,
370
        string $postcode = null,
371
        string $street = null,
372
        int $maxResults = null,
373
        float $radius = null
374
    ): iterable {
375 2
        $branches = $this->client->getBranchesForLocation(
376 2
            $shipper,
377 2
            $country,
378 2
            $city,
379 2
            $postcode,
380 2
            $street,
381 2
            $maxResults,
382 2
            $radius
383
        );
384
385 2
        foreach ($branches as $branch) {
386 1
            yield Branch::newInstanceFromData($shipper, null, $branch);
387
        }
388 2
    }
389
390
    /**
391
     * Returns list of countries where service with cash-on-delivery payment type is available in
392
     *
393
     * @param string $shipper
394
     *
395
     * @return array[]
396
     *
397
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
398
     */
399 2
    public function getCodCountries(string $shipper): array
400
    {
401 2
        $countries = $this->client->getCodCountries($shipper);
402
403 2
        return $countries;
404
    }
405
406
    /**
407
     * Returns list of countries where service is available in
408
     *
409
     * @param string $shipper
410
     *
411
     * @return array[]
412
     *
413
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
414
     */
415 2
    public function getCountries(string $shipper): array
416
    {
417 2
        $countries = $this->client->getCountries($shipper);
418
419 2
        return $countries;
420
    }
421
422
    /**
423
     * Returns available branches for the given shipper and its service
424
     *
425
     * @param string      $shipper
426
     * @param string      $service
427
     * @param string|null $country
428
     *
429
     * @return \Generator|\Inspirum\Balikobot\Model\Values\PostCode[]
430
     *
431
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
432
     */
433 2
    public function getPostCodes(string $shipper, string $service, string $country = null): iterable
434
    {
435 2
        $postcodes = $this->client->getPostCodes($shipper, $service, $country);
436
437 2
        foreach ($postcodes as $postcode) {
438 1
            yield PostCode::newInstanceFromData($shipper, $service, $postcode);
439
        }
440 2
    }
441
442
    /**
443
     * Check package(s) data.
444
     *
445
     * @param \Inspirum\Balikobot\Model\Aggregates\PackageCollection $packages
446
     *
447
     * @return void
448
     *
449
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
450
     */
451 1
    public function checkPackages(PackageCollection $packages): void
452
    {
453 1
        $this->client->checkPackages($packages->getShipper(), $packages->toArray());
454 1
    }
455
456
    /**
457
     * Returns available manipulation units for the given shipper
458
     *
459
     * @param string $shipper
460
     *
461
     * @return string[]
462
     *
463
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
464
     */
465 2
    public function getAdrUnits(string $shipper): array
466
    {
467 2
        $units = $this->client->getAdrUnits($shipper);
468
469 2
        return $units;
470
    }
471
472
    /**
473
     * Returns available activated services for the given shipper
474
     *
475
     * @param string $shipper
476
     *
477
     * @return array
478
     *
479
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
480
     */
481 2
    public function getActivatedServices(string $shipper): array
482
    {
483 2
        $units = $this->client->getActivatedServices($shipper);
484
485 2
        return $units;
486
    }
487
}
488