UserType::configureOptions()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 1
nop 1
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminSecurityBundle\Form\Type\Admin;
13
14
use Symfony\Component\Form\AbstractType;
15
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
16
use Symfony\Component\Form\Extension\Core\Type\EmailType;
17
use Symfony\Component\Form\FormBuilderInterface;
18
use Symfony\Component\Form\FormInterface;
19
use Symfony\Component\OptionsResolver\OptionsResolver;
20
21
class UserType extends AbstractType
22
{
23
    /**
24
     * @var string
25
     */
26
    private $dataClass;
27
28
    /**
29
     * @var array
30
     */
31
    private $roles;
32
33
    public function __construct(?string $dataClass, ?array $roles)
34
    {
35
        $this->dataClass = $dataClass;
36
        $this->roles = $roles;
37
    }
38
39
    public function buildForm(FormBuilderInterface $builder, array $options): void
40
    {
41
        $builder->add('email', EmailType::class, ['label' => 'admin.admin_user.email']);
42
43
        $builder->add('roles', ChoiceType::class, [
44
            'label' => 'admin.admin_user.roles',
45
            'choices' => $this->getRoleList(),
46
            'expanded' => true,
47
            'multiple' => true,
48
        ]);
49
    }
50
51
    public function configureOptions(OptionsResolver $resolver): void
52
    {
53
        $resolver->setDefaults([
54
            'data_class' => $this->dataClass,
55
            'translation_domain' => 'FSiAdminSecurity',
56
            'validation_groups' => function (FormInterface $form): array {
57
                return ['Default', null !== $form->getData() ? 'Edit' : 'Create'];
58
            }
59
        ]);
60
    }
61
62
    private function getRoleList(): array
63
    {
64
        $roleList = [];
65
66
        foreach ($this->roles as $role => $child) {
67
            $label = sprintf('%s [ %s ]', $role, implode(', ', $child));
68
            $roleList[$label] = $role;
69
        }
70
71
        return $roleList;
72
    }
73
}
74