Completed
Push — currency-cookie ( 8ef560...28b724 )
by Kamil
24:47
created

CheckoutStateResolver::resolve()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 8.8571
cc 5
eloc 6
nc 4
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\StateResolver;
13
14
use SM\Factory\FactoryInterface;
15
use Sylius\Component\Core\Model\OrderItemInterface;
16
use Sylius\Component\Core\OrderCheckoutTransitions;
17
use Sylius\Component\Order\Model\OrderInterface;
18
use Sylius\Component\Order\StateResolver\StateResolverInterface;
19
20
/**
21
 * @author Anna Walasek <[email protected]>
22
 */
23
final class CheckoutStateResolver implements StateResolverInterface
24
{
25
    /**
26
     * @var FactoryInterface
27
     */
28
    private $stateMachineFactory;
29
30
    /**
31
     * @param FactoryInterface $stateMachineFactory
32
     */
33
    public function __construct(FactoryInterface $stateMachineFactory)
34
    {
35
        $this->stateMachineFactory = $stateMachineFactory;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function resolve(OrderInterface $order)
42
    {
43
        $stateMachine = $this->stateMachineFactory->get($order, OrderCheckoutTransitions::GRAPH);
44
45
        if (!$this->isShippingRequired($order) && $stateMachine->can(OrderCheckoutTransitions::TRANSITION_SKIP_SHIPPING)) {
46
            $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SKIP_SHIPPING);
47
        }
48
49
        if (0 === $order->getTotal() && $stateMachine->can(OrderCheckoutTransitions::TRANSITION_SKIP_PAYMENT)) {
50
            $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SKIP_PAYMENT);
51
        }
52
    }
53
54
55
    /**
56
     * @param OrderInterface $order
57
     *
58
     * @return bool
59
     */
60
    private function isShippingRequired(OrderInterface $order)
61
    {
62
        /** @var OrderItemInterface $orderItem */
63
        foreach ($order->getItems() as $orderItem) {
64
            if ($orderItem->getVariant()->isShippingRequired()) {
65
                return true;
66
            }
67
        }
68
69
        return false;
70
    }
71
}
72