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

OrderShipmentFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 38
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createForOrder() 0 15 4
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