Completed
Push — master ( 07e238...0f15f1 )
by Pavel
09:14
created

FormProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Adaptors\Symfony;
4
5
use ScayTrase\Api\Cruds\EntityProcessorInterface;
6
use ScayTrase\Api\Cruds\Exception\EntityProcessingException;
7
use Symfony\Component\Form\FormFactoryInterface;
8
use Symfony\Component\Form\FormTypeInterface;
9
10
final class FormProcessor implements EntityProcessorInterface
11
{
12
    /** @var  FormFactoryInterface */
13
    private $factory;
14
    /** @var  string|FormTypeInterface */
15
    private $form;
16
    /** @var  array */
17
    private $options = [];
18
19
    /**
20
     * FormProcessor constructor.
21
     *
22
     * @param FormFactoryInterface     $factory
23
     * @param string|FormTypeInterface $form
24
     * @param array                    $options
25
     */
26 1
    public function __construct(FormFactoryInterface $factory, $form, array $options = [])
27
    {
28 1
        $this->factory = $factory;
29 1
        $this->form    = $form;
30 1
        $this->options = $options;
31 1
    }
32
33
    /** {@inheritdoc} */
34 1
    public function updateEntity($entity, $data)
35
    {
36 1
        $form = $this->factory->create($this->form, $entity, $this->options);
0 ignored issues
show
Bug introduced by
It seems like $this->form can also be of type object<Symfony\Component\Form\FormTypeInterface>; however, Symfony\Component\Form\F...toryInterface::create() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
37
38 1
        $form->submit($data, false);
39
40 1
        if (!$form->isValid()) {
41 1
            throw EntityProcessingException::invalidDataSubmitted((string)$form->getErrors(true), $data);
42
        }
43
44 1
        return $form->getData();
45
    }
46
}
47