vendor/sylius/sylius/src/Sylius/Bundle/CoreBundle/Controller/ProductTaxonController.php line 27

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\CoreBundle\Controller;
  12. use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration;
  13. use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
  14. use Sylius\Component\Core\Model\ProductTaxonInterface;
  15. use Sylius\Component\Resource\ResourceActions;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpFoundation\Session\Session;
  20. use Symfony\Component\HttpKernel\Exception\HttpException;
  21. use Webmozart\Assert\Assert;
  22. class ProductTaxonController extends ResourceController
  23. {
  24.     /**
  25.      * @throws HttpException
  26.      *
  27.      * @deprecated This ajax action is deprecated and will be removed in Sylius 2.0 - use ProductTaxonController::updateProductTaxonsPositionsAction instead.
  28.      */
  29.     public function updatePositionsAction(Request $request): Response
  30.     {
  31.         $configuration $this->requestConfigurationFactory->create($this->metadata$request);
  32.         $this->isGrantedOr403($configurationResourceActions::UPDATE);
  33.         $productTaxons $request->get('productTaxons');
  34.         $this->validateCsrfProtection($request$configuration);
  35.         if ($this->shouldProductsPositionsBeUpdated($request$productTaxons)) {
  36.             /** @psalm-var array{position: string|int, id: int} $productTaxon */
  37.             foreach ($productTaxons as $productTaxon) {
  38.                 try {
  39.                     $this->updatePositions($productTaxon['position'], $productTaxon['id']);
  40.                 } catch (\InvalidArgumentException $exception) {
  41.                     throw new HttpException(Response::HTTP_BAD_REQUEST$exception->getMessage());
  42.                 }
  43.                 $this->manager->flush();
  44.             }
  45.         }
  46.         return new JsonResponse();
  47.     }
  48.     public function updateProductTaxonsPositionsAction(Request $request): Response
  49.     {
  50.         $configuration $this->requestConfigurationFactory->create($this->metadata$request);
  51.         $this->isGrantedOr403($configurationResourceActions::UPDATE);
  52.         $productTaxons $request->get('productTaxons');
  53.         $this->validateCsrfProtection($request$configuration);
  54.         if ($this->shouldProductsPositionsBeUpdated($request$productTaxons)) {
  55.             /** @var Session $session */
  56.             $session $request->getSession();
  57.             /** @var ProductTaxonInterface $productTaxon */
  58.             foreach ($productTaxons as $id => $position) {
  59.                 try {
  60.                     $this->updatePositions($position$id);
  61.                 } catch (\InvalidArgumentException $exception) {
  62.                     $session->getFlashBag()->add('error'$exception->getMessage());
  63.                     return $this->redirectHandler->redirectToReferer($configuration);
  64.                 }
  65.             }
  66.             $this->manager->flush();
  67.         }
  68.         return $this->redirectHandler->redirectToReferer($configuration);
  69.     }
  70.     private function validateCsrfProtection(Request $requestRequestConfiguration $configuration): void
  71.     {
  72.         if ($configuration->isCsrfProtectionEnabled() && !$this->isCsrfTokenValid('update-product-taxon-position'$request->request->get('_csrf_token'))) {
  73.             throw new HttpException(Response::HTTP_FORBIDDEN'Invalid csrf token.');
  74.         }
  75.     }
  76.     private function shouldProductsPositionsBeUpdated(Request $request, ?array $productTaxons): bool
  77.     {
  78.         return in_array($request->getMethod(), ['POST''PUT''PATCH'], true) && null !== $productTaxons;
  79.     }
  80.     private function updatePositions(string $positionint $id): void
  81.     {
  82.         Assert::numeric($positionsprintf('The position "%s" is invalid.'$position));
  83.         $productTaxonFromBase $this->repository->findOneBy(['id' => $id]);
  84.         $productTaxonFromBase->setPosition((int) $position);
  85.     }
  86. }