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 $methodName; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
private $arguments; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string|null |
55
|
|
|
*/ |
56
|
|
|
private $denyAccessAttribute; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
private $successMessage; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string|null |
65
|
|
|
*/ |
66
|
|
|
private $redirectRoute; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var array |
70
|
|
|
*/ |
71
|
|
|
private $redirectRouteParameters; |
72
|
|
|
|
73
|
9 |
|
public function __construct( |
74
|
|
|
ControllerHelperInterface $controllerHelper, |
75
|
|
|
ArgumentCompilerInterface $argumentCompiler, |
76
|
|
|
array $options |
77
|
|
|
) { |
78
|
9 |
|
Assert::null($this->controllerHelper); |
79
|
9 |
|
Assert::keyExists($options, 'entity-class'); |
80
|
8 |
|
Assert::keyExists($options, 'method'); |
81
|
|
|
|
82
|
7 |
|
$options = array_merge([ |
83
|
7 |
|
'arguments' => [], |
84
|
|
|
'deny-access-attribute' => null, |
85
|
|
|
'success-message' => "Entity method invoked!", |
86
|
|
|
'redirect-route' => null, |
87
|
|
|
'redirect-route-parameters' => [], |
88
|
7 |
|
], $options); |
89
|
|
|
|
90
|
7 |
|
Assert::classExists($options['entity-class']); |
91
|
6 |
|
Assert::methodExists($options['entity-class'], $options['method']); |
92
|
5 |
|
Assert::isArray($options['arguments'], 'Method-arguments must be array!'); |
93
|
|
|
|
94
|
4 |
|
$this->controllerHelper = $controllerHelper; |
95
|
4 |
|
$this->argumentCompiler = $argumentCompiler; |
96
|
4 |
|
$this->entityClass = $options['entity-class']; |
97
|
4 |
|
$this->methodName = $options['method']; |
98
|
4 |
|
$this->arguments = $options['arguments']; |
99
|
4 |
|
$this->denyAccessAttribute = $options['deny-access-attribute']; |
100
|
4 |
|
$this->successMessage = $options['success-message']; |
101
|
4 |
|
$this->redirectRoute = $options['redirect-route']; |
102
|
4 |
|
$this->redirectRouteParameters = $options['redirect-route-parameters']; |
103
|
4 |
|
} |
104
|
|
|
|
105
|
3 |
|
public function invokeEntityMethod(Request $request, string $entityId): Response |
106
|
|
|
{ |
107
|
|
|
/** @var object|null $entity */ |
108
|
3 |
|
$entity = $this->controllerHelper->findEntity($this->entityClass, $entityId); |
109
|
|
|
|
110
|
3 |
|
if (is_null($entity)) { |
111
|
1 |
|
throw new InvalidArgumentException(sprintf( |
112
|
1 |
|
"Entity with id '%s' not found!", |
113
|
1 |
|
$entityId |
114
|
|
|
)); |
115
|
|
|
} |
116
|
|
|
|
117
|
2 |
|
if (!empty($this->denyAccessAttribute)) { |
118
|
1 |
|
$this->controllerHelper->denyAccessUnlessGranted($this->denyAccessAttribute, $entity); |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
$reflectionObject = new ReflectionObject($entity); |
122
|
|
|
|
123
|
|
|
/** @var ReflectionMethod $reflectionMethod */ |
124
|
1 |
|
$reflectionMethod = $reflectionObject->getMethod($this->methodName); |
125
|
|
|
|
126
|
|
|
/** @var array $callArguments */ |
127
|
1 |
|
$callArguments = $this->argumentCompiler->buildCallArguments( |
128
|
1 |
|
$reflectionMethod, |
129
|
1 |
|
$this->arguments, |
130
|
1 |
|
$request |
131
|
|
|
); |
132
|
|
|
|
133
|
1 |
|
$this->controllerHelper->dispatchEvent("symfony_generics.entity_interaction", new EntityInteractionEvent( |
134
|
1 |
|
$this->entityClass, |
135
|
1 |
|
$entityId, |
136
|
1 |
|
$entity, |
137
|
1 |
|
$this->methodName, |
138
|
1 |
|
$callArguments |
139
|
|
|
)); |
140
|
|
|
|
141
|
|
|
/** @var mixed $result */ |
142
|
1 |
|
$result = $reflectionMethod->invokeArgs($entity, $callArguments); |
143
|
|
|
|
144
|
1 |
|
$this->controllerHelper->flushORM(); |
145
|
|
|
|
146
|
|
|
/** @var Response $response */ |
147
|
1 |
|
$response = null; |
|
|
|
|
148
|
|
|
|
149
|
1 |
|
if (is_null($this->redirectRoute)) { |
150
|
1 |
|
$response = new Response($this->successMessage); |
151
|
|
|
|
152
|
|
|
} else { |
153
|
|
|
$response = $this->controllerHelper->redirectToRoute( |
154
|
|
|
$this->redirectRoute, |
155
|
|
|
$this->argumentCompiler->buildArguments($this->redirectRouteParameters, $request, [ |
156
|
|
|
'result' => $result |
157
|
|
|
]) |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
return $response; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
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.