Completed
Push — master ( 20a9ba...cdeb22 )
by Paweł
23:39 queued 09:59
created

ShipmentProcessor::updateUnitStates()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 8.8571
cc 5
eloc 7
nc 4
nop 2
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\Shipping\Processor;
13
14
use Doctrine\Common\Collections\Collection;
15
use SM\Factory\FactoryInterface;
16
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
17
use Sylius\Component\Shipping\Model\ShipmentInterface;
18
use Sylius\Component\Shipping\Model\ShipmentUnitInterface;
19
use Sylius\Component\Shipping\ShipmentTransitions;
20
use Sylius\Component\Shipping\ShipmentUnitTransitions;
21
22
/**
23
 * @author Saša Stamenković <[email protected]>
24
 */
25
class ShipmentProcessor implements ShipmentProcessorInterface
26
{
27
    /**
28
     * @var FactoryInterface
29
     */
30
    protected $factory;
31
32
    public function __construct(FactoryInterface $factory)
33
    {
34
        $this->factory = $factory;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function updateShipmentStates($shipments, $transition)
41
    {
42
        if (!is_array($shipments) && !$shipments instanceof Collection) {
43
            throw new \InvalidArgumentException('Shipments value must be array or instance of "Doctrine\Common\Collections\Collection".');
44
        }
45
46
        foreach ($shipments as $shipment) {
47
            if (!$shipment instanceof ShipmentInterface) {
48
                throw new UnexpectedTypeException($shipment, ShipmentInterface::class);
49
            }
50
51
            $this->factory->get($shipment, ShipmentTransitions::GRAPH)->apply($transition, true);
52
        }
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function updateUnitStates($units, $transition)
59
    {
60
        if (!is_array($units) && !$units instanceof Collection) {
61
            throw new \InvalidArgumentException('Shipping units value must be array or instance of "Doctrine\Common\Collections\Collection".');
62
        }
63
64
        foreach ($units as $unit) {
65
            if (!$unit instanceof ShipmentUnitInterface) {
66
                throw new UnexpectedTypeException($unit, 'Sylius\Component\Shipping\Model\ShipmentUnitInterface');
67
            }
68
69
            $this->factory->get($unit, ShipmentUnitTransitions::GRAPH)->apply($transition, true);
70
        }
71
    }
72
}
73