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

SettingsFormFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
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