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

ShipmentsRepository   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getShipment() 0 20 3
B verifyShipmentConfig() 0 16 7
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