Completed
Push — master ( f1dd26...2cc827 )
by Dmitri
03:03
created

ChoiceController::indexAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use Devmachine\Bundle\FormBundle\Form\Type\ChildChoiceType;
6
use Devmachine\Bundle\FormBundle\Form\Type\FlatChoiceType;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
0 ignored issues
show
Bug introduced by
The type Sensio\Bundle\FrameworkE...dle\Configuration\Route was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
0 ignored issues
show
Bug introduced by
The type Sensio\Bundle\FrameworkE...\Configuration\Template was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
11
12
class ChoiceController extends Controller
13
{
14
    use ServicesTrait;
15
16
    /**
17
     * @Route("/", name="choice")
18
     * @Template("AppBundle::choice.html.twig")
19
     */
20
    public function indexAction()
21
    {
22
        return [
23
            'flat'  => $this->flatForm()->createView(),
24
            'child' => $this->childForm()->createView(),
25
            'title' => 'Choices',
26
            'nav'   => 'choice',
27
        ];
28
    }
29
30
    private function flatForm()
31
    {
32
        $langs = ['PHP', 'Ruby', 'Python', 'Go', 'Java'];
33
34
        $form = $this->createNamedBuilder('flat')
35
            ->add('langs', FlatChoiceType::class, [
36
                'label'     => 'Programming languages',
37
                'separator' => ', ', // default
38
            ])
39
            ->getForm()
40
            ->setData(['langs' => $langs])
41
        ;
42
43
        return $form;
44
    }
45
46
    private function childForm()
47
    {
48
        $countries = ['Germany', 'Japan'];
49
50
        $manufacturers = [
51
            'Germany'  => ['BMW', 'Audi', 'Mercedes'],
52
            'Japan'    => ['Honda', 'Toyota', 'Nissan'],
53
        ];
54
55
        $models = [
56
            'BMW'      => ['X3', 'X5', 'X6'],   'Audi'   => ['A1', 'A6', 'A6'],
57
            'Mercedes' => ['Vito', 'Sprinter'], 'Honda'  => ['Accord', 'Civic'],
58
            'Toyota'   => ['Avensis', 'Camry'], 'Nissan' => ['Versa', 'X-trail'],
59
        ];
60
61
        $combine = function (array $items) { return array_combine($items, $items); };
62
63
        $form = $this->createNamedBuilder('child')
64
            ->add('country', ChoiceType::class, [
65
                'choices'     => $combine($countries),
66
                'required'    => false,
67
                'placeholder' => 'Select country',
68
            ])
69
            ->add('manufacturer', ChildChoiceType::class, [
70
                'parent'      => 'country',
71
                'required'    => false,
72
                'placeholder' => 'Select manufacturer',
73
                'choices'     => array_map($combine, $manufacturers),
74
            ])
75
            ->add('model', ChildChoiceType::class, [
76
                'parent'      => 'manufacturer',
77
                'required'    => false,
78
                'placeholder' => 'Select model',
79
                'choices'     => array_map($combine, $models),
80
                'select2'     => true, // Style with select2
81
            ])
82
            ->getForm()
83
        ;
84
85
        return $form;
86
    }
87
}
88