Completed
Push — master ( fcfa74...4ea0bb )
by Paweł
20s
created

CheckoutResolver::getRequestedGraph()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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\Bundle\CoreBundle\Checkout;
13
14
use SM\Factory\FactoryInterface;
15
use Sylius\Component\Core\Model\OrderInterface;
16
use Sylius\Component\Core\OrderCheckoutTransitions;
17
use Sylius\Component\Order\Context\CartContextInterface;
18
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19
use Symfony\Component\HttpFoundation\RedirectResponse;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
22
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
23
use Symfony\Component\HttpKernel\KernelEvents;
24
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
25
26
/**
27
 * @author Arkadiusz Krakowiak <[email protected]>
28
 */
29
final class CheckoutResolver implements EventSubscriberInterface
30
{
31
    /**
32
     * @var CartContextInterface
33
     */
34
    private $cartContext;
35
36
    /**
37
     * @var UrlGeneratorInterface
38
     */
39
    private $urlGenerator;
40
41
    /**
42
     * @var RequestMatcherInterface
43
     */
44
    private $requestMatcher;
45
46
    /**
47
     * @var FactoryInterface
48
     */
49
    private $stateMachineFactory;
50
51
    /**
52
     * @param CartContextInterface $cartContext
53
     * @param UrlGeneratorInterface $urlGenerator
54
     * @param RequestMatcherInterface $requestMatcher
55
     * @param FactoryInterface $stateMachineFactory
56
     */
57
    public function __construct(
58
        CartContextInterface $cartContext,
59
        UrlGeneratorInterface $urlGenerator,
60
        RequestMatcherInterface $requestMatcher,
61
        FactoryInterface $stateMachineFactory
62
    ) {
63
        $this->cartContext = $cartContext;
64
        $this->urlGenerator = $urlGenerator;
65
        $this->requestMatcher = $requestMatcher;
66
        $this->stateMachineFactory = $stateMachineFactory;
67
    }
68
69
    /**
70
     * @param GetResponseEvent $event
71
     */
72
    public function onKernelRequest(GetResponseEvent $event)
73
    {
74
        if (!$event->isMasterRequest()) {
75
            return;
76
        }
77
78
        $request = $event->getRequest();
79
80
        if (!$this->requestMatcher->matches($request)) {
81
            return;
82
        }
83
84
        /** @var OrderInterface $order */
85
        $order = $this->cartContext->getCart();
86
        if ($order->isEmpty()) {
87
            $event->setResponse(new RedirectResponse($this->urlGenerator->generate('sylius_shop_cart_summary')));
88
        }
89
90
        $stateMachine = $this->stateMachineFactory->get($order, $this->getRequestedGraph($request));
91
92
        if ($stateMachine->can($this->getRequestedTransition($request))) {
93
            return;
94
        }
95
96
        if (null !== $referer = $this->getReferer($request)) {
97
            $event->setResponse(new RedirectResponse($referer));
0 ignored issues
show
Bug introduced by
It seems like $referer defined by $this->getReferer($request) on line 96 can also be of type array; however, Symfony\Component\HttpFo...Response::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
98
99
            return;
100
        }
101
102
        $event->setResponse(new RedirectResponse($this->urlGenerator->generateForOrderCheckoutState($order)));
0 ignored issues
show
Bug introduced by
The method generateForOrderCheckoutState() does not exist on Symfony\Component\Routin...r\UrlGeneratorInterface. Did you maybe mean generate()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public static function getSubscribedEvents()
109
    {
110
        return [
111
            KernelEvents::REQUEST => 'onKernelRequest',
112
        ];
113
    }
114
115
    /**
116
     * @param Request $request
117
     *
118
     * @return string
119
     */
120
    private function getRequestedGraph(Request $request)
121
    {
122
        return $request->attributes->get('_sylius')['state_machine']['graph'];
123
    }
124
125
        /**
126
     * @param Request $request
127
     *
128
     * @return string
129
     */
130
    private function getRequestedTransition(Request $request)
131
    {
132
        return $request->attributes->get('_sylius')['state_machine']['transition'];
133
    }
134
135
    /**
136
     * @param Request $request
137
     *
138
     * @return null|string
139
     */
140
    private function getReferer(Request $request)
141
    {
142
        return $request->headers->get('referer');
143
    }
144
}
145