RestEntityTransformer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
ccs 4
cts 4
cp 1
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Pgs\RestfonyBundle\Form\DataTransformer;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
7
use Exception;
8
use RuntimeException;
9
use Symfony\Component\Form\DataTransformerInterface;
10
use Symfony\Component\Form\Exception\TransformationFailedException;
11
12
/**
13
 * Transforms Relationship data for entity based forms.
14
 */
15
class RestEntityTransformer implements DataTransformerInterface
16
{
17
    /** @var ObjectManager */
18
    private $objectManager;
19
20
    /** @var string */
21
    private $entityName;
22
23
    /**
24
     * @param ObjectManager $objectManager
25
     * @param string $entityName
26
     */
27 8
    public function __construct(ObjectManager $objectManager, $entityName)
28
    {
29 8
        $this->objectManager = $objectManager;
30 8
        $this->entityName = $entityName;
31 8
    }
32
33
    /**
34
     * Transforms an entity to a string: __toString or id, in that order.
35
     *
36
     * @param string|null $entity
37
     *
38
     * @return string|null
39
     */
40 5
    public function transform($entity)
41
    {
42 5
        if (null === $entity) {
43 1
            return null;
44
        }
45
46
        try {
47 4
            $entityString = (string) $entity;
48 2
        } catch (Exception $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $e) { ...($metadata, $entity); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
49 2
            $metadata = $this->objectManager->getClassMetadata(get_class($entity));
50 2
            $entityString = $this->getEntityIdentifier($metadata, $entity);
51
        }
52
53 3
        return $entityString;
54
    }
55
56
    /**
57
     * Transforms an id to an entity.
58
     *
59
     * @param string $identifier
60
     *
61
     * @throws TransformationFailedException if entity is not found.
62
     *
63
     * @return null|object
64
     */
65 3
    public function reverseTransform($identifier)
66
    {
67 3
        if (!$identifier) {
68 1
            return null;
69
        }
70
71 2
        $entity = $this->objectManager
72 2
            ->getRepository($this->entityName)
73 2
            ->find($identifier);
74
75 2
        if (null === $entity) {
76 1
            throw new TransformationFailedException(sprintf(
77 1
                'A %s with id "%s" does not exist!',
78 1
                $this->entityName,
79
                $identifier
80
            ));
81
        }
82
83 1
        return $entity;
84
    }
85
86
    /**
87
     * @param ClassMetadata $metadata
88
     * @param mixed $entity The entity.
89
     *
90
     * @throws RuntimeException if multiple ids.
91
     *
92
     * @return mixed
93
     */
94 2 View Code Duplication
    protected function getEntityIdentifier(ClassMetadata $metadata, $entity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
    {
96 2
        if (count($metadata->getIdentifierFieldNames()) !== 1) {
97 1
            throw new RuntimeException('Only one identifier allowed at this time.');
98
        }
99
100 1
        return $metadata->getIdentifierValues($entity)[$metadata->getIdentifierFieldNames()[0]];
101
    }
102
}
103