|
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; |
|
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
|
|
|
|
|
24
|
|
|
final class GenericEntityInvokeController |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var ControllerHelperInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $controllerHelper; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var ArgumentCompilerInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $argumentCompiler; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $entityClass; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
private $methodName; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
private $arguments; |
|
51
|
|
|
|
|
52
|
8 |
|
public function __construct( |
|
53
|
|
|
ControllerHelperInterface $controllerHelper, |
|
54
|
|
|
ArgumentCompilerInterface $argumentCompiler, |
|
55
|
|
|
array $options |
|
56
|
|
|
) { |
|
57
|
8 |
|
Assert::null($this->controllerHelper); |
|
58
|
8 |
|
Assert::keyExists($options, 'entity-class'); |
|
59
|
7 |
|
Assert::keyExists($options, 'method'); |
|
60
|
|
|
|
|
61
|
6 |
|
$options = array_merge([ |
|
62
|
6 |
|
'arguments' => [] |
|
63
|
6 |
|
], $options); |
|
64
|
|
|
|
|
65
|
6 |
|
Assert::classExists($options['entity-class']); |
|
66
|
5 |
|
Assert::methodExists($options['entity-class'], $options['method']); |
|
67
|
4 |
|
Assert::isArray($options['arguments']); |
|
68
|
|
|
|
|
69
|
3 |
|
$this->controllerHelper = $controllerHelper; |
|
70
|
3 |
|
$this->argumentCompiler = $argumentCompiler; |
|
71
|
3 |
|
$this->entityClass = $options['entity-class']; |
|
72
|
3 |
|
$this->methodName = $options['method']; |
|
73
|
3 |
|
$this->arguments = $options['arguments']; |
|
74
|
3 |
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
public function invokeEntityMethod(Request $request, string $entityId): Response |
|
77
|
|
|
{ |
|
78
|
|
|
/** @var object|null $entity */ |
|
79
|
2 |
|
$entity = $this->controllerHelper->findEntity($this->entityClass, $entityId); |
|
80
|
|
|
|
|
81
|
2 |
|
if (is_null($entity)) { |
|
82
|
1 |
|
throw new InvalidArgumentException(sprintf( |
|
83
|
1 |
|
"Entity with id '%s' not found!", |
|
84
|
1 |
|
$entityId |
|
85
|
|
|
)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
$reflectionObject = new ReflectionObject($entity); |
|
89
|
|
|
|
|
90
|
|
|
/** @var ReflectionMethod $reflectionMethod */ |
|
91
|
1 |
|
$reflectionMethod = $reflectionObject->getMethod($this->methodName); |
|
92
|
|
|
|
|
93
|
|
|
/** @var array $callArguments */ |
|
94
|
1 |
|
$callArguments = $this->argumentCompiler->buildCallArguments( |
|
95
|
1 |
|
$reflectionMethod, |
|
96
|
1 |
|
$this->arguments, |
|
97
|
1 |
|
$request |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
1 |
|
$reflectionMethod->invokeArgs($entity, $callArguments); |
|
101
|
|
|
|
|
102
|
1 |
|
return new Response("Entity method invoked!"); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|