SearchType::getBlockPrefix()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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\Form\Type;
12
13
use BitBag\SyliusElasticsearchPlugin\Facet\RegistryInterface;
14
use BitBag\SyliusElasticsearchPlugin\Model\Search;
15
use BitBag\SyliusElasticsearchPlugin\Model\SearchBox;
16
use BitBag\SyliusElasticsearchPlugin\QueryBuilder\QueryBuilderInterface;
17
use Elastica\Query;
18
use FOS\ElasticaBundle\Finder\PaginatedFinderInterface;
19
use FOS\ElasticaBundle\Paginator\FantaPaginatorAdapter;
20
use Pagerfanta\Adapter\AdapterInterface;
21
use Symfony\Component\Form\AbstractType;
22
use Symfony\Component\Form\FormBuilderInterface;
23
use Symfony\Component\Form\FormEvent;
24
use Symfony\Component\Form\FormEvents;
25
use Symfony\Component\Form\FormInterface;
26
use Symfony\Component\OptionsResolver\OptionsResolver;
27
28
final class SearchType extends AbstractType
29
{
30
    /** @var PaginatedFinderInterface */
31
    private $finder;
32
33
    /** @var RegistryInterface */
34
    private $facetRegistry;
35
36
    /** @var QueryBuilderInterface */
37
    private $searchProductsQueryBuilder;
38
39
    public function __construct(
40
        PaginatedFinderInterface $finder,
41
        RegistryInterface $facetRegistry,
42
        QueryBuilderInterface $searchProductsQueryBuilder
43
    ) {
44
        $this->finder = $finder;
45
        $this->facetRegistry = $facetRegistry;
46
        $this->searchProductsQueryBuilder = $searchProductsQueryBuilder;
47
    }
48
49
    public function buildForm(FormBuilderInterface $builder, array $options): void
50
    {
51
        $builder
52
            ->add('box', SearchBoxType::class, ['label' => false])
53
            ->setMethod('GET')
54
        ;
55
56
        $formModifier = function (FormInterface $form, AdapterInterface $adapter) {
57
            if (!$adapter instanceof FantaPaginatorAdapter) {
58
                return;
59
            }
60
61
            $form->add('facets', SearchFacetsType::class, ['facets' => $adapter->getAggregations(), 'label' => false]);
62
        };
63
64
        $builder
65
            ->get('box')
66
            ->addEventListener(
67
                FormEvents::POST_SUBMIT,
68
                function (FormEvent $event) use ($formModifier) {
69
                    /** @var SearchBox $data */
70
                    $data = $event->getForm()->getData();
71
72
                    if (!$data->getQuery()) {
73
                        return;
74
                    }
75
76
                    $query = new Query($this->searchProductsQueryBuilder->buildQuery(['query' => $data->getQuery()]));
77
78
                    foreach ($this->facetRegistry->getFacets() as $facetId => $facet) {
79
                        $query->addAggregation($facet->getAggregation()->setName($facetId));
80
                    }
81
                    $query->setSize(0);
82
83
                    $results = $this->finder->findPaginated($query);
84
85
                    if ($results->getAdapter()) {
86
                        $formModifier($event->getForm()->getParent(), $results->getAdapter());
87
                    }
88
                }
89
            )
90
        ;
91
    }
92
93
    public function configureOptions(OptionsResolver $resolver): void
94
    {
95
        $resolver->setDefaults([
96
            'data_class' => Search::class,
97
            'csrf_protection' => false,
98
        ]);
99
    }
100
101
    public function getBlockPrefix(): string
102
    {
103
        return 'bitbag_elasticsearch_search';
104
    }
105
}
106