Completed
Push — master ( 30ce68...607ba7 )
by Radu
05:54 queued 01:08
created

Repository::getShipment()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 0
dl 0
loc 22
rs 9.7998
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 Repository 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('next tuesday')));
17
18
        $shipment->setAttribute('shipFrom', $shipmentConfig['attributes']['shipFrom']);
19
        $shipment->setAttribute('shipTo', $shipmentConfig['attributes']['shipTo']);
20
        $shipment->setAttribute('packages', $shipmentConfig['attributes']['packages']);
21
        $shipment->setAttribute('goodsDescription', $shipmentConfig['attributes']['goodsDescription']);
22
        $shipment->setAttribute('invoiceSubtotal', $shipmentConfig['attributes']['invoiceSubtotal']);
23
        foreach (['useCod', 'saturdayDelivery'] as $item) {
24
            if (isset($shipmentConfig['attributes'][$item])) {
25
                $shipment->setAttribute($item, $shipmentConfig['attributes'][$item]);
26
            }
27
        }
28
        $shipment->setService($shipmentConfig['meta']['service']);
29
30
        return $shipment;
31
    }
32
33
    protected function verifyShipmentConfig($shipmentConfig)
34
    {
35
        if (empty($shipmentConfig) || !is_array($shipmentConfig)) {
36
            throw new ApplicationException('Missing or invalid shipment configuration');
37
        }
38
        foreach (['shipFrom', 'shipTo', 'packages'] as $item) {
39
            if (!isset($shipmentConfig['attributes'][$item]) || !is_array($shipmentConfig['attributes'][$item])) {
40
                throw new ApplicationException(
41
                    sprintf('Missing or invalid shipment configuration attribute: %s', $item)
42
                );
43
            }
44
        }
45
        if (!isset($shipmentConfig['meta']['service'])) {
46
            throw new ApplicationException('Missing or invalid shipment configuration meta: service');
47
        }
48
        return true;
49
    }
50
}
51