Passed
Push — master ( ce0ad5...9496a9 )
by Julito
08:27
created

ExtraFieldType   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 115
c 1
b 0
f 0
dl 0
loc 167
rs 8.8
wmc 45

2 Methods

Rating   Name   Duplication   Size   Complexity  
F buildForm() 0 151 44
A __construct() 0 8 1

How to fix   Complexity   

Complex Class

Complex classes like ExtraFieldType often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ExtraFieldType, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Form;
8
9
use Chamilo\CoreBundle\Entity\ExtraField;
10
use Chamilo\CoreBundle\Entity\ExtraFieldItemInterface;
11
use Chamilo\CoreBundle\Entity\User;
12
use Chamilo\CoreBundle\Repository\ExtraFieldRepository;
13
use Chamilo\CoreBundle\Repository\ExtraFieldValuesRepository;
14
use DateTime;
15
use Symfony\Component\Form\AbstractType;
16
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
17
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
18
use Symfony\Component\Form\Extension\Core\Type\DateType;
19
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
20
use Symfony\Component\Form\Extension\Core\Type\NumberType;
21
use Symfony\Component\Form\Extension\Core\Type\TelType;
22
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
23
use Symfony\Component\Form\Extension\Core\Type\TextType;
24
use Symfony\Component\Form\Extension\Core\Type\UrlType;
25
use Symfony\Component\Form\FormBuilderInterface;
26
use Symfony\Component\Form\FormEvent;
27
use Symfony\Component\Form\FormEvents;
28
use Symfony\Component\Security\Core\Security;
29
30
class ExtraFieldType extends AbstractType
31
{
32
    private ExtraFieldValuesRepository $extraFieldValuesRepository;
33
    private Security $security;
34
    private ExtraFieldRepository $extraFieldRepository;
35
36
    public function __construct(
37
        ExtraFieldValuesRepository $extraFieldValuesRepository,
38
        ExtraFieldRepository $extraFieldRepository,
39
        Security $security
40
    ) {
41
        $this->extraFieldValuesRepository = $extraFieldValuesRepository;
42
        $this->extraFieldRepository = $extraFieldRepository;
43
        $this->security = $security;
44
    }
45
46
    public function buildForm(FormBuilderInterface $builder, array $options): void
47
    {
48
        // @todo implement Course/Session extra fields
49
        /** @var User|null|ExtraFieldItemInterface $item */
50
        $item = $this->security->getUser();
51
52
        if (null === $item) {
53
            return;
54
        }
55
56
        $extraFields = $this->extraFieldRepository->getExtraFields(ExtraField::USER_FIELD_TYPE);
57
        $values = $this->extraFieldValuesRepository->getExtraFieldValuesFromItem($item, ExtraField::USER_FIELD_TYPE);
58
59
        $data = [];
60
        foreach ($values as $value) {
61
            $data[$value->getField()->getVariable()] = $value->getValue();
62
        }
63
64
        foreach ($extraFields as $extraField) {
65
            $text = $extraField->getDisplayText();
66
            $variable = $extraField->getVariable();
67
            $value = $data[$extraField->getVariable()] ?? null;
68
69
            $defaultOptions = [
70
                'label' => $text,
71
                'required' => false,
72
                'by_reference' => false,
73
                'mapped' => false,
74
                'data' => $value,
75
            ];
76
77
            // @todo validate data.
78
            switch ($extraField->getFieldType()) {
79
                case \ExtraField::FIELD_TYPE_DOUBLE_SELECT:
80
                case \ExtraField::FIELD_TYPE_DIVIDER:
81
                case \ExtraField::FIELD_TYPE_TAG:
82
                case \ExtraField::FIELD_TYPE_TIMEZONE:
83
                case \ExtraField::FIELD_TYPE_FILE_IMAGE:
84
                case \ExtraField::FIELD_TYPE_FILE:
85
                case \ExtraField::FIELD_TYPE_LETTERS_SPACE:
86
                case \ExtraField::FIELD_TYPE_ALPHANUMERIC_SPACE:
87
                case \ExtraField::FIELD_TYPE_GEOLOCALIZATION_COORDINATES:
88
                case \ExtraField::FIELD_TYPE_GEOLOCALIZATION:
89
                case \ExtraField::FIELD_TYPE_SELECT_WITH_TEXT_FIELD:
90
                case \ExtraField::FIELD_TYPE_TRIPLE_SELECT:
91
                    //@todo
92
                    break;
93
                case \ExtraField::FIELD_TYPE_VIDEO_URL:
94
                case \ExtraField::FIELD_TYPE_SOCIAL_PROFILE:
95
                    $builder->add($variable, UrlType::class, $defaultOptions);
96
97
                    break;
98
                case \ExtraField::FIELD_TYPE_MOBILE_PHONE_NUMBER:
99
                    $builder->add($variable, TelType::class, $defaultOptions);
100
101
                    break;
102
                case \ExtraField::FIELD_TYPE_DATE:
103
                    if (!empty($value)) {
104
                        $defaultOptions['data'] = new DateTime($value);
105
                    }
106
                    $defaultOptions['widget'] = 'single_text';
107
                    $builder->add($variable, DateType::class, $defaultOptions);
108
109
                    break;
110
                case \ExtraField::FIELD_TYPE_DATETIME:
111
                    if (!empty($value)) {
112
                        $defaultOptions['data'] = new DateTime($value);
113
                    }
114
                    $defaultOptions['widget'] = 'single_text';
115
                    $builder->add($variable, DateTimeType::class, $defaultOptions);
116
117
                    break;
118
                case \ExtraField::FIELD_TYPE_TEXTAREA:
119
                    $builder->add($variable, TextareaType::class, $defaultOptions);
120
121
                    break;
122
                case \ExtraField::FIELD_TYPE_FLOAT:
123
                    $builder->add($variable, NumberType::class, $defaultOptions);
124
125
                    break;
126
                case \ExtraField::FIELD_TYPE_INTEGER:
127
                    $builder->add($variable, IntegerType::class, $defaultOptions);
128
129
                    break;
130
                case \ExtraField::FIELD_TYPE_LETTERS_ONLY:
131
                case \ExtraField::FIELD_TYPE_ALPHANUMERIC:
132
                case \ExtraField::FIELD_TYPE_TEXT:
133
                    $builder->add($variable, TextType::class, $defaultOptions);
134
135
                    break;
136
                case \ExtraField::FIELD_TYPE_CHECKBOX:
137
                case \ExtraField::FIELD_TYPE_RADIO:
138
                case \ExtraField::FIELD_TYPE_SELECT:
139
                case \ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
140
                    if (empty($value)) {
141
                        $defaultOptions['data'] = null;
142
                    }
143
                    $options = $extraField->getOptions();
144
                    $choices = [];
145
                    foreach ($options as $option) {
146
                        $choices[$option->getDisplayText()] = $option->getValue();
147
                    }
148
                    $defaultOptions['choices'] = $choices;
149
150
                    if (\ExtraField::FIELD_TYPE_CHECKBOX === $extraField->getFieldType()) {
151
                        $defaultOptions['expanded'] = true;
152
                        $defaultOptions['multiple'] = true;
153
                    }
154
                    if (\ExtraField::FIELD_TYPE_SELECT === $extraField->getFieldType()) {
155
                        $defaultOptions['expanded'] = false;
156
                        $defaultOptions['multiple'] = false;
157
                    }
158
                    if (\ExtraField::FIELD_TYPE_SELECT_MULTIPLE === $extraField->getFieldType()) {
159
                        $defaultOptions['expanded'] = false;
160
                        $defaultOptions['multiple'] = true;
161
                    }
162
                    $builder->add($variable, ChoiceType::class, $defaultOptions);
163
164
                    break;
165
            }
166
        }
167
168
        $builder->addEventListener(
169
            FormEvents::PRE_SET_DATA,
170
            function (FormEvent $event) use ($item, $extraFields): void {
0 ignored issues
show
Unused Code introduced by
The import $item is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
171
                $data = $event->getData();
172
                foreach ($extraFields as $extraField) {
173
                    $newValue = $data[$extraField->getVariable()] ?? '';
174
                    if (!empty($newValue)) {
175
                        if (\ExtraField::FIELD_TYPE_DATE === $extraField->getFieldType()) {
176
                            var_dump($newValue);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($newValue) looks like debug code. Are you sure you do not want to remove it?
Loading history...
177
                            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
178
                        }
179
                        if (\ExtraField::FIELD_TYPE_DATETIME === $extraField->getFieldType()) {
180
                        }
181
                    }
182
                }
183
            }
184
        );
185
186
        $builder->addEventListener(
187
            FormEvents::PRE_SUBMIT,
188
            function (FormEvent $event) use ($item, $extraFields): void {
189
                $data = $event->getData();
190
                foreach ($extraFields as $extraField) {
191
                    $newValue = $data[$extraField->getVariable()] ?? null;
192
                    var_dump($extraField->getVariable(), var_dump($newValue));
0 ignored issues
show
Security Debugging Code introduced by
var_dump($newValue) looks like debug code. Are you sure you do not want to remove it?
Loading history...
Bug introduced by
Are you sure the usage of var_dump($newValue) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
193
                    if (empty($newValue)) {
194
                        continue;
195
                    }
196
                    $this->extraFieldValuesRepository->updateItemData($extraField, $item, $newValue);
197
                }
198
            }
199
        );
200
    }
201
}
202