ServiceAttributeType   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 72
c 2
b 0
f 0
dl 0
loc 98
ccs 0
cts 40
cp 0
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
B buildForm() 0 94 2
1
<?php
2
3
namespace App\Form;
4
5
use App\Entity\ServiceAttributeType as ServiceAttributeTypeEnum;
6
use App\Entity\ServiceProvider;
7
use Doctrine\ORM\EntityRepository;
8
use SchulIT\CommonBundle\Form\FieldsetType;
9
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
10
use Symfony\Component\Form\AbstractType;
11
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
12
use Symfony\Component\Form\Extension\Core\Type\EnumType;
13
use Symfony\Component\Form\Extension\Core\Type\TextType;
14
use Symfony\Component\Form\FormBuilderInterface;
15
use Symfony\Component\Form\FormEvent;
16
use Symfony\Component\Form\FormEvents;
17
use Symfony\Contracts\Translation\TranslatorInterface;
18
19
class ServiceAttributeType extends AbstractType {
20
21
    public function __construct(private readonly TranslatorInterface $translator) { }
22
23
    public function buildForm(FormBuilderInterface $builder, array $options) {
24
        $builder->add('group_general', FieldsetType::class, [
25
            'legend' => 'label.general',
26
            'fields' => function(FormBuilderInterface $builder) {
27
                $builder
28
                    ->add('name', TextType::class, [
29
                        'label' => 'label.name'
30
                    ])
31
                    ->add('label', TextType::class, [
32
                        'label' => 'label.label'
33
                    ])
34
                    ->add('description', TextType::class, [
35
                        'label' => 'label.description'
36
                    ])
37
                    ->add('isUserEditEnabled', CheckboxType::class, [
38
                        'label' => 'label.user_edit_enabled',
39
                        'required' => false,
40
                        'label_attr' => [
41
                            'class' => 'checkbox-custom'
42
                        ]
43
                    ])
44
                    ->add('samlAttributeName', TextType::class, [
45
                        'label' => 'label.saml_attribute_name'
46
                    ])
47
                    ->add('type', EnumType::class, [
48
                        'class' => ServiceAttributeTypeEnum::class,
49
                        'label' => 'label.type',
50
                        'label_attr' => [
51
                            'class' => 'radio-custom'
52
                        ],
53
                        'expanded' => true,
54
                        'choice_label' => fn(ServiceAttributeTypeEnum $type) => $this->translator->trans('service_attribute_type.' . $type->value, [ ], 'enums')
55
                    ])
56
                    ->add('services', EntityType::class, [
57
                        'class' => ServiceProvider::class,
58
                        'query_builder' => fn(EntityRepository $repository) => $repository->createQueryBuilder('s')
59
                            ->orderBy('s.name', 'asc'),
60
                        'choice_label' => 'name',
61
                        'label' => 'label.services',
62
                        'multiple' => true,
63
                        'required' => false,
64
                        'expanded' => true,
65
                        'label_attr' => [
66
                            'class' => 'checkbox-custom'
67
                        ]
68
                    ]);
69
            }
70
        ])
71
            ->add('group_select', FieldsetType::class, [
72
                'attr' => [
73
                    'id'=> 'group_select'
74
                ],
75
                'legend' => 'label.options',
76
                'fields' => function(FormBuilderInterface $builder) {
77
                    $builder
78
                        ->add('isMultipleChoice', CheckboxType::class, [
79
                            'label' => 'label.is_multiple_choice',
80
                            'required' => false,
81
                            'label_attr' => [
82
                                'class' => 'checkbox-custom'
83
                            ]
84
                        ])
85
                        ->add('options', KeyValueType::class, [
86
                            'value_type' => TextType::class,
87
                            'value_options' => [
88
                                'label' => 'label.value'
89
                            ],
90
                            'key_type' => TextType::class,
91
                            'key_options' => [
92
                                'label' => 'label.key'
93
                            ],
94
                            'allow_add' => true,
95
                            'allow_delete' => true,
96
                            'label' => 'label.options'
97
                        ]);
98
                }
99
        ])
100
            ->addEventListener(FormEvents::POST_SET_DATA, function(FormEvent $event) {
101
                $attribute = $event->getData();
102
                $form = $event->getForm();
103
104
                if($attribute->getId() !== null) {
105
                    $form->get('group_general')
106
                        ->add('type', EnumType::class, [
107
                            'class' => ServiceAttributeTypeEnum::class,
108
                            'label' => 'label.type',
109
                            'label_attr' => [
110
                                'class' => 'radio-custom'
111
                            ],
112
                            'expanded' => true,
113
                            'disabled' => true,
114
                            'mapped' => false,
115
                            'data' => $attribute->getType(),
116
                            'choice_label' => fn(ServiceAttributeTypeEnum $type) => $this->translator->trans('service_attribute_type.' . $type->value, [ ], 'enums')
117
                        ]);
118
                }
119
            });
120
    }
121
}