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 | namespace Sylius\Bundle\ResourceBundle\Controller; |
||
13 | |||
14 | use Doctrine\Common\Persistence\ObjectManager; |
||
15 | use FOS\RestBundle\View\View; |
||
16 | use Sylius\Component\Resource\Factory\FactoryInterface; |
||
17 | use Sylius\Component\Resource\Metadata\MetadataInterface; |
||
18 | use Sylius\Component\Resource\Repository\RepositoryInterface; |
||
19 | use Sylius\Component\Resource\ResourceActions; |
||
20 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
||
21 | use Symfony\Component\HttpFoundation\RedirectResponse; |
||
22 | use Symfony\Component\HttpFoundation\Request; |
||
23 | use Symfony\Component\HttpFoundation\Response; |
||
24 | use Symfony\Component\HttpKernel\Exception\HttpException; |
||
25 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
26 | use Symfony\Component\PropertyAccess\PropertyAccess; |
||
27 | use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
||
28 | |||
29 | /** |
||
30 | * @author Paweł Jędrzejewski <[email protected]> |
||
31 | * @author Saša Stamenković <[email protected]> |
||
32 | */ |
||
33 | class ResourceController extends Controller |
||
34 | { |
||
35 | /** |
||
36 | * @var MetadataInterface |
||
37 | */ |
||
38 | protected $metadata; |
||
39 | |||
40 | /** |
||
41 | * @var RequestConfigurationFactoryInterface |
||
42 | */ |
||
43 | protected $requestConfigurationFactory; |
||
44 | |||
45 | /** |
||
46 | * @var ViewHandlerInterface |
||
47 | */ |
||
48 | protected $viewHandler; |
||
49 | |||
50 | /** |
||
51 | * @var RepositoryInterface |
||
52 | */ |
||
53 | protected $repository; |
||
54 | |||
55 | /** |
||
56 | * @var FactoryInterface |
||
57 | */ |
||
58 | protected $factory; |
||
59 | |||
60 | /** |
||
61 | * @var NewResourceFactoryInterface |
||
62 | */ |
||
63 | protected $newResourceFactory; |
||
64 | |||
65 | /** |
||
66 | * @var ObjectManager |
||
67 | */ |
||
68 | protected $manager; |
||
69 | |||
70 | /** |
||
71 | * @var SingleResourceProviderInterface |
||
72 | */ |
||
73 | protected $singleResourceProvider; |
||
74 | |||
75 | /** |
||
76 | * @var ResourcesCollectionProviderInterface |
||
77 | */ |
||
78 | protected $resourcesCollectionProvider; |
||
79 | |||
80 | /** |
||
81 | * @var ResourceFormFactoryInterface |
||
82 | */ |
||
83 | protected $resourceFormFactory; |
||
84 | |||
85 | /** |
||
86 | * @var RedirectHandlerInterface |
||
87 | */ |
||
88 | protected $redirectHandler; |
||
89 | |||
90 | /** |
||
91 | * @var FlashHelperInterface |
||
92 | */ |
||
93 | protected $flashHelper; |
||
94 | |||
95 | /** |
||
96 | * @var AuthorizationCheckerInterface |
||
97 | */ |
||
98 | protected $authorizationChecker; |
||
99 | |||
100 | /** |
||
101 | * @var EventDispatcherInterface |
||
102 | */ |
||
103 | protected $eventDispatcher; |
||
104 | |||
105 | /** |
||
106 | * @param MetadataInterface $metadata |
||
107 | * @param RequestConfigurationFactoryInterface $requestConfigurationFactory |
||
108 | * @param ViewHandlerInterface $viewHandler |
||
109 | * @param RepositoryInterface $repository |
||
110 | * @param FactoryInterface $factory |
||
111 | * @param NewResourceFactoryInterface $newResourceFactory |
||
112 | * @param ObjectManager $manager |
||
113 | * @param SingleResourceProviderInterface $singleResourceProvider |
||
114 | * @param ResourcesCollectionProviderInterface $resourcesFinder |
||
115 | * @param ResourceFormFactoryInterface $resourceFormFactory |
||
116 | * @param RedirectHandlerInterface $redirectHandler |
||
117 | * @param FlashHelperInterface $flashHelper |
||
118 | * @param AuthorizationCheckerInterface $authorizationChecker |
||
119 | * @param EventDispatcherInterface $eventDispatcher |
||
120 | */ |
||
121 | public function __construct( |
||
122 | MetadataInterface $metadata, |
||
123 | RequestConfigurationFactoryInterface $requestConfigurationFactory, |
||
124 | ViewHandlerInterface $viewHandler, |
||
125 | RepositoryInterface $repository, |
||
126 | FactoryInterface $factory, |
||
127 | NewResourceFactoryInterface $newResourceFactory, |
||
128 | ObjectManager $manager, |
||
129 | SingleResourceProviderInterface $singleResourceProvider, |
||
130 | ResourcesCollectionProviderInterface $resourcesFinder, |
||
131 | ResourceFormFactoryInterface $resourceFormFactory, |
||
132 | RedirectHandlerInterface $redirectHandler, |
||
133 | FlashHelperInterface $flashHelper, |
||
134 | AuthorizationCheckerInterface $authorizationChecker, |
||
135 | EventDispatcherInterface $eventDispatcher |
||
136 | ) { |
||
137 | $this->metadata = $metadata; |
||
138 | $this->requestConfigurationFactory = $requestConfigurationFactory; |
||
139 | $this->viewHandler = $viewHandler; |
||
140 | $this->repository = $repository; |
||
141 | $this->factory = $factory; |
||
142 | $this->newResourceFactory = $newResourceFactory; |
||
143 | $this->manager = $manager; |
||
144 | $this->singleResourceProvider = $singleResourceProvider; |
||
145 | $this->resourcesCollectionProvider = $resourcesFinder; |
||
146 | $this->resourceFormFactory = $resourceFormFactory; |
||
147 | $this->redirectHandler = $redirectHandler; |
||
148 | $this->flashHelper = $flashHelper; |
||
149 | $this->authorizationChecker = $authorizationChecker; |
||
150 | $this->eventDispatcher = $eventDispatcher; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param Request $request |
||
155 | * |
||
156 | * @return Response |
||
157 | */ |
||
158 | public function showAction(Request $request) |
||
159 | { |
||
160 | $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
||
161 | |||
162 | $this->isGrantedOr403($configuration, ResourceActions::SHOW); |
||
163 | $resource = $this->findOr404($configuration); |
||
164 | |||
165 | $this->eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $resource); |
||
166 | |||
167 | $view = View::create($resource); |
||
168 | |||
169 | if ($configuration->isHtmlRequest()) { |
||
170 | $view |
||
171 | ->setTemplate($configuration->getTemplate(ResourceActions::SHOW . '.html')) |
||
172 | ->setTemplateVar($this->metadata->getName()) |
||
173 | ->setData([ |
||
174 | 'configuration' => $configuration, |
||
175 | 'metadata' => $this->metadata, |
||
176 | 'resource' => $resource, |
||
177 | $this->metadata->getName() => $resource, |
||
178 | ]) |
||
179 | ; |
||
180 | } |
||
181 | |||
182 | return $this->viewHandler->handle($configuration, $view); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @param Request $request |
||
187 | * |
||
188 | * @return \Symfony\Component\HttpFoundation\Response |
||
189 | */ |
||
190 | public function indexAction(Request $request) |
||
191 | { |
||
192 | $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
||
193 | |||
194 | $this->isGrantedOr403($configuration, ResourceActions::INDEX); |
||
195 | $resources = $this->resourcesCollectionProvider->get($configuration, $this->repository); |
||
196 | |||
197 | $view = View::create($resources); |
||
198 | |||
199 | if ($configuration->isHtmlRequest()) { |
||
200 | $view |
||
201 | ->setTemplate($configuration->getTemplate(ResourceActions::INDEX . '.html')) |
||
202 | ->setTemplateVar($this->metadata->getPluralName()) |
||
203 | ->setData([ |
||
204 | 'configuration' => $configuration, |
||
205 | 'metadata' => $this->metadata, |
||
206 | 'resources' => $resources, |
||
207 | $this->metadata->getPluralName() => $resources, |
||
208 | ]) |
||
209 | ; |
||
210 | } |
||
211 | |||
212 | return $this->viewHandler->handle($configuration, $view); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * @param Request $request |
||
217 | * |
||
218 | * @return Response |
||
219 | */ |
||
220 | public function createAction(Request $request) |
||
221 | { |
||
222 | $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
||
223 | |||
224 | $this->isGrantedOr403($configuration, ResourceActions::CREATE); |
||
225 | $newResource = $this->newResourceFactory->create($configuration, $this->factory); |
||
226 | |||
227 | $form = $this->resourceFormFactory->create($configuration, $newResource); |
||
228 | |||
229 | if ($request->isMethod('POST') && $form->submit($request)->isValid()) { |
||
230 | $newResource = $form->getData(); |
||
231 | |||
232 | $event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource); |
||
233 | |||
234 | if ($event->isStopped() && !$configuration->isHtmlRequest()) { |
||
235 | throw new HttpException($event->getErrorCode(), $event->getMessage()); |
||
236 | } |
||
237 | if ($event->isStopped()) { |
||
238 | $this->flashHelper->addFlashFromEvent($configuration, $event); |
||
239 | |||
240 | return $this->redirectHandler->redirectToIndex($configuration, $newResource); |
||
241 | } |
||
242 | |||
243 | $this->repository->add($newResource); |
||
244 | $this->eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource); |
||
245 | |||
246 | if (!$configuration->isHtmlRequest()) { |
||
247 | return $this->viewHandler->handle($configuration, View::create($newResource, 201)); |
||
248 | } |
||
249 | |||
250 | $this->flashHelper->addSuccessFlash($configuration, ResourceActions::CREATE, $newResource); |
||
251 | |||
252 | return $this->redirectHandler->redirectToResource($configuration, $newResource); |
||
253 | } |
||
254 | |||
255 | if (!$configuration->isHtmlRequest()) { |
||
256 | return $this->viewHandler->handle($configuration, View::create($form, 400)); |
||
257 | } |
||
258 | |||
259 | $view = View::create() |
||
260 | ->setData([ |
||
261 | 'configuration' => $configuration, |
||
262 | 'metadata' => $this->metadata, |
||
263 | 'resource' => $newResource, |
||
264 | $this->metadata->getName() => $newResource, |
||
265 | 'form' => $form->createView(), |
||
266 | ]) |
||
267 | ->setTemplate($configuration->getTemplate(ResourceActions::CREATE . '.html')) |
||
268 | ; |
||
269 | |||
270 | return $this->viewHandler->handle($configuration, $view); |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @param Request $request |
||
275 | * |
||
276 | * @return Response |
||
277 | */ |
||
278 | public function updateAction(Request $request) |
||
279 | { |
||
280 | $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
||
281 | |||
282 | $this->isGrantedOr403($configuration, ResourceActions::UPDATE); |
||
283 | $resource = $this->findOr404($configuration); |
||
284 | |||
285 | $form = $this->resourceFormFactory->create($configuration, $resource); |
||
286 | |||
287 | if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH']) && $form->submit($request, !$request->isMethod('PATCH'))->isValid()) { |
||
288 | $resource = $form->getData(); |
||
289 | |||
290 | $event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource); |
||
291 | |||
292 | if ($event->isStopped() && !$configuration->isHtmlRequest()) { |
||
293 | throw new HttpException($event->getErrorCode(), $event->getMessage()); |
||
294 | } |
||
295 | if ($event->isStopped()) { |
||
296 | $this->flashHelper->addFlashFromEvent($configuration, $event); |
||
297 | |||
298 | return $this->redirectHandler->redirectToResource($configuration, $resource); |
||
299 | } |
||
300 | |||
301 | $this->manager->flush(); |
||
302 | $this->eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource); |
||
303 | |||
304 | if (!$configuration->isHtmlRequest()) { |
||
305 | return $this->viewHandler->handle($configuration, View::create($resource, 204)); |
||
306 | } |
||
307 | |||
308 | $this->flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource); |
||
309 | |||
310 | return $this->redirectHandler->redirectToResource($configuration, $resource); |
||
311 | } |
||
312 | |||
313 | if (!$configuration->isHtmlRequest()) { |
||
314 | return $this->viewHandler->handle($configuration, View::create($form, 400)); |
||
315 | } |
||
316 | |||
317 | $view = View::create() |
||
318 | ->setData([ |
||
319 | 'configuration' => $configuration, |
||
320 | 'metadata' => $this->metadata, |
||
321 | 'resource' => $resource, |
||
322 | $this->metadata->getName() => $resource, |
||
323 | 'form' => $form->createView(), |
||
324 | ]) |
||
325 | ->setTemplate($configuration->getTemplate(ResourceActions::UPDATE . '.html')) |
||
326 | ; |
||
327 | |||
328 | return $this->viewHandler->handle($configuration, $view); |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * @param Request $request |
||
333 | * |
||
334 | * @return Response |
||
335 | */ |
||
336 | public function deleteAction(Request $request) |
||
337 | { |
||
338 | $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
||
339 | |||
340 | $this->isGrantedOr403($configuration, ResourceActions::DELETE); |
||
341 | $resource = $this->findOr404($configuration); |
||
342 | |||
343 | $event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource); |
||
344 | |||
345 | if ($event->isStopped() && !$configuration->isHtmlRequest()) { |
||
346 | throw new HttpException($event->getErrorCode(), $event->getMessage()); |
||
347 | } |
||
348 | if ($event->isStopped()) { |
||
349 | $this->flashHelper->addFlashFromEvent($configuration, $event); |
||
350 | |||
351 | return $this->redirectHandler->redirectToIndex($configuration, $resource); |
||
352 | } |
||
353 | |||
354 | $this->repository->remove($resource); |
||
355 | $this->eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource); |
||
356 | |||
357 | if (!$configuration->isHtmlRequest()) { |
||
358 | return $this->viewHandler->handle($configuration, View::create(null, 204)); |
||
359 | } |
||
360 | |||
361 | $this->flashHelper->addSuccessFlash($configuration, ResourceActions::DELETE, $resource); |
||
362 | |||
363 | return $this->redirectHandler->redirectToIndex($configuration, $resource); |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * @param Request $request |
||
368 | * |
||
369 | * @return RedirectResponse |
||
370 | */ |
||
371 | public function enableAction(Request $request) |
||
372 | { |
||
373 | return $this->toggle($request, true); |
||
374 | } |
||
375 | /** |
||
376 | * @param Request $request |
||
377 | * |
||
378 | * @return RedirectResponse |
||
379 | */ |
||
380 | public function disableAction(Request $request) |
||
381 | { |
||
382 | return $this->toggle($request, false); |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * @param Request $request |
||
387 | * @param $enabled |
||
388 | * |
||
389 | * @return RedirectResponse |
||
390 | */ |
||
391 | protected function toggle(Request $request, $enabled) |
||
392 | { |
||
393 | $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
||
394 | |||
395 | $this->isGrantedOr403($configuration, ResourceActions::UPDATE); |
||
396 | |||
397 | $resource = $this->findOr404($configuration); |
||
398 | $resource->setEnabled($enabled); |
||
399 | |||
400 | $this->eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource); |
||
401 | $this->manager->flush(); |
||
402 | $this->eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource); |
||
403 | |||
404 | if (!$configuration->isHtmlRequest()) { |
||
405 | return $this->viewHandler->handle($configuration, View::create($resource, 204)); |
||
406 | } |
||
407 | |||
408 | $this->flashHelper->addSuccessFlash($configuration, $enabled ? 'enable' : 'disable', $resource); |
||
409 | |||
410 | return $this->redirectHandler->redirectToIndex($configuration, $resource); |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * @param Request $request |
||
415 | * |
||
416 | * @return Response |
||
417 | */ |
||
418 | public function moveUpAction(Request $request) |
||
419 | { |
||
420 | return $this->move($request, 1); |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * @param Request $request |
||
425 | * |
||
426 | * @return Response |
||
427 | */ |
||
428 | public function moveDownAction(Request $request) |
||
429 | { |
||
430 | return $this->move($request, -1); |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * @param Request $request |
||
435 | * @param int $movement |
||
436 | * |
||
437 | * @return RedirectResponse |
||
438 | */ |
||
439 | protected function move(Request $request, $movement) |
||
440 | { |
||
441 | $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
||
442 | $resource = $this->findOr404($configuration); |
||
443 | |||
444 | $position = $configuration->getSortablePosition(); |
||
445 | $accessor = PropertyAccess::createPropertyAccessor(); |
||
446 | $accessor->setValue( |
||
447 | $resource, |
||
448 | $position, |
||
449 | $accessor->getValue($resource, $position) + $movement |
||
450 | ); |
||
451 | |||
452 | $this->eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource); |
||
453 | $this->manager->flush(); |
||
454 | $this->eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource); |
||
455 | |||
456 | if (!$configuration->isHtmlRequest()) { |
||
457 | return $this->viewHandler->handle($configuration, View::create($resource, 204)); |
||
458 | } |
||
459 | |||
460 | $this->flashHelper->addSuccessFlash($configuration, 'move', $resource); |
||
461 | |||
462 | return $this->redirectHandler->redirectToIndex($configuration, $resource); |
||
0 ignored issues
–
show
|
|||
463 | } |
||
464 | |||
465 | /** |
||
466 | * @param Request $request |
||
467 | * @param string $transition |
||
468 | * @param string $graph |
||
469 | * |
||
470 | * @return RedirectResponse |
||
471 | */ |
||
472 | public function updateStateAction(Request $request, $transition, $graph) |
||
473 | { |
||
474 | $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
||
475 | $resource = $this->findOr404($configuration); |
||
476 | |||
477 | $stateMachine = $this->get('sm.factory')->get($resource, $graph); |
||
478 | if (!$stateMachine->can($transition)) { |
||
479 | throw new NotFoundHttpException(sprintf( |
||
480 | 'The requested transition %s cannot be applied on the given %s with graph %s.', |
||
481 | $transition, |
||
482 | $this->metadata->getName(), |
||
483 | $graph |
||
484 | )); |
||
485 | } |
||
486 | |||
487 | $stateMachine->apply($transition); |
||
488 | |||
489 | $this->manager->flush(); |
||
490 | |||
491 | if (!$configuration->isHtmlRequest()) { |
||
492 | return $this->viewHandler->handle($configuration, View::create($resource, 204)); |
||
493 | } |
||
494 | |||
495 | $this->flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource); |
||
496 | |||
497 | return $this->redirectHandler->redirectToReferer($configuration); |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * @param RequestConfiguration $configuration |
||
502 | * @param string $permission |
||
503 | * |
||
504 | * @throws AccessDeniedException |
||
505 | */ |
||
506 | protected function isGrantedOr403(RequestConfiguration $configuration, $permission) |
||
507 | { |
||
508 | if (!$configuration->hasPermission()) { |
||
509 | return; |
||
510 | } |
||
511 | |||
512 | $permission = $configuration->getPermission($permission); |
||
513 | |||
514 | if (!$this->authorizationChecker->isGranted($configuration, $permission)) { |
||
515 | throw new AccessDeniedException(); |
||
516 | } |
||
517 | } |
||
518 | |||
519 | /** |
||
520 | * @param RequestConfiguration $configuration |
||
521 | * |
||
522 | * @return \Sylius\Component\Resource\Model\ResourceInterface |
||
523 | * |
||
524 | * @throws NotFoundHttpException |
||
525 | */ |
||
526 | protected function findOr404(RequestConfiguration $configuration) |
||
527 | { |
||
528 | if (null === $resource = $this->singleResourceProvider->get($configuration, $this->repository)) { |
||
529 | throw new NotFoundHttpException(); |
||
530 | } |
||
531 | |||
532 | return $resource; |
||
533 | } |
||
534 | } |
||
535 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: