src/Controller/Product/PostProductSearchFormAction.php line 29

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Product;
  4. use App\DataProvider\Product\ProductCollectionDataProvider;
  5. use App\DataProvider\RequestDataProviderTrait;
  6. use App\Model\Product\Product;
  7. use App\Service\Cache\Exception\UnableToSaveKeyException;
  8. use App\Service\Form\FormUtils;
  9. use App\Service\Product\ProductFormHandler;
  10. use App\Voters\ProductVoter;
  11. use Psr\Cache\CacheException;
  12. use Psr\Cache\InvalidArgumentException;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Serializer\Exception\ExceptionInterface;
  18. use Symfony\Component\Serializer\Serializer;
  19. use Symfony\Component\Serializer\SerializerInterface;
  20. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  21. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  22. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  23. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  24. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  25. class PostProductSearchFormAction extends AbstractController
  26. {
  27.     use FormUtils;
  28.     use RequestDataProviderTrait;
  29.     /**
  30.      * @var ProductFormHandler
  31.      */
  32.     private $productFormHandler;
  33.     /**
  34.      * @var ProductCollectionDataProvider
  35.      */
  36.     private $productCollectionDataProvider;
  37.     /**
  38.      * @var Serializer
  39.      */
  40.     private $serializer;
  41.     /**
  42.      * @param ProductFormHandler            $productFormHandler
  43.      * @param ProductCollectionDataProvider $productCollectionDataProvider
  44.      * @param Serializer                    $serializer
  45.      */
  46.     public function __construct(
  47.         ProductFormHandler $productFormHandler,
  48.         ProductCollectionDataProvider $productCollectionDataProvider,
  49.         SerializerInterface $serializer
  50.     ) {
  51.         $this->productFormHandler $productFormHandler;
  52.         $this->productCollectionDataProvider $productCollectionDataProvider;
  53.         $this->serializer $serializer;
  54.     }
  55.     /**
  56.      * @param Request $request
  57.      *
  58.      * @return JsonResponse|iterable
  59.      *
  60.      * @throws ClientExceptionInterface
  61.      * @throws DecodingExceptionInterface
  62.      * @throws RedirectionExceptionInterface
  63.      * @throws ServerExceptionInterface
  64.      * @throws TransportExceptionInterface
  65.      * @throws UnableToSaveKeyException
  66.      * @throws CacheException
  67.      * @throws InvalidArgumentException
  68.      * @throws ExceptionInterface
  69.      */
  70.     public function __invoke(Request $request)
  71.     {
  72.         $this->denyAccessUnlessGranted(ProductVoter::PRODUCT_SHOW_LIST);
  73.         $productSearchForm $this->productFormHandler->handleProductSearchForm($request->getContent());
  74.         if (!$productSearchForm->isSubmitted() || !$productSearchForm->isValid()) {
  75.             return new JsonResponse([
  76.                 'code' => Response::HTTP_BAD_REQUEST,
  77.                 'data' => $this->convertFormErrorsToArray($productSearchForm),
  78.                 'errorMessage' => 'product_form_search',
  79.             ], Response::HTTP_BAD_REQUEST);
  80.         }
  81.         $searchBody $this->completeSearchBodyFromRequest(
  82.             $request,
  83.             json_decode($this->serializer->serialize($productSearchForm->getData(), 'json'), true)
  84.         );
  85.         $transformResponse = !$request->query->get('disableTransformation'false);
  86.         $response $this
  87.             ->productCollectionDataProvider
  88.             ->getCollection(Product::class, null, [
  89.                 'filters' => $searchBody,
  90.                 'transformResponse' => $transformResponse,
  91.             ])
  92.         ;
  93.         if ($transformResponse) {
  94.             return new JsonResponse($response);
  95.         }
  96.         return $response;
  97.     }
  98. }