EntityToIdTransformer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
eloc 16
dl 0
loc 39
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A transform() 0 6 2
A reverseTransform() 0 12 3
1
<?php
2
3
namespace Fi\CoreBundle\Form\DataTransformer;
4
5
use Symfony\Component\Form\DataTransformerInterface;
6
use Symfony\Component\Form\Exception\TransformationFailedException;
7
use Doctrine\Common\Persistence\ObjectManager;
8
9
class EntityToIdTransformer implements DataTransformerInterface
10
{
11
12
    /**
13
     * @var ObjectManager
14
     */
15
    protected $objectManager;
16
17
    /**
18
     * @var string
19
     */
20
    protected $class;
21
22
    public function __construct(ObjectManager $objectManager, $class)
23
    {
24
        $this->objectManager = $objectManager;
25
        $this->class = $class;
26
    }
27
28
    public function transform($entity)
29
    {
30
        if (null === $entity) {
31
            return;
32
        }
33
        return $entity->getId();
34
    }
35
36
    public function reverseTransform($id)
37
    {
38
        if (!$id) {
39
            return null;
40
        }
41
        $entity = $this->objectManager
42
                ->getRepository($this->class)
43
                ->find($id);
44
        if (null === $entity) {
45
            throw new TransformationFailedException();
46
        }
47
        return $entity;
48
    }
49
}
50