SwitchLocaleType::configureOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Copyright 2015 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace OpenConext\ProfileBundle\Form\Type;
20
21
use OpenConext\Profile\Value\Locale;
22
use OpenConext\Profile\Value\LocaleSet;
23
use OpenConext\ProfileBundle\Profile\Command\ChangeLocaleCommand;
24
use OpenConext\ProfileBundle\Service\LocaleService;
25
use Symfony\Component\Form\AbstractType;
26
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
27
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
28
use Symfony\Component\Form\FormBuilderInterface;
29
use Symfony\Component\OptionsResolver\OptionsResolver;
30
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
31
32
class SwitchLocaleType extends AbstractType
33
{
34
    /**
35
     * @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface
36
     */
37
    private $urlGenerator;
38
39
    /**
40
     * @var LocaleService
41
     */
42
    private $localeService;
43
44
    public function __construct(
45
        UrlGeneratorInterface $urlGenerator,
46
        LocaleService $localeService
47
    ) {
48
        $this->urlGenerator  = $urlGenerator;
49
        $this->localeService = $localeService;
50
    }
51
52
    public function buildForm(FormBuilderInterface $builder, array $options)
53
    {
54
        $availableLocales = $this->localeService->getAvailableLocales();
55
        $localeChoices    = $this->formatLocaleChoices($availableLocales);
56
57
        $builder
58
            ->setAction(
59
                $this->urlGenerator->generate(
60
                    'profile.locale_switch_locale',
61
                    ['return-url' => $options['return_url']]
62
                )
63
            )
64
            ->add(
65
                'newLocale',
66
                ChoiceType::class,
67
                [
68
                    'choices' => $localeChoices,
69
                    'data'    => $this->localeService->getLocale()->getLocale(),
70
                    'attr'    => [
71
                        'data-locale-options' => ''
72
                    ]
73
                ]
74
            )
75
            ->add('changeLocale', SubmitType::class, ['label' => 'profile.locale.choose_locale']);
76
    }
77
78
    public function configureOptions(OptionsResolver $resolver)
79
    {
80
        $resolver->setDefaults(
81
            [
82
                'return_url' => '',
83
                'data_class' => ChangeLocaleCommand::class
84
            ]
85
        );
86
87
        $resolver->setRequired('return_url');
88
        $resolver->setAllowedTypes('return_url', 'string');
89
    }
90
91
    public function getBlockPrefix()
92
    {
93
        return 'profile_switch_locale';
94
    }
95
96
    /**
97
     * @param LocaleSet $availableLocales
98
     * @return array
99
     */
100
    private function formatLocaleChoices(LocaleSet $availableLocales)
101
    {
102
        $localeChoices = [];
103
104
        /** @var Locale $locale */
105
        foreach ($availableLocales as $locale) {
106
            $localeChoices['profile.locale.' . $locale->getLocale()] = $locale->getLocale();
107
        }
108
109
        return $localeChoices;
110
    }
111
}
112