Passed
Push — master ( 5c51ef...2cabbf )
by Julito
08:38
created

SettingsFormFactory::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 3
dl 0
loc 23
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chamilo\CourseBundle\Settings;
6
7
use Sylius\Bundle\SettingsBundle\Form\Factory\SettingsFormFactoryInterface;
8
use Sylius\Bundle\SettingsBundle\Registry\ServiceRegistryInterface;
9
use Sylius\Bundle\SettingsBundle\Schema\SchemaFormOptionsInterface;
10
use Sylius\Bundle\SettingsBundle\Schema\SchemaInterface;
11
use Symfony\Component\Form\Extension\Core\Type\FormType;
12
use Symfony\Component\Form\FormFactoryInterface;
13
14
final class SettingsFormFactory implements SettingsFormFactoryInterface
15
{
16
    private ServiceRegistryInterface $schemaRegistry;
17
18
    private FormFactoryInterface $formFactory;
19
20
    public function __construct(ServiceRegistryInterface $schemaRegistry, FormFactoryInterface $formFactory)
21
    {
22
        $this->schemaRegistry = $schemaRegistry;
23
        $this->formFactory = $formFactory;
24
    }
25
26
    public function create($schemaAlias, $data = null, array $options = [])
27
    {
28
        /** @var SchemaInterface $schema */
29
        $schema = $this->schemaRegistry->get($schemaAlias);
30
31
        if ($schema instanceof SchemaFormOptionsInterface) {
32
            $options = array_merge($schema->getOptions(), $options);
33
        }
34
35
        $builder = $this->formFactory->createBuilder(
36
            FormType::class,
37
            $data,
38
            array_merge_recursive(
39
                [
40
                    'data_class' => null,
41
                ],
42
                $options
43
            )
44
        );
45
46
        $schema->buildForm($builder);
47
48
        return $builder->getForm();
49
    }
50
}
51