1 | <?php |
||
29 | final class GenericEntityCreateController |
||
30 | { |
||
31 | |||
32 | /** |
||
33 | * @var ControllerHelperInterface |
||
34 | */ |
||
35 | private $controllerHelper; |
||
36 | |||
37 | /** |
||
38 | * @var ContainerInterface |
||
39 | */ |
||
40 | private $container; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | private $entityClass; |
||
46 | |||
47 | /** |
||
48 | * @var array<string, array<string, mixed>> |
||
49 | */ |
||
50 | private $calls = array(); |
||
51 | |||
52 | /** |
||
53 | * @var string|null |
||
54 | */ |
||
55 | private $factory = null; |
||
56 | |||
57 | /** |
||
58 | * @var array<string, mixed> |
||
59 | */ |
||
60 | private $constructArguments = array(); |
||
61 | |||
62 | /** |
||
63 | * @var ArgumentCompilerInterface |
||
64 | */ |
||
65 | private $argumentBuilder; |
||
66 | |||
67 | /** |
||
68 | * @var string |
||
69 | */ |
||
70 | private $successResponse; |
||
71 | |||
72 | /** |
||
73 | * @var string|null |
||
74 | */ |
||
75 | private $authorizationAttribute; |
||
76 | |||
77 | /** |
||
78 | * @var string|null |
||
79 | */ |
||
80 | private $successRedirectRoute; |
||
81 | |||
82 | /** |
||
83 | * @var array |
||
84 | */ |
||
85 | private $successRedirectArguments; |
||
86 | |||
87 | /** |
||
88 | * @var int |
||
89 | */ |
||
90 | private $successRedirectStatus; |
||
91 | |||
92 | /** |
||
93 | * @var string |
||
94 | */ |
||
95 | private $entityIdKey; |
||
96 | |||
97 | /** |
||
98 | * @var string |
||
99 | */ |
||
100 | private $entityIdGetter; |
||
101 | |||
102 | 21 | public function __construct( |
|
150 | |||
151 | 2 | public function __invoke(): Response |
|
160 | |||
161 | 15 | public function createEntity(Request $request): Response |
|
232 | |||
233 | /** |
||
234 | * @param object $factoryObject |
||
235 | */ |
||
236 | 15 | private function findConstructorReflection(&$factoryObject = null): ?ReflectionFunctionAbstract |
|
237 | { |
||
238 | /** @var ReflectionFunctionAbstract|null $constructorReflection */ |
||
239 | 15 | $constructorReflection = null; |
|
240 | |||
241 | 15 | if (!empty($this->factory)) { |
|
242 | 9 | if (is_int(strpos($this->factory, '::'))) { |
|
243 | 7 | [$factoryClass, $factoryMethod] = explode('::', $this->factory, 2); |
|
244 | |||
245 | 7 | if (!empty($factoryClass)) { |
|
246 | 6 | if ($factoryClass[0] == '@') { |
|
247 | # Create by factory-service-object |
||
248 | |||
249 | 6 | $factoryObject = $this->container->get(substr($factoryClass, 1)); |
|
250 | |||
251 | 6 | Assert::object($factoryObject, sprintf( |
|
252 | 6 | "Did not find service '%s'!", |
|
253 | 6 | substr($factoryClass, 1) |
|
254 | )); |
||
255 | |||
256 | 5 | $constructorReflection = (new ReflectionObject($factoryObject))->getMethod($factoryMethod); |
|
257 | |||
258 | } else { |
||
259 | # Create by static factory-method of other class |
||
260 | |||
261 | 3 | $constructorReflection = (new ReflectionClass($factoryClass))->getMethod($factoryMethod); |
|
262 | } |
||
263 | |||
264 | } else { |
||
265 | 1 | throw new ErrorException(sprintf( |
|
266 | 1 | "Invalid constructor definition: '%s'!", |
|
267 | 4 | $this->factory |
|
268 | )); |
||
269 | } |
||
270 | |||
271 | 2 | } elseif (method_exists($this->entityClass, $this->factory)) { |
|
272 | # Create by static factory method on entity class |
||
273 | |||
274 | 2 | $constructorReflection = (new ReflectionClass($this->entityClass))->getMethod($this->factory); |
|
275 | |||
276 | } elseif (function_exists($this->factory)) { |
||
277 | # Create by factory function |
||
278 | |||
279 | 5 | $constructorReflection = new ReflectionFunction($this->factory); |
|
280 | } |
||
281 | |||
282 | } else { |
||
283 | # Create by calling the constructor directly |
||
284 | |||
285 | 6 | $constructorReflection = (new ReflectionClass($this->entityClass))->getConstructor(); |
|
286 | } |
||
287 | |||
288 | 11 | return $constructorReflection; |
|
289 | } |
||
290 | |||
291 | /** |
||
292 | * @param object|null $factoryObject |
||
293 | * |
||
294 | * @return object |
||
295 | */ |
||
296 | 10 | private function createEntityByConstructor( |
|
297 | ReflectionFunctionAbstract $constructorReflection, |
||
298 | array $constructArguments, |
||
299 | $factoryObject |
||
300 | ) { |
||
301 | /** @var object|null $entity */ |
||
302 | 10 | $entity = null; |
|
303 | |||
304 | 10 | if ($constructorReflection instanceof ReflectionMethod) { |
|
305 | 10 | if ($constructorReflection->isConstructor()) { |
|
306 | 5 | $entity = $constructorReflection->getDeclaringClass()->newInstanceArgs($constructArguments); |
|
307 | |||
308 | 5 | } elseif ($constructorReflection->isStatic()) { |
|
309 | 2 | $entity = $constructorReflection->invokeArgs(null, $constructArguments); |
|
310 | |||
311 | } else { |
||
312 | 10 | $entity = $constructorReflection->invokeArgs($factoryObject, $constructArguments); |
|
313 | } |
||
314 | |||
315 | } elseif ($constructorReflection instanceof ReflectionFunction) { |
||
316 | $entity = $constructorReflection->invokeArgs($constructArguments); |
||
317 | } |
||
318 | |||
319 | 10 | Assert::isInstanceOf($entity, $this->entityClass); |
|
320 | |||
321 | 9 | return $entity; |
|
322 | } |
||
323 | |||
324 | /** |
||
325 | * @param object $entity |
||
326 | */ |
||
327 | 10 | private function performPostCreationCalls($entity, Request $request): void |
|
346 | |||
347 | } |
||
348 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.