Passed
Pull Request — feature/unit-tests (#37)
by Daniel
05:39
created

FormFactory::create()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 15
ccs 0
cts 8
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 12
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentBundle\Factory\Form;
15
16
use Silverback\ApiComponentBundle\Entity\Component\Form;
17
use Symfony\Component\Form\FormBuilderInterface;
18
use Symfony\Component\Form\FormFactoryInterface;
19
use Symfony\Component\Routing\RouterInterface;
20
21
/**
22
 * @author Daniel West <[email protected]>
23
 */
24
class FormFactory
25
{
26
    private FormFactoryInterface $formFactory;
27
    private RouterInterface $router;
28
29
    public function __construct(
30
        FormFactoryInterface $formFactory,
31
        RouterInterface $router
32
    ) {
33
        $this->formFactory = $formFactory;
34
        $this->router = $router;
35
    }
36
37
    public function create(Form $component): FormBuilderInterface
38
    {
39
        $builder = $this->formFactory->createBuilder($component->formType);
40
        if (!($currentAction = $builder->getAction()) || '' === $currentAction) {
41
            // Should we not be looking for the POST endpoint for the resource from API Platform instead of assuming this will be the name?
42
            $action = $this->router->generate(
43
                'api_forms_post_item',
44
                [
45
                    'id' => $component->getId(),
46
                ]
47
            );
48
            $builder->setAction($action);
49
        }
50
51
        return $builder;
52
    }
53
}
54