Completed
Push — master ( 1714a8...2dab78 )
by Michał
11:58
created

OrderShipmentFactory::createForOrder()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.2
cc 4
eloc 9
nc 6
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\Resource\Factory\FactoryInterface;
16
17
/**
18
 * Shipment factory.
19
 *
20
 * @author Paweł Jędrzejewski <[email protected]>
21
 */
22
class OrderShipmentFactory implements OrderShipmentFactoryInterface
23
{
24
    /**
25
    * Shipment repository.
26
     *
27
     * @var FactoryInterface
28
     */
29
    protected $shipmentFactory;
30
31
    /**
32
     * Constructor.
33
     *
34
     * @param FactoryInterface $shipmentFactory
35
     */
36
    public function __construct(FactoryInterface $shipmentFactory)
37
    {
38
        $this->shipmentFactory = $shipmentFactory;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function createForOrder(OrderInterface $order)
45
    {
46
        if ($order->hasShipments()) {
47
            $shipment = $order->getShipments()->first();
48
        } else {
49
            $shipment = $this->shipmentFactory->createNew();
50
            $order->addShipment($shipment);
51
        }
52
53
        foreach ($order->getInventoryUnits() as $inventoryUnit) {
54
            if (null === $inventoryUnit->getShipment()) {
55
                $shipment->addItem($inventoryUnit);
56
            }
57
        }
58
    }
59
}
60