UserGroupType   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 81
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A buildForm() 0 18 1
A configureOptions() 0 6 1
A getRoleChoices() 0 12 1
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Form/Type/Console/UserGroupType.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Form\Type\Console;
10
11
use App\DTO\UserGroup\UserGroup;
12
use App\Entity\Role;
13
use App\Form\DataTransformer\RoleTransformer;
14
use App\Form\Type\FormTypeLabelInterface;
15
use App\Form\Type\Traits\AddBasicFieldToForm;
16
use App\Resource\RoleResource;
17
use App\Security\RolesService;
18
use Symfony\Component\Form\AbstractType;
19
use Symfony\Component\Form\Extension\Core\Type;
20
use Symfony\Component\Form\FormBuilderInterface;
21
use Symfony\Component\OptionsResolver\OptionsResolver;
22
use Throwable;
23
24
/**
25
 * Class UserGroupType
26
 *
27
 * @psalm-suppress MissingTemplateParam
28
 *
29
 * @package App\Form\Type\Console
30
 * @author TLe, Tarmo Leppänen <[email protected]>
31
 */
32
class UserGroupType extends AbstractType
33
{
34
    use AddBasicFieldToForm;
35
36
    /**
37
     * Base form fields
38
     *
39
     * @var array<int, array<int, mixed>>
40
     */
41
    private static array $formFields = [
42
        [
43
            'name',
44
            Type\TextType::class,
45
            [
46
                FormTypeLabelInterface::LABEL => 'Group name',
47
                FormTypeLabelInterface::REQUIRED => true,
48
                FormTypeLabelInterface::EMPTY_DATA => '',
49
            ],
50
        ],
51
    ];
52
53 1
    public function __construct(
54
        private readonly RolesService $rolesService,
55
        private readonly RoleResource $roleResource,
56
        private readonly RoleTransformer $roleTransformer,
57
    ) {
58 1
    }
59
60
    /**
61
     * {@inheritdoc}
62
     *
63
     * @throws Throwable
64
     */
65 1
    public function buildForm(FormBuilderInterface $builder, array $options): void
66
    {
67 1
        parent::buildForm($builder, $options);
68
69 1
        $this->addBasicFieldToForm($builder, self::$formFields);
70
71 1
        $builder
72 1
            ->add(
73 1
                'role',
74 1
                Type\ChoiceType::class,
75 1
                [
76 1
                    FormTypeLabelInterface::LABEL => 'Role',
77 1
                    FormTypeLabelInterface::CHOICES => $this->getRoleChoices(),
78 1
                    FormTypeLabelInterface::REQUIRED => true,
79 1
                ],
80 1
            );
81
82 1
        $builder->get('role')->addModelTransformer($this->roleTransformer);
83
    }
84
85 1
    public function configureOptions(OptionsResolver $resolver): void
86
    {
87 1
        parent::configureOptions($resolver);
88
89 1
        $resolver->setDefaults([
90 1
            'data_class' => UserGroup::class,
91 1
        ]);
92
    }
93
94
    /**
95
     * Method to get choices array for user groups.
96
     *
97
     * @return array<string, string>
98
     *
99
     * @throws Throwable
100
     */
101 1
    public function getRoleChoices(): array
102
    {
103
        // Initialize output
104 1
        $choices = [];
105
106 1
        $iterator = function (Role $role) use (&$choices): void {
107 1
            $choices[$this->rolesService->getRoleLabel($role->getId())] = $role->getId();
108 1
        };
109
110 1
        array_map($iterator, $this->roleResource->find());
111
112 1
        return $choices;
113
    }
114
}
115