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
|
10 |
View Code Duplication |
public function __construct( |
|
|
|
|
76
|
|
|
ControllerHelperInterface $controllerHelper, |
77
|
|
|
ArgumentCompilerInterface $argumentCompiler, |
78
|
|
|
array $options |
79
|
|
|
) { |
80
|
10 |
|
Assert::null($this->controllerHelper); |
81
|
10 |
|
Assert::keyExists($options, 'entity-class'); |
82
|
9 |
|
Assert::classExists($options['entity-class']); |
83
|
|
|
|
84
|
8 |
|
$options = array_merge([ |
85
|
8 |
|
'format' => 'json', |
86
|
|
|
'criteria' => [], |
87
|
|
|
'authorization-attribute' => null, |
88
|
|
|
'encoder' => null, |
89
|
|
|
'normalizer' => null, |
90
|
|
|
'data-template' => null, |
91
|
8 |
|
], $options); |
92
|
|
|
|
93
|
8 |
|
Assert::true(is_null($options['encoder']) || $options['encoder'] instanceof EncoderInterface); |
94
|
7 |
|
Assert::true(is_null($options['normalizer']) || $options['normalizer'] instanceof NormalizerInterface); |
95
|
|
|
|
96
|
6 |
|
$this->controllerHelper = $controllerHelper; |
97
|
6 |
|
$this->argumentCompiler = $argumentCompiler; |
98
|
6 |
|
$this->entityClass = $options['entity-class']; |
99
|
6 |
|
$this->criteria = $options['criteria']; |
100
|
6 |
|
$this->encoder = $options['encoder']; |
101
|
6 |
|
$this->normalizer = $options['normalizer']; |
102
|
6 |
|
$this->format = $options['format']; |
103
|
6 |
|
$this->dataTemplate = $options['data-template']; |
104
|
6 |
|
$this->authorizationAttribute = $options['authorization-attribute']; |
105
|
6 |
|
} |
106
|
|
|
|
107
|
2 |
View Code Duplication |
public function __invoke(): Response |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
/** @var Request $request */ |
110
|
2 |
|
$request = $this->controllerHelper->getCurrentRequest(); |
111
|
|
|
|
112
|
2 |
|
Assert::isInstanceOf($request, Request::class, "Cannot use controller outside of request-scope!"); |
113
|
|
|
|
114
|
1 |
|
return $this->listEntities($request); |
115
|
|
|
} |
116
|
|
|
|
117
|
4 |
|
public function listEntities(Request $request): Response |
118
|
|
|
{ |
119
|
4 |
|
if (!is_null($this->authorizationAttribute)) { |
120
|
1 |
|
$this->controllerHelper->denyAccessUnlessGranted($this->authorizationAttribute, $request); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** @var array $criteria */ |
124
|
3 |
|
$criteria = $this->argumentCompiler->buildArguments($this->criteria, $request); |
125
|
|
|
|
126
|
|
|
/** @var array<object> $entities */ |
127
|
3 |
|
$entities = $this->controllerHelper->findEntities($this->entityClass, $criteria); |
128
|
|
|
|
129
|
|
|
/** @var array<int, array> $resultData */ |
130
|
3 |
|
$resultEntries = array(); |
131
|
|
|
|
132
|
3 |
|
foreach ($entities as $entity) { |
133
|
|
|
/** @var object $entity */ |
134
|
|
|
|
135
|
|
|
/** @var array $normalizedEntity */ |
136
|
3 |
|
$normalizedEntity = array(); |
|
|
|
|
137
|
|
|
|
138
|
3 |
View Code Duplication |
if ($this->normalizer instanceof NormalizerInterface) { |
|
|
|
|
139
|
1 |
|
$normalizedEntity = $this->normalizer->normalize($entity); |
140
|
|
|
|
141
|
|
|
} else { |
142
|
2 |
|
$normalizer = new ObjectNormalizer(); |
143
|
|
|
|
144
|
2 |
|
$normalizedEntity = $normalizer->normalize($entity); |
145
|
|
|
} |
146
|
|
|
|
147
|
3 |
|
if (!is_array($normalizedEntity)) { |
148
|
1 |
|
throw new ErrorException("Result of normalize process must be an array!"); |
149
|
|
|
} |
150
|
|
|
|
151
|
2 |
|
if (!is_null($this->dataTemplate)) { |
152
|
2 |
|
$normalizedEntity = $this->applyDataTemplate($normalizedEntity, $this->dataTemplate); |
153
|
|
|
} |
154
|
|
|
|
155
|
2 |
|
$resultEntries[] = $normalizedEntity; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** @var string $serializedEntity */ |
159
|
2 |
|
$serializedEntity = ""; |
|
|
|
|
160
|
|
|
|
161
|
2 |
View Code Duplication |
if ($this->encoder instanceof EncoderInterface) { |
|
|
|
|
162
|
|
|
$serializedEntity = $this->encoder->encode($resultEntries, $this->format); |
163
|
|
|
|
164
|
|
|
} else { |
165
|
2 |
|
$serializedEntity = json_encode($resultEntries); |
166
|
|
|
} |
167
|
|
|
|
168
|
2 |
|
return new Response($serializedEntity); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
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.