Passed
Pull Request — master (#300)
by Arnaud
14:15 queued 08:05
created

FormFactory::createEntityForm()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 16
c 0
b 0
f 0
nc 5
nop 3
dl 0
loc 28
rs 9.1111
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Form\Factory;
6
7
use LAG\AdminBundle\Admin\AdminInterface;
8
use LAG\AdminBundle\Factory\FieldFactoryInterface;
9
use LAG\AdminBundle\Utils\FormUtils;
10
use Symfony\Component\Form\Extension\Core\Type\FormType;
11
use Symfony\Component\Form\FormFactoryInterface as SymfonyFormFactoryInterface;
12
use Symfony\Component\Form\FormInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
15
class FormFactory implements FormFactoryInterface
16
{
17
    public function __construct(
18
        private SymfonyFormFactoryInterface $formFactory,
19
        private FieldFactoryInterface $fieldFactory
20
    ) {
21
    }
22
23
    public function createEntityForm(AdminInterface $admin, Request $request, object $data): FormInterface
24
    {
25
        $action = $admin->getAction();
26
        $formType = $action->getConfiguration()->getForm();
27
28
        if ($formType !== null) {
29
            return $this->formFactory->create($formType, $data, $action->getConfiguration()->getFormOptions());
30
        }
31
        $formBuilder = $this->formFactory->createBuilder(FormType::class, $data, [
32
            'label' => false,
33
        ]);
34
        $fieldDefinitions = $this->fieldFactory->createDefinitions(\get_class($data));
35
36
        foreach ($fieldDefinitions as $name => $definition) {
37
            // We do not want to edit those values in a Form
38
            if (\in_array($name, ['createdAt', 'updatedAt']) && 'datetime' === $definition->getType()) {
39
                continue;
40
            }
41
42
            if (in_array($name, ['id', 'identifier', 'uuid'])) {
43
                continue;
44
            }
45
            $formType = FormUtils::convertShortFormType($definition->getType());
46
            $formOptions = $definition->getFormOptions();
47
            $formBuilder->add($name, $formType, $formOptions);
48
        }
49
50
        return $formBuilder->getForm();
51
    }
52
53
    public function createDeleteForm(AdminInterface $admin, Request $request, $data): FormInterface
54
    {
55
        $action = $admin->getAction();
56
        $actionConfiguration = $action->getConfiguration();
57
58
        return $this
59
            ->formFactory
60
            ->create($actionConfiguration->getForm(), $data, $actionConfiguration->getFormOptions())
0 ignored issues
show
Bug introduced by
It seems like $actionConfiguration->getForm() can also be of type null; however, parameter $type of Symfony\Component\Form\F...toryInterface::create() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
            ->create(/** @scrutinizer ignore-type */ $actionConfiguration->getForm(), $data, $actionConfiguration->getFormOptions())
Loading history...
61
        ;
62
    }
63
}
64