TypeaheadController::indexAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Form\Type\TypeaheadLanguageType;
6
use Devmachine\Bundle\FormBundle\Form\Type\TypeaheadCountryType;
7
use Devmachine\Bundle\FormBundle\Form\Type\TypeaheadTimezoneType;
8
use Devmachine\Bundle\FormBundle\Form\Type\TypeaheadType;
9
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...
10
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...
11
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\Intl\Intl;
15
16
class TypeaheadController extends Controller
17
{
18
    use ServicesTrait;
19
20
    /**
21
     * @Route("/", name="typeahead")
22
     * @Template("AppBundle::typeahead.html.twig")
23
     */
24
    public function indexAction()
25
    {
26
        return [
27
            'language' => $this->languageForm()->createView(),
28
            'airport'  => $this->airportForm()->createView(),
29
            'ajax'     => $this->ajaxForm()->createView(),
30
            'country'  => $this->countryForm()->createView(),
31
            'timezone' => $this->timezoneForm()->createView(),
32
            'title'    => 'Typeahead',
33
            'nav'      => 'typeahead',
34
        ];
35
    }
36
37
    /**
38
     * @Route("/search", name="typeahead_search", methods="get", condition="request.isXmlHttpRequest()")
39
     */
40
    public function searchAction(Request $request)
41
    {
42
        $query = strtolower($request->query->get('query'));
43
44
        $data = [];
45
        if (strlen($query) < 2) {
46
            return JsonResponse::create($data);
47
        }
48
49
        // Do the search, could be a database call.
50
        // Ensure keys are set according to form type.
51
        foreach (Intl::getLanguageBundle()->getLanguageNames() as $key => $val) {
52
            if (strpos(strtolower($val), $query) === 0) {
53
                $data[] = ['key' => $key, 'val' => $val];
54
            }
55
        }
56
57
        return JsonResponse::create($data);
58
    }
59
60
    private function languageForm()
61
    {
62
        return $this->createNamedBuilder('language')
63
            ->add('language', TypeaheadType::class, [
64
                'label'       => 'Programming language',
65
                'source_name' => 'languages',
66
                'min_length'  => 1,
67
                'placeholder' => 'Start typing',
68
                'matcher'     => 'starts_with', // ends_with, contains
69
                'source'      => [
70
                    'PHP', 'Ruby', 'Python', 'Go', 'Java', 'C', 'C++',
71
                    'Javascript', 'ML', 'Racket', 'Lisp', 'Pascal',
72
                ],
73
            ])
74
            ->getForm()
75
        ;
76
    }
77
78
    private function airportForm()
79
    {
80
        return $this->createNamedBuilder('airport')
81
            ->add('airport', TypeaheadType::class, [
82
                'source_name' => 'airports',
83
                'min_length'  => 2,
84
                'highlight'   => false, // Do not highlight current text
85
                'hint'        => false, // Do not show completion suggestion
86
                'label_key'   => 'city',
87
                'value_key'   => 'code',
88
                'source' => [
89
                    ['code' => 'RIX', 'city' => 'Riga', 'country' => 'LV'],
90
                    ['code' => 'LGW', 'city' => 'Gatwick', 'country' => 'UK'],
91
                    ['code' => 'STN', 'city' => 'Stansted', 'country' => 'UK'],
92
                    ['code' => 'LHR', 'city' => 'Heathrow', 'country' => 'UK'],
93
                ],
94
            ])
95
            ->getForm()
96
        ;
97
    }
98
99
    private function ajaxForm()
100
    {
101
        return $this->createNamedBuilder('ajax')
102
            ->add('language', TypeaheadLanguageType::class)
103
            ->getForm()
104
        ;
105
    }
106
107
    private function countryForm()
108
    {
109
        return $this->createNamedBuilder('country')
110
            ->add('country', TypeaheadCountryType::class)
111
            ->getForm()
112
        ;
113
    }
114
115
    private function timezoneForm()
116
    {
117
        return $this->createNamedBuilder('timezone')
118
            ->add('timezone', TypeaheadTimezoneType::class)
119
            ->getForm()
120
        ;
121
    }
122
}
123