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