SearchFacetsType::configureOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
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\SearchFacets;
15
use Symfony\Component\Form\AbstractType;
16
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
17
use Symfony\Component\Form\FormBuilderInterface;
18
use Symfony\Component\OptionsResolver\OptionsResolver;
19
20
final class SearchFacetsType extends AbstractType
21
{
22
    /** @var RegistryInterface */
23
    private $facetRegistry;
24
25
    public function __construct(RegistryInterface $facetRegistry)
26
    {
27
        $this->facetRegistry = $facetRegistry;
28
    }
29
30
    public function buildForm(FormBuilderInterface $builder, array $options): void
31
    {
32
        foreach ($options['facets'] as $facetId => $facetData) {
33
            $facet = $this->facetRegistry->getFacetById($facetId);
34
            $choices = [];
35
            foreach ($facetData['buckets'] as $bucket) {
36
                $choices[$facet->getBucketLabel($bucket)] = $bucket['key'];
37
            }
38
            if (!empty($choices)) {
39
                $builder
40
                    ->add(
41
                        $facetId,
42
                        ChoiceType::class,
43
                        [
44
                            'label' => $facet->getLabel(),
45
                            'choices' => $choices,
46
                            'expanded' => true,
47
                            'multiple' => true,
48
                        ]
49
                    )
50
                ;
51
            }
52
        }
53
    }
54
55
    public function configureOptions(OptionsResolver $resolver): void
56
    {
57
        $resolver->setRequired('facets');
58
        $resolver->setDefault('data_class', SearchFacets::class);
59
    }
60
}
61