Passed
Pull Request — master (#66)
by Daniel
07:13
created

FormViewFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components 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\ApiComponentsBundle\Factory\Form;
15
16
use ApiPlatform\Core\Api\IriConverterInterface;
17
use Silverback\ApiComponentsBundle\Dto\FormView;
18
use Silverback\ApiComponentsBundle\Entity\Component\Form;
19
use Symfony\Component\Form\FormFactoryInterface;
20
use Symfony\Component\HttpFoundation\UrlHelper;
21
22
/**
23
 * @author Daniel West <[email protected]>
24
 */
25
class FormViewFactory
26
{
27
    private FormFactoryInterface $formFactory;
28
    private IriConverterInterface $iriConverter;
29
    private UrlHelper $urlHelper;
30
31
    public function __construct(FormFactoryInterface $formFactory, IriConverterInterface $iriConverter, UrlHelper $urlHelper)
32
    {
33
        $this->formFactory = $formFactory;
34
        $this->iriConverter = $iriConverter;
35
        $this->urlHelper = $urlHelper;
36
    }
37
38
    public function create(Form $form): FormView
39
    {
40
        $builder = $this->formFactory->createBuilder($form->formType);
41
42
        if (!($currentAction = $builder->getAction()) || '' === $currentAction) {
43
            $builder->setAction($this->getFormAction($form));
44
        }
45
46
        return new FormView($builder->getForm());
47
    }
48
49
    private function getFormAction(Form $form): string
50
    {
51
        return $this->urlHelper->getAbsoluteUrl($this->iriConverter->getIriFromItem($form) . '/submit');
52
    }
53
}
54