EntityToIdentifierTransformer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 28
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 11 2
A reverseTransform() 0 7 2
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Bundle\CoreBundle\Form\DataTransformer;
14
15
use Symfony\Component\PropertyAccess\PropertyPathInterface;
16
17
/**
18
 * Class EntityToIdentifierTransformer
19
 *
20
 * @author  Adam Piotrowski <[email protected]>
21
 */
22
class EntityToIdentifierTransformer extends AbstractDataTransformer
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function transform($modelData)
28
    {
29
        if (null === $modelData) {
30
            return 0;
31
        }
32
33
        $meta       = $this->getRepository()->getMetadata();
34
        $identifier = $meta->getSingleIdentifierFieldName();
35
36
        return $this->propertyAccessor->getValue($modelData, $identifier);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function reverseTransform($modelData, PropertyPathInterface $propertyPath, $value)
43
    {
44
        if (null !== $value) {
45
            $entity = $this->getRepository()->find($value);
46
            $this->propertyAccessor->setValue($modelData, $propertyPath, $entity);
47
        }
48
    }
49
}
50