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)); |
|
|
|
|
98
|
|
|
|
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$event->setResponse(new RedirectResponse($this->urlGenerator->generateForOrderCheckoutState($order))); |
|
|
|
|
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
|
|
|
|
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.