Passed
Push — master ( 335c59...1f66c0 )
by Julito
07:27
created

SettingsFormFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 46
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 21 2
1
<?php
2
3
namespace Chamilo\CourseBundle\Manager;
4
5
use Sylius\Bundle\SettingsBundle\Form\Factory\SettingsFormFactoryInterface;
6
use Sylius\Bundle\SettingsBundle\Schema\SchemaFormOptionsInterface;
7
use Sylius\Bundle\SettingsBundle\Schema\SchemaInterface;
8
use Sylius\Component\Registry\ServiceRegistryInterface;
9
use Symfony\Component\Form\Extension\Core\Type\FormType;
10
use Symfony\Component\Form\FormFactoryInterface;
11
12
final class SettingsFormFactory implements SettingsFormFactoryInterface
13
{
14
    /**
15
     * @var ServiceRegistryInterface
16
     */
17
    private $schemaRegistry;
18
19
    /**
20
     * @var FormFactoryInterface
21
     */
22
    private $formFactory;
23
24
    /**
25
     * @param ServiceRegistryInterface $schemaRegistry
26
     * @param FormFactoryInterface     $formFactory
27
     */
28
    public function __construct(ServiceRegistryInterface $schemaRegistry, FormFactoryInterface $formFactory)
29
    {
30
        $this->schemaRegistry = $schemaRegistry;
31
        $this->formFactory = $formFactory;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function create($schemaAlias, $data = null, array $options = [])
38
    {
39
        /** @var SchemaInterface $schema */
40
        $schema = $this->schemaRegistry->get($schemaAlias);
41
42
        if ($schema instanceof SchemaFormOptionsInterface) {
43
            $options = array_merge($schema->getOptions(), $options);
44
        }
45
46
        $builder = $this->formFactory->createBuilder(
47
            FormType::class,
48
            $data,
49
            array_merge_recursive(
50
                ['data_class' => null],
51
                $options
52
            )
53
        );
54
55
        $schema->buildForm($builder);
56
57
        return $builder->getForm();
58
    }
59
}
60