Completed
Push — master ( 0be8e1...585ca2 )
by Daniel
59:26 queued 46:02
created

FormFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 34
ccs 0
cts 11
cp 0
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A create() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Silverback\ApiComponentBundle\Factory\Form;
6
7
use Silverback\ApiComponentBundle\Entity\Component\Form\Form;
8
use Symfony\Component\Form\FormBuilderInterface;
9
use Symfony\Component\Form\FormFactoryInterface;
10
use Symfony\Component\Routing\RouterInterface;
11
12
class FormFactory
13
{
14
    private $formFactory;
15
    private $router;
16
17
    /**
18
     * @param FormFactoryInterface $formFactory
19
     * @param RouterInterface $router
20
     */
21
    public function __construct(
22
        FormFactoryInterface $formFactory,
23
        RouterInterface $router
24
    ) {
25
        $this->formFactory = $formFactory;
26
        $this->router = $router;
27
    }
28
29
    /**
30
     * @param Form $component
31
     * @return FormBuilderInterface
32
     */
33
    public function create(Form $component): FormBuilderInterface
34
    {
35
        $builder = $this->formFactory->createBuilder($component->getFormType());
36
        if (!($currentAction = $builder->getAction()) || $currentAction === '') {
37
            $action = $this->router->generate(
38
                'api_forms_post_item',
39
                [
40
                    'id' => $component->getId()
41
                ]
42
            );
43
            $builder->setAction($action);
44
        }
45
        return $builder;
46
    }
47
}
48