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 Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface; |
17
|
|
|
use Webmozart\Assert\Assert; |
18
|
|
|
use InvalidArgumentException; |
19
|
|
|
use ReflectionObject; |
20
|
|
|
use ReflectionMethod; |
21
|
|
|
use Symfony\Component\HttpFoundation\Request; |
22
|
|
|
use Symfony\Component\HttpFoundation\Response; |
23
|
|
|
use Addiks\SymfonyGenerics\Events\EntityInteractionEvent; |
24
|
|
|
|
25
|
|
|
final class GenericEntityInvokeController |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ControllerHelperInterface |
30
|
|
|
*/ |
31
|
|
|
private $controllerHelper; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ArgumentCompilerInterface |
35
|
|
|
*/ |
36
|
|
|
private $argumentCompiler; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $entityClass; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $entityIdKey; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
private $entityIdSource; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
private $methodName; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
private $arguments; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string|null |
65
|
|
|
*/ |
66
|
|
|
private $denyAccessAttribute; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
private $successMessage; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var string|null |
75
|
|
|
*/ |
76
|
|
|
private $redirectRoute; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var array |
80
|
|
|
*/ |
81
|
|
|
private $redirectRouteParameters; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var bool |
85
|
|
|
*/ |
86
|
|
|
private $sendReturnValueInResponse = false; |
87
|
|
|
|
88
|
12 |
|
public function __construct( |
89
|
|
|
ControllerHelperInterface $controllerHelper, |
90
|
|
|
ArgumentCompilerInterface $argumentCompiler, |
91
|
|
|
array $options |
92
|
|
|
) { |
93
|
12 |
|
Assert::null($this->controllerHelper); |
94
|
12 |
|
Assert::keyExists($options, 'entity-class'); |
95
|
11 |
|
Assert::keyExists($options, 'method'); |
96
|
|
|
|
97
|
10 |
|
$options = array_merge([ |
98
|
10 |
|
'arguments' => [], |
99
|
|
|
'deny-access-attribute' => null, |
100
|
|
|
'success-message' => "Entity method invoked!", |
101
|
|
|
'redirect-route' => null, |
102
|
|
|
'redirect-route-parameters' => [], |
103
|
|
|
'entity-id-key' => 'entityId', |
104
|
|
|
'entity-id-source' => 'request', |
105
|
|
|
'send-return-value-in-response' => false, |
106
|
10 |
|
], $options); |
107
|
|
|
|
108
|
10 |
|
Assert::classExists($options['entity-class']); |
109
|
9 |
|
Assert::methodExists($options['entity-class'], $options['method']); |
110
|
8 |
|
Assert::isArray($options['arguments'], 'Method-arguments must be array!'); |
111
|
7 |
|
Assert::oneOf($options['entity-id-source'], ['request', 'argument']); |
112
|
|
|
|
113
|
7 |
|
$this->controllerHelper = $controllerHelper; |
114
|
7 |
|
$this->argumentCompiler = $argumentCompiler; |
115
|
7 |
|
$this->entityClass = $options['entity-class']; |
116
|
7 |
|
$this->entityIdKey = $options['entity-id-key']; |
117
|
7 |
|
$this->entityIdSource = $options['entity-id-source']; |
118
|
7 |
|
$this->methodName = $options['method']; |
119
|
7 |
|
$this->arguments = $options['arguments']; |
120
|
7 |
|
$this->denyAccessAttribute = $options['deny-access-attribute']; |
121
|
7 |
|
$this->successMessage = $options['success-message']; |
122
|
7 |
|
$this->redirectRoute = $options['redirect-route']; |
123
|
7 |
|
$this->redirectRouteParameters = $options['redirect-route-parameters']; |
124
|
7 |
|
$this->sendReturnValueInResponse = $options['send-return-value-in-response']; |
125
|
7 |
|
} |
126
|
|
|
|
127
|
2 |
|
public function __invoke(): Response |
128
|
|
|
{ |
129
|
|
|
/** @var Request $request */ |
130
|
2 |
|
$request = $this->controllerHelper->getCurrentRequest(); |
131
|
|
|
|
132
|
2 |
|
Assert::isInstanceOf($request, Request::class, "Cannot use controller outside of request-scope!"); |
133
|
|
|
|
134
|
|
|
/** @var Response $response */ |
135
|
1 |
|
$response = null; |
136
|
|
|
|
137
|
1 |
|
if ($this->entityIdSource === 'request') { |
138
|
|
|
/** @var string $entityId */ |
139
|
1 |
|
$entityId = $request->get($this->entityIdKey); |
140
|
|
|
|
141
|
1 |
|
$response = $this->invokeEntityMethod($entityId); |
142
|
|
|
|
143
|
|
|
} elseif ($this->entityIdSource === 'argument') { |
144
|
|
|
$response = $this->invokeEntityMethod(''); |
145
|
|
|
} |
146
|
|
|
|
147
|
1 |
|
return $response; |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
5 |
|
public function invokeEntityMethod(string $entityId): Response |
152
|
|
|
{ |
153
|
|
|
/** @var object|null $entity */ |
154
|
5 |
|
$entity = null; |
155
|
|
|
|
156
|
5 |
|
if ($this->entityIdSource === 'request') { |
157
|
5 |
|
$entity = $this->controllerHelper->findEntity($this->entityClass, $entityId); |
158
|
5 |
|
Assert::object($entity, sprintf("Entity with id '%s' not found!", $entityId)); |
159
|
|
|
|
160
|
|
|
} elseif ($this->entityIdSource === 'argument') { |
161
|
|
|
$entity = $this->argumentCompiler->buildArgument($this->entityIdKey); |
162
|
|
|
Assert::object($entity, "Entity not found!"); |
163
|
|
|
} |
164
|
|
|
|
165
|
4 |
|
Assert::isInstanceOf($entity, $this->entityClass, sprintf( |
166
|
4 |
|
"Found entity is not of expected class '%s', but of class '%s' instead!", |
167
|
4 |
|
$this->entityClass, |
168
|
4 |
|
get_class($entity) |
169
|
|
|
)); |
170
|
|
|
|
171
|
4 |
|
if (!empty($this->denyAccessAttribute)) { |
172
|
1 |
|
$this->controllerHelper->denyAccessUnlessGranted($this->denyAccessAttribute, $entity); |
173
|
|
|
} |
174
|
|
|
|
175
|
3 |
|
$reflectionObject = new ReflectionObject($entity); |
176
|
|
|
|
177
|
|
|
/** @var ReflectionMethod $reflectionMethod */ |
178
|
3 |
|
$reflectionMethod = $reflectionObject->getMethod($this->methodName); |
179
|
|
|
|
180
|
|
|
/** @var array $callArguments */ |
181
|
3 |
|
$callArguments = $this->argumentCompiler->buildCallArguments( |
182
|
3 |
|
$reflectionMethod, |
183
|
3 |
|
$this->arguments |
184
|
|
|
); |
185
|
|
|
|
186
|
3 |
|
$this->controllerHelper->dispatchEvent("symfony_generics.entity_interaction", new EntityInteractionEvent( |
187
|
3 |
|
$this->entityClass, |
188
|
3 |
|
$entityId, |
189
|
3 |
|
$entity, |
190
|
3 |
|
$this->methodName, |
191
|
3 |
|
$callArguments |
192
|
|
|
)); |
193
|
|
|
|
194
|
|
|
/** @var mixed $result */ |
195
|
3 |
|
$result = $reflectionMethod->invokeArgs($entity, $callArguments); |
196
|
|
|
|
197
|
3 |
|
$this->controllerHelper->flushORM(); |
198
|
|
|
|
199
|
|
|
/** @var Response $response */ |
200
|
3 |
|
$response = null; |
|
|
|
|
201
|
|
|
|
202
|
3 |
|
if ($this->sendReturnValueInResponse) { |
203
|
|
|
return new Response((string)$result); |
204
|
|
|
|
205
|
3 |
|
} elseif (is_null($this->redirectRoute)) { |
206
|
2 |
|
$response = new Response($this->successMessage); |
207
|
|
|
|
208
|
|
|
} else { |
209
|
1 |
|
$response = $this->controllerHelper->redirectToRoute( |
210
|
1 |
|
$this->redirectRoute, |
211
|
1 |
|
$this->argumentCompiler->buildArguments($this->redirectRouteParameters, [ |
212
|
1 |
|
'result' => $result |
213
|
|
|
]) |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
|
217
|
3 |
|
return $response; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
} |
221
|
|
|
|
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.