UserType::getTimeZoneChoices()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Form/Type/Console/UserType.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Form\Type\Console;
10
11
use App\DTO\User\User as UserDto;
12
use App\Enum\Language;
13
use App\Enum\Locale;
14
use App\Form\DataTransformer\UserGroupTransformer;
15
use App\Form\Type\FormTypeLabelInterface;
16
use App\Form\Type\Traits\AddBasicFieldToForm;
17
use App\Form\Type\Traits\UserGroupChoices;
18
use App\Resource\UserGroupResource;
19
use App\Service\Localization;
0 ignored issues
show
Bug introduced by
The type App\Service\Localization 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...
20
use Symfony\Component\Form\AbstractType;
21
use Symfony\Component\Form\Extension\Core\Type;
22
use Symfony\Component\Form\FormBuilderInterface;
23
use Symfony\Component\OptionsResolver\Exception\AccessException;
24
use Symfony\Component\OptionsResolver\OptionsResolver;
25
use Throwable;
26
use function array_map;
27
28
/**
29
 * Class UserType
30
 *
31
 * @psalm-suppress MissingTemplateParam
32
 *
33
 * @package App\Form\Type\Console
34
 * @author TLe, Tarmo Leppänen <[email protected]>
35
 */
36
class UserType extends AbstractType
37
{
38
    use AddBasicFieldToForm;
39
    use UserGroupChoices;
40
41
    /**
42
     * Base form fields
43
     *
44
     * @var array<int, array<int, mixed>>
45
     */
46
    private static array $formFields = [
47
        [
48
            'username',
49
            Type\TextType::class,
50
            [
51
                FormTypeLabelInterface::LABEL => 'Username',
52
                FormTypeLabelInterface::REQUIRED => true,
53
                FormTypeLabelInterface::EMPTY_DATA => '',
54
            ],
55
        ],
56
        [
57
            'firstName',
58
            Type\TextType::class,
59
            [
60
                FormTypeLabelInterface::LABEL => 'First name',
61
                FormTypeLabelInterface::REQUIRED => true,
62
                FormTypeLabelInterface::EMPTY_DATA => '',
63
            ],
64
        ],
65
        [
66
            'lastName',
67
            Type\TextType::class,
68
            [
69
                FormTypeLabelInterface::LABEL => 'Last name',
70
                FormTypeLabelInterface::REQUIRED => true,
71
                FormTypeLabelInterface::EMPTY_DATA => '',
72
            ],
73
        ],
74
        [
75
            'email',
76
            Type\EmailType::class,
77
            [
78
                FormTypeLabelInterface::LABEL => 'Email address',
79
                FormTypeLabelInterface::REQUIRED => true,
80
                FormTypeLabelInterface::EMPTY_DATA => '',
81
            ],
82
        ],
83
        [
84
            'password',
85
            Type\RepeatedType::class,
86
            [
87
                FormTypeLabelInterface::TYPE => Type\PasswordType::class,
88
                FormTypeLabelInterface::REQUIRED => true,
89
                FormTypeLabelInterface::FIRST_NAME => 'password1',
90
                FormTypeLabelInterface::FIRST_OPTIONS => [
91
                    FormTypeLabelInterface::LABEL => 'Password',
92
                ],
93
                FormTypeLabelInterface::SECOND_NAME => 'password2',
94
                FormTypeLabelInterface::SECOND_OPTIONS => [
95
                    FormTypeLabelInterface::LABEL => 'Repeat password',
96
                ],
97
            ],
98
        ],
99
    ];
100
101 1
    public function __construct(
102
        private readonly UserGroupResource $userGroupResource,
103
        private readonly UserGroupTransformer $userGroupTransformer,
104
        private readonly Localization $localization,
105
    ) {
106 1
    }
107
108
    /**
109
     * {@inheritdoc}
110
     *
111
     * @throws Throwable
112
     */
113 1
    public function buildForm(FormBuilderInterface $builder, array $options): void
114
    {
115 1
        parent::buildForm($builder, $options);
116
117 1
        $this->addBasicFieldToForm($builder, self::$formFields);
118 1
        $this->addLocalizationFieldsToForm($builder);
119
120 1
        $builder
121 1
            ->add(
122 1
                'userGroups',
123 1
                Type\ChoiceType::class,
124 1
                [
125 1
                    FormTypeLabelInterface::CHOICES => $this->getUserGroupChoices(),
126 1
                    FormTypeLabelInterface::REQUIRED => true,
127 1
                    FormTypeLabelInterface::EMPTY_DATA => '',
128 1
                    'multiple' => true,
129 1
                ]
130 1
            );
131
132 1
        $builder->get('userGroups')->addModelTransformer($this->userGroupTransformer);
133
    }
134
135
    /**
136
     * Configures the options for this type.
137
     *
138
     * @param OptionsResolver $resolver The resolver for the options
139
     *
140
     * @throws AccessException
141
     */
142 1
    public function configureOptions(OptionsResolver $resolver): void
143
    {
144 1
        parent::configureOptions($resolver);
145
146 1
        $resolver->setDefaults([
147 1
            'data_class' => UserDto::class,
148 1
        ]);
149
    }
150
151 1
    private function addLocalizationFieldsToForm(FormBuilderInterface $builder): void
152
    {
153 1
        $builder
154 1
            ->add(
155 1
                'language',
156 1
                Type\EnumType::class,
157 1
                [
158 1
                    FormTypeLabelInterface::CLASS_NAME => Language::class,
159 1
                    FormTypeLabelInterface::LABEL => 'Language',
160 1
                    FormTypeLabelInterface::REQUIRED => true,
161 1
                    FormTypeLabelInterface::EMPTY_DATA => Language::getDefault(),
162 1
                ],
163 1
            );
164
165 1
        $builder
166 1
            ->add(
167 1
                'locale',
168 1
                Type\EnumType::class,
169 1
                [
170 1
                    FormTypeLabelInterface::CLASS_NAME => Locale::class,
171 1
                    FormTypeLabelInterface::LABEL => 'Locale',
172 1
                    FormTypeLabelInterface::REQUIRED => true,
173 1
                    FormTypeLabelInterface::EMPTY_DATA => Locale::getDefault(),
174 1
                ],
175 1
            );
176
177 1
        $builder
178 1
            ->add(
179 1
                'timezone',
180 1
                Type\ChoiceType::class,
181 1
                [
182 1
                    FormTypeLabelInterface::LABEL => 'Timezone',
183 1
                    FormTypeLabelInterface::REQUIRED => true,
184 1
                    FormTypeLabelInterface::EMPTY_DATA => Localization::DEFAULT_TIMEZONE,
185 1
                    FormTypeLabelInterface::CHOICES => $this->getTimeZoneChoices(),
186 1
                ],
187 1
            );
188
    }
189
190
    /**
191
     * Method to get choices array for time zones.
192
     *
193
     * @return array<string, string>
194
     */
195 1
    private function getTimeZoneChoices(): array
196
    {
197
        // Initialize output
198 1
        $choices = [];
199
200 1
        $iterator = static function (array $timezone) use (&$choices): void {
201 1
            $choices[$timezone['value'] . ' (' . $timezone['offset'] . ')'] = $timezone['identifier'];
202 1
        };
203
204 1
        array_map($iterator, $this->localization->getFormattedTimezones());
205
206 1
        return $choices;
207
    }
208
}
209