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) { |
|
|
|
|
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) |
|
|
|
|
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
|
|
|
|
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
orexit
statements that have been added for debug purposes.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.