Passed
Push — master ( e08493...2442d1 )
by Radu
01:22
created

ShipmentsRepository::verifyShipmentConfig()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 9
nc 6
nop 1
dl 0
loc 16
rs 8.8333
c 0
b 0
f 0
1
<?php
2
namespace ParcelValue\ApiClient\Domain\Shipments;
3
4
use ParcelValue\Api\JsonApi\ResourceObjects\Shipment;
5
use WebServCo\Framework\Exceptions\ApplicationException;
6
7
final class ShipmentsRepository extends \ParcelValue\ApiClient\AbstractRepository
8
{
9
    public function getShipment()
10
    {
11
        $shipmentConfig = $this->config()->load('Shipment', $this->config()->get('path/project'));
12
        $this->verifyShipmentConfig($shipmentConfig);
13
14
        $shipment = new Shipment();
15
16
        $shipment->setAttribute('shipDate', date(Shipment::DATE_FORMAT, strtotime('tomorrow')));
17
18
        $shipment->setAttribute('shipFrom', $shipmentConfig['attributes']['shipFrom']);
19
        $shipment->setAttribute('shipTo', $shipmentConfig['attributes']['shipTo']);
20
        $shipment->setAttribute('packages', $shipmentConfig['attributes']['packages']);
21
        foreach (['useCod', 'saturdayDelivery'] as $item) {
22
            if (isset($shipmentConfig['attributes'][$item])) {
23
                $shipment->setAttribute($item, $shipmentConfig['attributes'][$item]);
24
            }
25
        }
26
        $shipment->setService($shipmentConfig['meta']['service']);
27
28
        return $shipment;
29
    }
30
31
    protected function verifyShipmentConfig($shipmentConfig)
32
    {
33
        if (empty($shipmentConfig) || !is_array($shipmentConfig)) {
34
            throw new ApplicationException('Missing or invalid shipment configuration');
35
        }
36
        foreach (['shipFrom', 'shipTo', 'packages'] as $item) {
37
            if (!isset($shipmentConfig['attributes'][$item]) || !is_array($shipmentConfig['attributes'][$item])) {
38
                throw new ApplicationException(
39
                    sprintf('Missing or invalid shipment configuration attribute: %s', $item)
40
                );
41
            }
42
        }
43
        if (!isset($shipmentConfig['meta']['service'])) {
44
            throw new ApplicationException('Missing or invalid shipment configuration meta: service');
45
        }
46
        return true;
47
    }
48
}
49