|
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 Webmozart\Assert\Assert; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
19
|
|
|
use Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface; |
|
20
|
|
|
use Addiks\SymfonyGenerics\Controllers\ApplyDataTemplateTrait; |
|
21
|
|
|
use Symfony\Component\Serializer\Encoder\EncoderInterface; |
|
22
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
|
23
|
|
|
use ErrorException; |
|
24
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
|
25
|
|
|
|
|
26
|
|
|
final class GenericEntityListingController |
|
27
|
|
|
{ |
|
28
|
|
|
use ApplyDataTemplateTrait; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var ControllerHelperInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $controllerHelper; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var ArgumentCompilerInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $argumentCompiler; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $entityClass; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
private $criteria; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var string|null |
|
52
|
|
|
*/ |
|
53
|
|
|
private $authorizationAttribute; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
private $format; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var EncoderInterface|null |
|
62
|
|
|
*/ |
|
63
|
|
|
private $encoder; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var NormalizerInterface|null |
|
67
|
|
|
*/ |
|
68
|
|
|
private $normalizer; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var array|null |
|
72
|
|
|
*/ |
|
73
|
|
|
private $dataTemplate; |
|
74
|
|
|
|
|
75
|
8 |
|
public function __construct( |
|
76
|
|
|
ControllerHelperInterface $controllerHelper, |
|
77
|
|
|
ArgumentCompilerInterface $argumentCompiler, |
|
78
|
|
|
array $options |
|
79
|
|
|
) { |
|
80
|
8 |
|
Assert::null($this->controllerHelper); |
|
81
|
8 |
|
Assert::keyExists($options, 'entity-class'); |
|
82
|
7 |
|
Assert::classExists($options['entity-class']); |
|
83
|
|
|
|
|
84
|
6 |
|
$options = array_merge([ |
|
85
|
6 |
|
'format' => 'json', |
|
86
|
|
|
'criteria' => [], |
|
87
|
|
|
'authorization-attribute' => null, |
|
88
|
|
|
'encoder' => null, |
|
89
|
|
|
'normalizer' => null, |
|
90
|
|
|
'data-template' => null, |
|
91
|
6 |
|
], $options); |
|
92
|
|
|
|
|
93
|
6 |
|
Assert::true(is_null($options['encoder']) || $options['encoder'] instanceof EncoderInterface); |
|
94
|
5 |
|
Assert::true(is_null($options['normalizer']) || $options['normalizer'] instanceof NormalizerInterface); |
|
95
|
|
|
|
|
96
|
4 |
|
$this->controllerHelper = $controllerHelper; |
|
97
|
4 |
|
$this->argumentCompiler = $argumentCompiler; |
|
98
|
4 |
|
$this->entityClass = $options['entity-class']; |
|
99
|
4 |
|
$this->criteria = $options['criteria']; |
|
100
|
4 |
|
$this->encoder = $options['encoder']; |
|
101
|
4 |
|
$this->normalizer = $options['normalizer']; |
|
102
|
4 |
|
$this->format = $options['format']; |
|
103
|
4 |
|
$this->dataTemplate = $options['data-template']; |
|
104
|
4 |
|
$this->authorizationAttribute = $options['authorization-attribute']; |
|
105
|
4 |
|
} |
|
106
|
|
|
|
|
107
|
3 |
|
public function listEntities(Request $request): Response |
|
108
|
|
|
{ |
|
109
|
3 |
|
if (!is_null($this->authorizationAttribute)) { |
|
110
|
1 |
|
$this->controllerHelper->denyAccessUnlessGranted($this->authorizationAttribute, $request); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** @var array $criteria */ |
|
114
|
2 |
|
$criteria = $this->argumentCompiler->buildArguments($this->criteria, $request); |
|
115
|
|
|
|
|
116
|
|
|
/** @var array<object> $entities */ |
|
117
|
2 |
|
$entities = $this->controllerHelper->findEntities($this->entityClass, $criteria); |
|
118
|
|
|
|
|
119
|
|
|
/** @var array<int, array> $resultData */ |
|
120
|
2 |
|
$resultEntries = array(); |
|
121
|
|
|
|
|
122
|
2 |
|
foreach ($entities as $entity) { |
|
123
|
|
|
/** @var object $entity */ |
|
124
|
|
|
|
|
125
|
|
|
/** @var array $normalizedEntity */ |
|
126
|
2 |
|
$normalizedEntity = array(); |
|
|
|
|
|
|
127
|
|
|
|
|
128
|
2 |
View Code Duplication |
if ($this->normalizer instanceof NormalizerInterface) { |
|
|
|
|
|
|
129
|
1 |
|
$normalizedEntity = $this->normalizer->normalize($entity); |
|
130
|
|
|
|
|
131
|
|
|
} else { |
|
132
|
1 |
|
$normalizer = new ObjectNormalizer(); |
|
133
|
|
|
|
|
134
|
1 |
|
$normalizedEntity = $normalizer->normalize($entity); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
2 |
|
if (!is_array($normalizedEntity)) { |
|
138
|
1 |
|
throw new ErrorException("Result of normalize process must be an array!"); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
1 |
|
if (!is_null($this->dataTemplate)) { |
|
142
|
1 |
|
$normalizedEntity = $this->applyDataTemplate($normalizedEntity, $this->dataTemplate); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
1 |
|
$resultEntries[] = $normalizedEntity; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** @var string $serializedEntity */ |
|
149
|
1 |
|
$serializedEntity = ""; |
|
|
|
|
|
|
150
|
|
|
|
|
151
|
1 |
View Code Duplication |
if ($this->encoder instanceof EncoderInterface) { |
|
|
|
|
|
|
152
|
|
|
$serializedEntity = $this->encoder->encode($resultEntries, $this->format); |
|
153
|
|
|
|
|
154
|
|
|
} else { |
|
155
|
1 |
|
$serializedEntity = json_encode($resultEntries); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
1 |
|
return new Response($serializedEntity); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.