Test Setup Failed
Branch master (354693)
by Valery
10:57
created

AddAreaFieldSubscriber   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 26
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A onLocalitySelected() 0 19 2
A getSubscribedEvents() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Form\EventSubscriber;
6
7
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
use Symfony\Component\Form\FormEvent;
10
use Symfony\Component\Form\FormEvents;
11
12
class AddAreaFieldSubscriber implements EventSubscriberInterface
13
{
14
    public static function getSubscribedEvents()
15
    {
16
        return [FormEvents::POST_SET_DATA => 'onLocalitySelected'];
17
    }
18
19
    public function onLocalitySelected(FormEvent $event)
20
    {
21
        $form = $event->getForm();
22
        $data = $event->getData();
23
        $areas = $data->getArea();
24
25
        if ($areas) {
26
            $form->get('locality')->setData($areas->getLocality());
27
28
            $form->add('area', EntityType::class, [
29
                'class' => 'App\Entity\Area',
30
                'placeholder' => 'placeholder.select_area',
31
                'choice_label' => 'name',
32
                'attr' => [
33
                    'class' => 'form-control',
34
                ],
35
                'required' => false,
36
                'label' => 'label.area',
37
                'choices' => $areas->getLocality()->getAreas(),
38
            ]);
39
        }
40
    }
41
}
42