UpdateMetroFieldSubscriber::onCityChanged()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 18
rs 9.8666
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 UpdateMetroFieldSubscriber implements EventSubscriberInterface
13
{
14
    /**
15
     * @return string[]
16
     */
17
    public static function getSubscribedEvents(): array
18
    {
19
        return [FormEvents::POST_SUBMIT => 'onCityChanged'];
20
    }
21
22
    public function onCityChanged(FormEvent $event): void
23
    {
24
        $form = $event->getForm();
25
26
        if ($form->getData()) {
27
            // Metro stations
28
            $stations = $form->getData()->getMetroStations();
29
30
            $form->getParent()->add('metro_station', EntityType::class, [
31
                'class' => 'App\Entity\Metro',
32
                'placeholder' => 'placeholder.select_metro_station',
33
                'choice_label' => 'name',
34
                'attr' => [
35
                    'class' => 'form-control',
36
                ],
37
                'required' => false,
38
                'label' => 'label.metro_station_name',
39
                'choices' => $stations,
40
            ]);
41
        }
42
    }
43
}
44