Completed
Pull Request — master (#15)
by Pavel
12:17
created

MappedEntityFormFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 69.23%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 56
ccs 18
cts 26
cp 0.6923
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B createFormForClass() 0 29 3
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Adaptors\Symfony;
4
5
use ScayTrase\Api\Cruds\Exception\EntityProcessingException;
6
use ScayTrase\Api\Cruds\PropertyMapperInterface;
7
use Symfony\Component\Form\FormFactoryInterface;
8
use Symfony\Component\Form\FormInterface;
9
10
final class MappedEntityFormFactory
11
{
12
    /** @var  FormFactoryInterface */
13
    private $factory;
14
    /** @var  PropertyMapperInterface */
15
    private $mapper;
16
17
    /**
18
     * MappedEntityFormFactory constructor.
19
     *
20
     * @param FormFactoryInterface    $factory
21
     * @param PropertyMapperInterface $mapper
22
     */
23 1
    public function __construct(FormFactoryInterface $factory, PropertyMapperInterface $mapper)
24
    {
25 1
        $this->factory = $factory;
26 1
        $this->mapper  = $mapper;
27 1
    }
28
29
    /**
30
     * @param $className
31
     *
32
     * @return FormInterface
33
     *
34
     * @throws EntityProcessingException
35
     */
36 1
    public function createFormForClass($className)
37
    {
38 1
        $form = $this->factory->create();
39
40
        try {
41 1
            foreach ($this->mapper->getApiProperties($className) as $apiProperty) {
42 1
                $form->add(
43 1
                    $this->factory->createForProperty(
44 1
                        $className,
45 1
                        $this->mapper->getEntityProperty($className, $apiProperty),
46 1
                        null,
47 1
                        ['auto_initialize' => false]
48 1
                    )
49 1
                );
50 1
            }
51 1
        } catch (\Exception $exception) {
52
            throw new EntityProcessingException(
53
                sprintf(
54
                    'Cannot create form for class %s: %s',
55
                    $className,
56
                    $exception->getMessage()
57
                ),
58
                $exception->getCode(),
59
                $exception
60
            );
61
        }
62
63 1
        return $form;
64
    }
65
}
66