Completed
Push — master ( 7c7881...78efce )
by Michał
26:17 queued 05:58
created

OrderShipmentProcessor::process()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 10
nc 5
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Core\OrderProcessing;
13
14
use Sylius\Component\Core\Model\OrderInterface;
15
use Sylius\Component\Core\Model\ShipmentInterface;
16
use Sylius\Component\Resource\Factory\FactoryInterface;
17
use Sylius\Component\Shipping\Exception\UnresolvedDefaultShippingMethodException;
18
use Sylius\Component\Shipping\Resolver\DefaultShippingMethodResolverInterface;
19
20
/**
21
 * @author Paweł Jędrzejewski <[email protected]>
22
 */
23
final class OrderShipmentProcessor implements OrderProcessorInterface
24
{
25
    /**
26
     * @var DefaultShippingMethodResolverInterface
27
     */
28
    private $defaultShippingMethodResolver;
29
30
    /**
31
     * @var FactoryInterface
32
     */
33
    private $shipmentFactory;
34
35
    /**
36
     * @param DefaultShippingMethodResolverInterface $defaultShippingMethodResolver
37
     * @param FactoryInterface $shipmentFactory
38
     */
39
    public function __construct(
40
        DefaultShippingMethodResolverInterface $defaultShippingMethodResolver,
41
        FactoryInterface $shipmentFactory
42
    ) {
43
        $this->defaultShippingMethodResolver = $defaultShippingMethodResolver;
44
        $this->shipmentFactory = $shipmentFactory;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function process(OrderInterface $order)
51
    {
52
        if ($order->isEmpty()) {
53
            $order->removeShipments();
54
55
            return;
56
        }
57
58
        $shipment = $this->getOrderShipment($order);
59
60
        if (null === $shipment) {
61
            return;
62
        }
63
64
        foreach ($order->getItemUnits() as $itemUnit) {
65
            if (null === $itemUnit->getShipment()) {
66
                $shipment->addUnit($itemUnit);
67
            }
68
        }
69
    }
70
71
    /**
72
     * @param OrderInterface $order
73
     *
74
     * @return ShipmentInterface
75
     */
76
    private function getOrderShipment(OrderInterface $order)
77
    {
78
        if ($order->hasShipments()) {
79
            return $order->getShipments()->first();
80
        }
81
82
        try {
83
            /** @var ShipmentInterface $shipment */
84
            $shipment = $this->shipmentFactory->createNew();
85
            $shipment->setOrder($order);
86
            $shipment->setMethod($this->defaultShippingMethodResolver->getDefaultShippingMethod($shipment));
87
88
            $order->addShipment($shipment);
89
90
            return $shipment;
91
        } catch (UnresolvedDefaultShippingMethodException $exception) {
92
            return null;
93
        }
94
    }
95
}
96