FormTypeExtension   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 10
c 3
b 0
f 0
dl 0
loc 47
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getExtendedType() 0 3 1
A getExtendedTypes() 0 3 1
A buildForm() 0 7 2
A __construct() 0 4 1
1
<?php declare(strict_types = 1);
2
3
namespace Bukashk0zzz\FilterBundle\Form\Extension;
4
5
use Bukashk0zzz\FilterBundle\Form\EventListener\FilterListener;
6
use Bukashk0zzz\FilterBundle\Service\Filter;
7
use Symfony\Component\Form\AbstractTypeExtension;
8
use Symfony\Component\Form\Extension\Core\Type\FormType;
9
use Symfony\Component\Form\FormBuilderInterface;
10
11
/**
12
 * Class FormTypeExtension.
13
 */
14
class FormTypeExtension extends AbstractTypeExtension
15
{
16
    /**
17
     * @var Filter
18
     */
19
    protected $filterService;
20
21
    /**
22
     * @var bool
23
     */
24
    protected $autoFilter;
25
26
    /**
27
     * FilterListener constructor.
28
     */
29
    public function __construct(Filter $filterService, bool $autoFilter)
30
    {
31
        $this->filterService = $filterService;
32
        $this->autoFilter = $autoFilter;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getExtendedType()
39
    {
40
        return self::getExtendedTypes()[0];
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public static function getExtendedTypes(): iterable
47
    {
48
        return [FormType::class];
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function buildForm(FormBuilderInterface $builder, array $options): void
55
    {
56
        if (!$this->autoFilter) {
57
            return;
58
        }
59
60
        $builder->addEventSubscriber(new FilterListener($this->filterService));
61
    }
62
}
63