These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | declare(strict_types=1); |
||
13 | |||
14 | namespace Sylius\Bundle\OrderBundle\Controller; |
||
15 | |||
16 | use FOS\RestBundle\View\View; |
||
17 | use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration; |
||
18 | use Sylius\Bundle\ResourceBundle\Controller\ResourceController; |
||
19 | use Sylius\Component\Core\Repository\OrderRepositoryInterface; |
||
20 | use Sylius\Component\Order\Context\CartContextInterface; |
||
21 | use Sylius\Component\Order\Model\OrderInterface; |
||
22 | use Sylius\Component\Order\SyliusCartEvents; |
||
23 | use Sylius\Component\Resource\ResourceActions; |
||
24 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
||
25 | use Symfony\Component\EventDispatcher\GenericEvent; |
||
26 | use Symfony\Component\HttpFoundation\Request; |
||
27 | use Symfony\Component\HttpFoundation\Response; |
||
28 | use Symfony\Component\HttpKernel\Exception\HttpException; |
||
29 | |||
30 | class OrderController extends ResourceController |
||
31 | { |
||
32 | /** |
||
33 | * @param Request $request |
||
34 | * |
||
35 | * @return Response |
||
36 | */ |
||
37 | public function summaryAction(Request $request): Response |
||
38 | { |
||
39 | $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
||
40 | |||
41 | $cart = $this->getCurrentCart(); |
||
42 | if (null !== $cart->getId()) { |
||
43 | $cart = $this->getOrderRepository()->findCartForSummary($cart->getId()); |
||
44 | } |
||
45 | |||
46 | if (!$configuration->isHtmlRequest()) { |
||
47 | return $this->viewHandler->handle($configuration, View::create($cart)); |
||
48 | } |
||
49 | |||
50 | $form = $this->resourceFormFactory->create($configuration, $cart); |
||
0 ignored issues
–
show
|
|||
51 | |||
52 | $view = View::create() |
||
53 | ->setTemplate($configuration->getTemplate('summary.html')) |
||
54 | ->setData([ |
||
55 | 'cart' => $cart, |
||
56 | 'form' => $form->createView(), |
||
57 | ]) |
||
58 | ; |
||
59 | |||
60 | return $this->viewHandler->handle($configuration, $view); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param Request $request |
||
65 | * |
||
66 | * @return Response |
||
67 | */ |
||
68 | public function widgetAction(Request $request): Response |
||
69 | { |
||
70 | $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
||
71 | |||
72 | $cart = $this->getCurrentCart(); |
||
73 | |||
74 | if (!$configuration->isHtmlRequest()) { |
||
75 | return $this->viewHandler->handle($configuration, View::create($cart)); |
||
76 | } |
||
77 | |||
78 | $view = View::create() |
||
79 | ->setTemplate($configuration->getTemplate('summary.html')) |
||
80 | ->setData(['cart' => $cart]) |
||
81 | ; |
||
82 | |||
83 | return $this->viewHandler->handle($configuration, $view); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param Request $request |
||
88 | * |
||
89 | * @return Response |
||
90 | */ |
||
91 | public function saveAction(Request $request): Response |
||
92 | { |
||
93 | $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
||
94 | |||
95 | $this->isGrantedOr403($configuration, ResourceActions::UPDATE); |
||
96 | $resource = $this->getCurrentCart(); |
||
97 | |||
98 | $form = $this->resourceFormFactory->create($configuration, $resource); |
||
99 | |||
100 | if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true) && $form->handleRequest($request)->isValid()) { |
||
101 | $resource = $form->getData(); |
||
102 | |||
103 | $event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource); |
||
104 | |||
105 | if ($event->isStopped() && !$configuration->isHtmlRequest()) { |
||
106 | throw new HttpException($event->getErrorCode(), $event->getMessage()); |
||
107 | } |
||
108 | if ($event->isStopped()) { |
||
109 | $this->flashHelper->addFlashFromEvent($configuration, $event); |
||
110 | |||
111 | return $this->redirectHandler->redirectToResource($configuration, $resource); |
||
112 | } |
||
113 | |||
114 | if ($configuration->hasStateMachine()) { |
||
115 | $this->stateMachine->apply($configuration, $resource); |
||
116 | } |
||
117 | |||
118 | $this->eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource); |
||
119 | |||
120 | if (!$configuration->isHtmlRequest()) { |
||
121 | return $this->viewHandler->handle($configuration, View::create(null, Response::HTTP_NO_CONTENT)); |
||
122 | } |
||
123 | |||
124 | $this->getEventDispatcher()->dispatch(SyliusCartEvents::CART_CHANGE, new GenericEvent($resource)); |
||
125 | $this->manager->flush(); |
||
126 | |||
127 | $this->flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource); |
||
128 | |||
129 | return $this->redirectHandler->redirectToResource($configuration, $resource); |
||
130 | } |
||
131 | |||
132 | if (!$configuration->isHtmlRequest()) { |
||
133 | return $this->viewHandler->handle($configuration, View::create($form, Response::HTTP_BAD_REQUEST)); |
||
134 | } |
||
135 | |||
136 | $view = View::create() |
||
137 | ->setData([ |
||
138 | 'configuration' => $configuration, |
||
139 | $this->metadata->getName() => $resource, |
||
140 | 'form' => $form->createView(), |
||
141 | 'cart' => $resource, |
||
142 | ]) |
||
143 | ->setTemplate($configuration->getTemplate(ResourceActions::UPDATE . '.html')) |
||
144 | ; |
||
145 | |||
146 | return $this->viewHandler->handle($configuration, $view); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @param Request $request |
||
151 | * |
||
152 | * @return Response |
||
153 | */ |
||
154 | public function clearAction(Request $request): Response |
||
155 | { |
||
156 | $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
||
157 | |||
158 | $this->isGrantedOr403($configuration, ResourceActions::DELETE); |
||
159 | $resource = $this->getCurrentCart(); |
||
160 | |||
161 | if ($configuration->isCsrfProtectionEnabled() && !$this->isCsrfTokenValid($resource->getId(), $request->get('_csrf_token'))) { |
||
162 | throw new HttpException(Response::HTTP_FORBIDDEN, 'Invalid csrf token.'); |
||
163 | } |
||
164 | |||
165 | $event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource); |
||
166 | |||
167 | if ($event->isStopped() && !$configuration->isHtmlRequest()) { |
||
168 | throw new HttpException($event->getErrorCode(), $event->getMessage()); |
||
169 | } |
||
170 | if ($event->isStopped()) { |
||
171 | $this->flashHelper->addFlashFromEvent($configuration, $event); |
||
172 | |||
173 | return $this->redirectHandler->redirectToIndex($configuration, $resource); |
||
174 | } |
||
175 | |||
176 | $this->repository->remove($resource); |
||
177 | $this->eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource); |
||
178 | |||
179 | if (!$configuration->isHtmlRequest()) { |
||
180 | return $this->viewHandler->handle($configuration, View::create(null, Response::HTTP_NO_CONTENT)); |
||
181 | } |
||
182 | |||
183 | $this->flashHelper->addSuccessFlash($configuration, ResourceActions::DELETE, $resource); |
||
184 | |||
185 | return $this->redirectHandler->redirectToIndex($configuration, $resource); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @param RequestConfiguration $configuration |
||
190 | * |
||
191 | * @return Response |
||
192 | */ |
||
193 | protected function redirectToCartSummary(RequestConfiguration $configuration): Response |
||
194 | { |
||
195 | if (null === $configuration->getParameters()->get('redirect')) { |
||
196 | return $this->redirectHandler->redirectToRoute($configuration, $this->getCartSummaryRoute()); |
||
197 | } |
||
198 | |||
199 | return $this->redirectHandler->redirectToRoute($configuration, $configuration->getParameters()->get('redirect')); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @return string |
||
204 | */ |
||
205 | protected function getCartSummaryRoute(): string |
||
206 | { |
||
207 | return 'sylius_cart_summary'; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @return OrderInterface |
||
212 | */ |
||
213 | protected function getCurrentCart(): OrderInterface |
||
214 | { |
||
215 | return $this->getContext()->getCart(); |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @return CartContextInterface |
||
220 | */ |
||
221 | protected function getContext(): CartContextInterface |
||
222 | { |
||
223 | return $this->get('sylius.context.cart'); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @return OrderRepositoryInterface |
||
228 | */ |
||
229 | protected function getOrderRepository(): OrderRepositoryInterface |
||
230 | { |
||
231 | return $this->get('sylius.repository.order'); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @return EventDispatcherInterface |
||
236 | */ |
||
237 | protected function getEventDispatcher(): EventDispatcherInterface |
||
238 | { |
||
239 | return $this->container->get('event_dispatcher'); |
||
240 | } |
||
241 | } |
||
242 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: