Passed
Pull Request — master (#69)
by Manuele
04:01
created

SearchAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 79
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
B __invoke() 0 36 6
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\Controller\RequestDataHandler\PaginationDataHandlerInterface;
9
use BitBag\SyliusElasticsearchPlugin\Facet\RegistryInterface;
10
use BitBag\SyliusElasticsearchPlugin\Model\Search;
11
use BitBag\SyliusElasticsearchPlugin\QueryBuilder\QueryBuilderInterface;
12
use Elastica\Query;
13
use FOS\ElasticaBundle\Finder\PaginatedFinderInterface;
14
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
18
final class SearchAction
19
{
20
    /**
21
     * @var EngineInterface
22
     */
23
    private $templatingEngine;
24
    /**
25
     * @var PaginatedFinderInterface
26
     */
27
    private $finder;
28
    /**
29
     * @var SearchFormEventListener
30
     */
31
    private $searchFormEventListener;
32
    /**
33
     * @var RegistryInterface
34
     */
35
    private $facetRegistry;
36
    /**
37
     * @var QueryBuilderInterface
38
     */
39
    private $searchProductsQueryBuilder;
40
    /**
41
     * @var PaginationDataHandlerInterface
42
     */
43
    private $paginationDataHandler;
44
45
    public function __construct(
46
        EngineInterface $templatingEngine,
47
        PaginatedFinderInterface $finder,
48
        SearchFormEventListener $searchFormEventListener,
49
        RegistryInterface $facetRegistry,
50
        QueryBuilderInterface $searchProductsQueryBuilder,
51
        PaginationDataHandlerInterface $paginationDataHandler
52
    ) {
53
        $this->templatingEngine = $templatingEngine;
54
        $this->finder = $finder;
55
        $this->searchFormEventListener = $searchFormEventListener;
56
        $this->facetRegistry = $facetRegistry;
57
        $this->searchProductsQueryBuilder = $searchProductsQueryBuilder;
58
        $this->paginationDataHandler = $paginationDataHandler;
59
    }
60
61
    public function __invoke(Request $request): Response
62
    {
63
        $template = $request->get('template');
64
        $form = $this->searchFormEventListener->getForm();
65
        $form->handleRequest($request);
66
67
        $results = null;
68
        if ($form->isSubmitted() && $form->isValid()) {
69
            /** @var Search $search */
70
            $search = $form->getData();
71
72
            $boolQuery = new Query\BoolQuery();
73
            $boolQuery->addMust(
74
                $this->searchProductsQueryBuilder->buildQuery(['query' => $search->getBox()->getQuery()])
75
            );
76
77
            if ($search->getFacets()) {
78
                foreach ($search->getFacets() as $facetId => $selectedBuckets) {
79
                    if (!$selectedBuckets) {
80
                        continue;
81
                    }
82
                    $facet = $this->facetRegistry->getFacetById($facetId);
83
                    $boolQuery->addFilter($facet->getQuery($selectedBuckets));
84
                }
85
            }
86
87
            $query = new Query($boolQuery);
88
89
            $results = $this->finder->findPaginated($query);
90
            $paginationData = $this->paginationDataHandler->retrieveData($request->query->all());
91
            $results->setCurrentPage($paginationData[PaginationDataHandlerInterface::PAGE_INDEX]);
92
            $results->setMaxPerPage($paginationData[PaginationDataHandlerInterface::LIMIT_INDEX]);
93
        }
94
        return $this->templatingEngine->renderResponse(
95
            $template,
96
            ['results' => $results, 'searchForm' => $form->createView()]
97
        );
98
    }
99
}
100