Passed
Push — master ( 410a8e...414ff7 )
by Arnaud
04:38 queued 01:13
created

AdminFormFactory::createEntityForm()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 9.1008

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 33
ccs 13
cts 20
cp 0.65
rs 8.8333
cc 7
nc 10
nop 3
crap 9.1008
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Factory;
6
7
use LAG\AdminBundle\Admin\AdminInterface;
8
use LAG\AdminBundle\DataProvider\Registry\DataProviderRegistryInterface;
9
use LAG\AdminBundle\Utils\FormUtils;
10
use Symfony\Component\Form\Extension\Core\Type\FormType;
11
use Symfony\Component\Form\FormFactoryInterface;
12
use Symfony\Component\Form\FormInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
15
class AdminFormFactory implements AdminFormFactoryInterface
16
{
17
    private DataProviderRegistryInterface $registry;
18
    private FormFactoryInterface $formFactory;
19
    private FieldFactoryInterface $fieldFactory;
20
21 4
    public function __construct(
22
        DataProviderRegistryInterface $registry,
23
        FormFactoryInterface $formFactory,
24
        FieldFactoryInterface $fieldFactory
25
    ) {
26 4
        $this->registry = $registry;
27 4
        $this->formFactory = $formFactory;
28 4
        $this->fieldFactory = $fieldFactory;
29 4
    }
30
31 2
    public function createEntityForm(AdminInterface $admin, Request $request, object $data = null): FormInterface
32
    {
33 2
        if ($data === null) {
34 2
            $dataProviderName = $admin->getConfiguration()->getDataProvider();
35 2
            $data = $this->registry->get($dataProviderName)->create($admin->getEntityClass());
36
        }
37 2
        $action = $admin->getAction();
38 2
        $formType = $action->getConfiguration()->getForm();
39
40 2
        if ($formType !== null) {
41 1
            return $this->formFactory->create($formType, $data, $action->getConfiguration()->getFormOptions());
42
        }
43 1
        $formBuilder = $this->formFactory->createBuilder(FormType::class, $data, [
44 1
            'label' => false,
45
        ]);
46 1
        $fieldDefinitions = $this->fieldFactory->createDefinitions(\get_class($data));
47
48 1
        foreach ($fieldDefinitions as $name => $definition) {
49
            // We do not want to edit those values in a Form
50
            if (\in_array($name, ['createdAt', 'updatedAt']) && 'datetime' === $definition->getType()) {
51
                continue;
52
            }
53
54
            if ($name === 'id') {
55
                continue;
56
            }
57
            $formType = FormUtils::convertShortFormType($definition->getType());
58
            $formOptions = $definition->getFormOptions();
59
60
            $formBuilder->add($name, $formType, $formOptions);
61
        }
62
63 1
        return $formBuilder->getForm();
64
    }
65
66 1
    public function createDeleteForm(AdminInterface $admin, Request $request, $data): FormInterface
67
    {
68 1
        $action = $admin->getAction();
69 1
        $actionConfiguration = $action->getConfiguration();
70
71
        return $this
72 1
            ->formFactory
73 1
            ->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

73
            ->create(/** @scrutinizer ignore-type */ $actionConfiguration->getForm(), $data, $actionConfiguration->getFormOptions())
Loading history...
74
        ;
75
    }
76
}
77