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

FormProcessor   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 37
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A updateEntity() 0 12 2
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