1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitBag\SyliusElasticsearchPlugin\Controller\Action\Shop; |
6
|
|
|
|
7
|
|
|
use BitBag\SyliusElasticsearchPlugin\Block\SearchFormEventListener; |
8
|
|
|
use BitBag\SyliusElasticsearchPlugin\Facet\RegistryInterface; |
9
|
|
|
use BitBag\SyliusElasticsearchPlugin\Model\Search; |
10
|
|
|
use Elastica\Query; |
11
|
|
|
use Elastica\Query\MultiMatch; |
12
|
|
|
use FOS\ElasticaBundle\Finder\PaginatedFinderInterface; |
13
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
16
|
|
|
|
17
|
|
|
final class SearchAction |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var EngineInterface |
21
|
|
|
*/ |
22
|
|
|
private $templatingEngine; |
23
|
|
|
/** |
24
|
|
|
* @var PaginatedFinderInterface |
25
|
|
|
*/ |
26
|
|
|
private $finder; |
27
|
|
|
/** |
28
|
|
|
* @var SearchFormEventListener |
29
|
|
|
*/ |
30
|
|
|
private $searchFormEventListener; |
31
|
|
|
/** |
32
|
|
|
* @var RegistryInterface |
33
|
|
|
*/ |
34
|
|
|
private $facetRegistry; |
35
|
|
|
|
36
|
|
|
public function __construct( |
37
|
|
|
EngineInterface $templatingEngine, |
38
|
|
|
PaginatedFinderInterface $finder, |
39
|
|
|
SearchFormEventListener $searchFormEventListener, |
40
|
|
|
RegistryInterface $facetRegistry |
41
|
|
|
) { |
42
|
|
|
$this->templatingEngine = $templatingEngine; |
43
|
|
|
$this->finder = $finder; |
44
|
|
|
$this->searchFormEventListener = $searchFormEventListener; |
45
|
|
|
$this->facetRegistry = $facetRegistry; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function __invoke(Request $request): Response |
49
|
|
|
{ |
50
|
|
|
$template = $request->get('template'); |
51
|
|
|
$form = $this->searchFormEventListener->getForm(); |
52
|
|
|
$form->handleRequest($request); |
53
|
|
|
|
54
|
|
|
$results = null; |
55
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
56
|
|
|
/** @var Search $search */ |
57
|
|
|
$search = $form->getData(); |
58
|
|
|
|
59
|
|
|
// TODO this is duplicated from src/Form/Type/SearchType.php:61 |
60
|
|
|
$multiMatch = new MultiMatch(); |
61
|
|
|
$multiMatch->setQuery($search->getBox()->getQuery()); |
62
|
|
|
// TODO set search fields here (pay attention to locale-contex field, like name): $query->setFields([]); |
63
|
|
|
$multiMatch->setFuzziness('AUTO'); |
64
|
|
|
|
65
|
|
|
$boolQuery = new Query\BoolQuery(); |
66
|
|
|
$boolQuery->addMust($multiMatch); |
67
|
|
|
|
68
|
|
|
if ($search->getFacets()) { |
69
|
|
|
foreach ($search->getFacets() as $facetId => $selectedBuckets) { |
70
|
|
|
if (!$selectedBuckets) { |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
$facet = $this->facetRegistry->getFacetById($facetId); |
74
|
|
|
$boolQuery->addFilter($facet->getQuery($selectedBuckets)); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$query = new Query($boolQuery); |
79
|
|
|
|
80
|
|
|
$results = $this->finder->findPaginated($query); |
81
|
|
|
} |
82
|
|
|
return $this->templatingEngine->renderResponse( |
83
|
|
|
$template, |
84
|
|
|
['results' => $results, 'searchForm' => $form->createView()] |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|