vendor/bitbag/elasticsearch-plugin/src/Controller/Action/Shop/ListProductsAction.php line 89

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file has been created by developers from BitBag.
  4.  * Feel free to contact us once you face any issues or want to start
  5.  * another great project.
  6.  * You can find more information about us on https://bitbag.shop and write us
  7.  * an email on mikolaj.krol@bitbag.pl.
  8.  */
  9. declare(strict_types=1);
  10. namespace BitBag\SyliusElasticsearchPlugin\Controller\Action\Shop;
  11. use BitBag\SyliusElasticsearchPlugin\Controller\RequestDataHandler\DataHandlerInterface;
  12. use BitBag\SyliusElasticsearchPlugin\Controller\RequestDataHandler\PaginationDataHandlerInterface;
  13. use BitBag\SyliusElasticsearchPlugin\Controller\RequestDataHandler\SortDataHandlerInterface;
  14. use BitBag\SyliusElasticsearchPlugin\Finder\ShopProductsFinderInterface;
  15. use BitBag\SyliusElasticsearchPlugin\Form\Type\ShopProductsFilterType;
  16. use Symfony\Component\Form\FormFactoryInterface;
  17. use Symfony\Component\Form\FormInterface;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\PropertyAccess\PropertyAccess;
  21. use Twig\Environment;
  22. final class ListProductsAction
  23. {
  24.     /** @var FormFactoryInterface */
  25.     private $formFactory;
  26.     /** @var DataHandlerInterface */
  27.     private $shopProductListDataHandler;
  28.     /** @var SortDataHandlerInterface */
  29.     private $shopProductsSortDataHandler;
  30.     /** @var PaginationDataHandlerInterface */
  31.     private $paginationDataHandler;
  32.     /** @var ShopProductsFinderInterface */
  33.     private $shopProductsFinder;
  34.     /** @var Environment */
  35.     private $twig;
  36.     public function __construct(
  37.         FormFactoryInterface $formFactory,
  38.         DataHandlerInterface $shopProductListDataHandler,
  39.         SortDataHandlerInterface $shopProductsSortDataHandler,
  40.         PaginationDataHandlerInterface $paginationDataHandler,
  41.         ShopProductsFinderInterface $shopProductsFinder,
  42.         Environment $twig
  43.     ) {
  44.         $this->formFactory $formFactory;
  45.         $this->shopProductListDataHandler $shopProductListDataHandler;
  46.         $this->shopProductsSortDataHandler $shopProductsSortDataHandler;
  47.         $this->paginationDataHandler $paginationDataHandler;
  48.         $this->shopProductsFinder $shopProductsFinder;
  49.         $this->twig $twig;
  50.     }
  51.     public function __invoke(Request $request): Response
  52.     {
  53.         $form $this->formFactory->create(ShopProductsFilterType::class);
  54.         $form->handleRequest($request);
  55.         $requestData array_merge(
  56.             $form->getData(),
  57.             $request->query->all(),
  58.             ['slug' => $request->get('slug')]
  59.         );
  60.         if (!$form->isValid()) {
  61.             $requestData $this->clearInvalidEntries($form$requestData);
  62.         }
  63.         $data array_merge(
  64.             $this->shopProductListDataHandler->retrieveData($requestData),
  65.             $this->shopProductsSortDataHandler->retrieveData($requestData),
  66.             $this->paginationDataHandler->retrieveData($requestData)
  67.         );
  68.         $template $request->get('template');
  69.         $products $this->shopProductsFinder->find($data);
  70.         return new Response($this->twig->render($template, [
  71.             'form' => $form->createView(),
  72.             'products' => $products,
  73.             'taxon' => $data['taxon'],
  74.         ]));
  75.     }
  76.     private function clearInvalidEntries(FormInterface $form, array $requestData): array
  77.     {
  78.         $propertyAccessor PropertyAccess::createPropertyAccessor();
  79.         foreach ($form->getErrors(truetrue) as $error) {
  80.             $errorOrigin $error->getOrigin();
  81.             $propertyAccessor->setValue(
  82.                 $requestData,
  83.                 ($errorOrigin->getParent()->getPropertyPath() ?? '') . $errorOrigin->getPropertyPath(),
  84.                 ''
  85.             );
  86.         }
  87.         return $requestData;
  88.     }
  89. }