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