src/Component/Elasticsearch/Controller/ListProductsAction.php line 21

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace BitBag\OpenMarketplace\Component\Elasticsearch\Controller;
  4. use BitBag\OpenMarketplace\Component\Elasticsearch\Provider\RedirectResponseProviderInterface;
  5. use BitBag\OpenMarketplace\Component\Taxonomy\Entity\TaxonInterface;
  6. use BitBag\SyliusElasticsearchPlugin\Controller\RequestDataHandler\DataHandlerInterface;
  7. use BitBag\SyliusElasticsearchPlugin\Controller\RequestDataHandler\PaginationDataHandlerInterface;
  8. use BitBag\SyliusElasticsearchPlugin\Controller\RequestDataHandler\SortDataHandlerInterface;
  9. use BitBag\SyliusElasticsearchPlugin\Finder\ShopProductsFinderInterface;
  10. use BitBag\SyliusElasticsearchPlugin\Form\Type\ShopProductsFilterType;
  11. use Symfony\Component\Form\FormError;
  12. use Symfony\Component\Form\FormFactoryInterface;
  13. use Symfony\Component\Form\FormInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Twig\Environment;
  17. final class ListProductsAction
  18. {
  19.     private static iterable $currentPageProducts;
  20.     private static TaxonInterface $taxon;
  21.     public function __construct(
  22.         private FormFactoryInterface $formFactory,
  23.         private DataHandlerInterface $shopProductListDataHandler,
  24.         private SortDataHandlerInterface $shopProductsSortDataHandler,
  25.         private PaginationDataHandlerInterface $paginationDataHandler,
  26.         private ShopProductsFinderInterface $shopProductsFinder,
  27.         private Environment $twig,
  28.         private RedirectResponseProviderInterface $redirectResponseProvider
  29.     ) {
  30.     }
  31.     public function __invoke(Request $request): Response
  32.     {
  33.         $redirectResponse $this->redirectResponseProvider->provide($request, [
  34.             'slug' => $request->get('slug'),
  35.         ]);
  36.         if (null !== $redirectResponse) {
  37.             return $redirectResponse;
  38.         }
  39.         $form $this->formFactory->create(ShopProductsFilterType::class);
  40.         $form->handleRequest($request);
  41.         $requestData array_merge(
  42.             $form->getData(),
  43.             $request->query->all(),
  44.             ['slug' => $request->get('slug')]
  45.         );
  46.         if (!$form->isValid()) {
  47.             $requestData $this->clearInvalidEntries($form$requestData);
  48.         }
  49.         $data array_merge(
  50.             $this->shopProductListDataHandler->retrieveData($requestData),
  51.             $this->shopProductsSortDataHandler->retrieveData($requestData),
  52.             $this->paginationDataHandler->retrieveData($requestData)
  53.         );
  54.         $template $request->get('template');
  55.         $products $this->shopProductsFinder->find($data);
  56.         self::$currentPageProducts $products->getCurrentPageResults();
  57.         self::$taxon $data['taxon'];
  58.         return new Response($this->twig->render($template, [
  59.             'form' => $form->createView(),
  60.             'products' => $products,
  61.             'taxon' => $data['taxon'],
  62.         ]));
  63.     }
  64.     private function clearInvalidEntries(FormInterface $form, array $requestData): array
  65.     {
  66.         /** @var FormError $error */
  67.         foreach ($form->getErrors(true) as $error) {
  68.             $errorOrigin $error->getOrigin();
  69.             if (null === $errorOrigin) {
  70.                 continue;
  71.             }
  72.             $parent $errorOrigin->getParent();
  73.             if (null === $parent) {
  74.                 continue;
  75.             }
  76.             $path = ($parent->getPropertyPath() ?? '') . $errorOrigin->getPropertyPath();
  77.             $keys explode(']['trim($path'[]'));
  78.             $dataRef = &$requestData;
  79.             foreach ($keys as $index => $key) {
  80.                 if (isset($dataRef[$key])) {
  81.                     if ($index === count($keys) - 1) {
  82.                         unset($dataRef[$key]);
  83.                     } else {
  84.                         $dataRef = &$dataRef[$key];
  85.                     }
  86.                 }
  87.             }
  88.         }
  89.         return $requestData;
  90.     }
  91.     public static function getCurrentPageProducts(): iterable
  92.     {
  93.         return self::$currentPageProducts;
  94.     }
  95.     public static function getTaxon(): TaxonInterface
  96.     {
  97.         return self::$taxon;
  98.     }
  99. }