vendor/bitbag/elasticsearch-plugin/src/Controller/Action/Shop/SearchAction.php line 24

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file was created by developers working at BitBag
  4.  * Do you need more information about us and what we do? Visit our https://bitbag.io website!
  5.  * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
  6. */
  7. declare(strict_types=1);
  8. namespace BitBag\SyliusElasticsearchPlugin\Controller\Action\Shop;
  9. use BitBag\SyliusElasticsearchPlugin\Block\SearchFormEventListener;
  10. use BitBag\SyliusElasticsearchPlugin\Controller\RequestDataHandler\PaginationDataHandlerInterface;
  11. use BitBag\SyliusElasticsearchPlugin\Facet\RegistryInterface;
  12. use BitBag\SyliusElasticsearchPlugin\Model\Search;
  13. use BitBag\SyliusElasticsearchPlugin\QueryBuilder\QueryBuilderInterface;
  14. use Elastica\Query;
  15. use FOS\ElasticaBundle\Finder\PaginatedFinderInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Twig\Environment;
  19. final class SearchAction
  20. {
  21.     /** @var Environment */
  22.     private $twig;
  23.     /** @var PaginatedFinderInterface */
  24.     private $finder;
  25.     /** @var SearchFormEventListener */
  26.     private $searchFormEventListener;
  27.     /** @var RegistryInterface */
  28.     private $facetRegistry;
  29.     /** @var QueryBuilderInterface */
  30.     private $searchProductsQueryBuilder;
  31.     /** @var PaginationDataHandlerInterface */
  32.     private $paginationDataHandler;
  33.     public function __construct(
  34.         Environment $twig,
  35.         PaginatedFinderInterface $finder,
  36.         SearchFormEventListener $searchFormEventListener,
  37.         RegistryInterface $facetRegistry,
  38.         QueryBuilderInterface $searchProductsQueryBuilder,
  39.         PaginationDataHandlerInterface $paginationDataHandler
  40.     ) {
  41.         $this->twig $twig;
  42.         $this->finder $finder;
  43.         $this->searchFormEventListener $searchFormEventListener;
  44.         $this->facetRegistry $facetRegistry;
  45.         $this->searchProductsQueryBuilder $searchProductsQueryBuilder;
  46.         $this->paginationDataHandler $paginationDataHandler;
  47.     }
  48.     public function __invoke(Request $request): Response
  49.     {
  50.         $template $request->get('template');
  51.         $form $this->searchFormEventListener->getForm();
  52.         $form->handleRequest($request);
  53.         $results null;
  54.         if ($form->isSubmitted() && $form->isValid()) {
  55.             /** @var Search $search */
  56.             $search $form->getData();
  57.             $boolQuery = new Query\BoolQuery();
  58.             $boolQuery->addMust(
  59.                 $this->searchProductsQueryBuilder->buildQuery(['query' => $search->getBox()->getQuery()])
  60.             );
  61.             if ($search->getFacets()) {
  62.                 foreach ($search->getFacets() as $facetId => $selectedBuckets) {
  63.                     if (!$selectedBuckets) {
  64.                         continue;
  65.                     }
  66.                     $facet $this->facetRegistry->getFacetById($facetId);
  67.                     $boolQuery->addFilter($facet->getQuery($selectedBuckets));
  68.                 }
  69.             }
  70.             $query = new Query($boolQuery);
  71.             $results $this->finder->findPaginated($query);
  72.             $paginationData $this->paginationDataHandler->retrieveData($request->query->all());
  73.             $results->setCurrentPage($paginationData[PaginationDataHandlerInterface::PAGE_INDEX]);
  74.             $results->setMaxPerPage($paginationData[PaginationDataHandlerInterface::LIMIT_INDEX]);
  75.         }
  76.         return new Response($this->twig->render(
  77.             $template,
  78.             ['results' => $results'searchForm' => $form->createView()]
  79.         ));
  80.     }
  81. }