Repository   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 11
eloc 35
c 3
b 0
f 0
dl 0
loc 58
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B verifyShipmentConfig() 0 19 8
A getShipment() 0 32 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ParcelValue\ApiClient\Domain\Shipments;
6
7
use ParcelValue\Api\JsonApi\ResourceObjects\Shipment;
8
use WebServCo\Framework\Exceptions\ApplicationException;
9
10
final class Repository extends \ParcelValue\ApiClient\AbstractRepository
11
{
12
    public function getShipment(): Shipment
13
    {
14
        $shipmentConfig = $this->config()->load(
15
            'Shipment',
16
            \WebServCo\Framework\Environment\Config::string('APP_PATH_PROJECT'),
17
        );
18
        $this->verifyShipmentConfig($shipmentConfig);
19
20
        $shipment = new Shipment();
21
22
        $shipment->setAttribute('shipDate', \date(Shipment::DATE_FORMAT, \strtotime('next tuesday')));
23
24
        $shipment->setAttribute('shipFrom', $shipmentConfig['attributes']['shipFrom']);
25
        $shipment->setAttribute('shipTo', $shipmentConfig['attributes']['shipTo']);
26
        $shipment->setAttribute('packages', $shipmentConfig['attributes']['packages']);
27
        $shipment->setAttribute('goodsDescription', $shipmentConfig['attributes']['goodsDescription']);
28
        $shipment->setAttribute('insuranceDescription', $shipmentConfig['attributes']['insuranceDescription']);
29
        $shipment->setAttribute('insuranceValue', $shipmentConfig['attributes']['insuranceValue']);
30
        $shipment->setAttribute('invoiceSubtotal', $shipmentConfig['attributes']['invoiceSubtotal']);
31
        foreach (['booking'] as $item) {
32
            if (!isset($shipmentConfig['attributes'][$item])) {
33
                continue;
34
            }
35
            $shipment->setAttribute($item, $shipmentConfig['attributes'][$item]);
36
        }
37
        $shipment->setAttribute('customerReference', $shipmentConfig['attributes']['customerReference']);
38
        $shipment->setAttribute('specialInstructions', $shipmentConfig['attributes']['specialInstructions']);
39
        $shipment->setAttribute('confirmationEmail', $shipmentConfig['attributes']['confirmationEmail']);
40
        $shipment->setScheduledProcessing((bool) $shipmentConfig['meta']['scheduledProcessing']);
41
        $shipment->setService($shipmentConfig['meta']['service']);
42
43
        return $shipment;
44
    }
45
46
    /**
47
    * @param array<mixed> $shipmentConfig
48
    */
49
    protected function verifyShipmentConfig(array $shipmentConfig): bool
50
    {
51
        if (empty($shipmentConfig) || !\is_array($shipmentConfig)) {
52
            throw new ApplicationException('Missing or invalid shipment configuration');
53
        }
54
        foreach (['shipFrom', 'shipTo', 'packages'] as $item) {
55
            if (!isset($shipmentConfig['attributes'][$item]) || !\is_array($shipmentConfig['attributes'][$item])) {
56
                throw new ApplicationException(
57
                    \sprintf('Missing or invalid shipment configuration attribute: %s', $item),
58
                );
59
            }
60
        }
61
        if (!isset($shipmentConfig['meta']['scheduledProcessing'])) {
62
            throw new ApplicationException('Missing or invalid shipment configuration meta: scheduledProcessing.');
63
        }
64
        if (!isset($shipmentConfig['meta']['service'])) {
65
            throw new ApplicationException('Missing or invalid shipment configuration meta: service.');
66
        }
67
        return true;
68
    }
69
}
70