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

SettingsFormFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 23 2
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