1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2018 Gerrit Addiks. |
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
7
|
|
|
* |
8
|
|
|
* @license GPL-3.0 |
9
|
|
|
* |
10
|
|
|
* @author Gerrit Addiks <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Addiks\SymfonyGenerics\Controllers\API; |
14
|
|
|
|
15
|
|
|
use Addiks\SymfonyGenerics\Controllers\ControllerHelperInterface; |
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
use Webmozart\Assert\Assert; |
18
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
use Symfony\Component\Serializer\Encoder\EncoderInterface; |
21
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
22
|
|
|
use DOMDocument; |
23
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
24
|
|
|
use ErrorException; |
25
|
|
|
use Addiks\SymfonyGenerics\Controllers\ApplyDataTemplateTrait; |
26
|
|
|
|
27
|
|
|
final class GenericEntityFetchController |
28
|
|
|
{ |
29
|
|
|
use ApplyDataTemplateTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ControllerHelperInterface |
33
|
|
|
*/ |
34
|
|
|
private $controllerHelper; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var EncoderInterface|null |
38
|
|
|
*/ |
39
|
|
|
private $encoder; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var NormalizerInterface|null |
43
|
|
|
*/ |
44
|
|
|
private $normalizer; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array|null |
48
|
|
|
*/ |
49
|
|
|
private $dataTemplate; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string|null |
53
|
|
|
*/ |
54
|
|
|
private $authorizationAttribute; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
private $entityClass; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
private $format; |
65
|
|
|
|
66
|
11 |
|
public function __construct( |
67
|
|
|
ControllerHelperInterface $controllerHelper, |
68
|
|
|
array $options |
69
|
|
|
) { |
70
|
11 |
|
Assert::null($this->controllerHelper); |
71
|
11 |
|
Assert::keyExists($options, 'entity-class'); |
72
|
|
|
|
73
|
10 |
|
$options = array_merge([ |
74
|
10 |
|
'format' => 'json', |
75
|
|
|
'encoder' => null, |
76
|
|
|
'normalizer' => null, |
77
|
|
|
'data-template' => null, |
78
|
|
|
'authorization-attribute' => null |
79
|
10 |
|
], $options); |
80
|
|
|
|
81
|
10 |
|
Assert::true(is_null($options['encoder']) || $options['encoder'] instanceof EncoderInterface); |
82
|
9 |
|
Assert::true(is_null($options['normalizer']) || $options['normalizer'] instanceof NormalizerInterface); |
83
|
8 |
|
Assert::classExists($options['entity-class']); |
84
|
|
|
|
85
|
7 |
|
$this->controllerHelper = $controllerHelper; |
86
|
7 |
|
$this->encoder = $options['encoder']; |
87
|
7 |
|
$this->normalizer = $options['normalizer']; |
88
|
7 |
|
$this->entityClass = $options['entity-class']; |
89
|
7 |
|
$this->format = $options['format']; |
90
|
7 |
|
$this->dataTemplate = $options['data-template']; |
91
|
7 |
|
$this->authorizationAttribute = $options['authorization-attribute']; |
92
|
7 |
|
} |
93
|
|
|
|
94
|
6 |
|
public function fetchEntity(string $entityId): Response |
95
|
|
|
{ |
96
|
|
|
/** @var object|null $entity */ |
97
|
6 |
|
$entity = $this->controllerHelper->findEntity($this->entityClass, $entityId); |
98
|
|
|
|
99
|
6 |
|
if (is_null($entity)) { |
100
|
1 |
|
throw new InvalidArgumentException(sprintf( |
101
|
1 |
|
"Could not find entity with id '%s'!", |
102
|
1 |
|
$entityId |
103
|
|
|
)); |
104
|
|
|
} |
105
|
|
|
|
106
|
5 |
|
if (!empty($this->authorizationAttribute)) { |
107
|
1 |
|
$this->controllerHelper->denyAccessUnlessGranted($this->authorizationAttribute, $entity); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** @var array $normalizedEntity */ |
111
|
4 |
|
$normalizedEntity = array(); |
|
|
|
|
112
|
|
|
|
113
|
4 |
View Code Duplication |
if ($this->normalizer instanceof NormalizerInterface) { |
|
|
|
|
114
|
1 |
|
$normalizedEntity = $this->normalizer->normalize($entity); |
115
|
|
|
|
116
|
|
|
} else { |
117
|
3 |
|
$normalizer = new ObjectNormalizer(); |
118
|
|
|
|
119
|
3 |
|
$normalizedEntity = $normalizer->normalize($entity); |
120
|
|
|
} |
121
|
|
|
|
122
|
4 |
|
if (!is_array($normalizedEntity)) { |
123
|
1 |
|
throw new ErrorException("Result of normalize process must be an array!"); |
124
|
|
|
} |
125
|
|
|
|
126
|
3 |
|
if (!is_null($this->dataTemplate)) { |
127
|
2 |
|
$normalizedEntity = $this->applyDataTemplate($normalizedEntity, $this->dataTemplate); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** @var string $serializedEntity */ |
131
|
2 |
|
$serializedEntity = ""; |
|
|
|
|
132
|
|
|
|
133
|
2 |
View Code Duplication |
if ($this->encoder instanceof EncoderInterface) { |
|
|
|
|
134
|
|
|
$serializedEntity = $this->encoder->encode($normalizedEntity, $this->format); |
135
|
|
|
|
136
|
|
|
} else { |
137
|
2 |
|
$serializedEntity = json_encode($normalizedEntity); |
138
|
|
|
} |
139
|
|
|
|
140
|
2 |
|
return new Response($serializedEntity); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.