AddDistrictFieldSubscriber   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 3 1
A onCitySelected() 0 17 2
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 AddDistrictFieldSubscriber implements EventSubscriberInterface
13
{
14
    /**
15
     * @return string[]
16
     */
17
    public static function getSubscribedEvents(): array
18
    {
19
        return [FormEvents::POST_SET_DATA => 'onCitySelected'];
20
    }
21
22
    public function onCitySelected(FormEvent $event): void
23
    {
24
        $form = $event->getForm();
25
        $data = $event->getData();
26
        $city = $data->getCity();
27
28
        if ($city) {
29
            $form->add('district', EntityType::class, [
30
                'class' => 'App\Entity\District',
31
                'placeholder' => 'placeholder.select_district',
32
                'choice_label' => 'name',
33
                'attr' => [
34
                    'class' => 'form-control',
35
                ],
36
                'required' => false,
37
                'label' => 'label.district',
38
                'choices' => $city->getDistricts(),
39
            ]);
40
        }
41
    }
42
}
43