1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
namespace Xervice\Controller\Business\Model\ResponseHandler; |
6
|
|
|
use Xervice\Controller\Business\Exception\ControllerException; |
7
|
|
|
use Xervice\Core\Plugin\AbstractBusinessPlugin; |
8
|
|
|
use Xervice\Web\Business\Model\Executor\ResponseHandler\ResponseHandlerInterface; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @method \Xervice\Controller\Business\ControllerBusinessFactory getFactory() |
13
|
|
|
*/ |
14
|
|
|
class ControllerResponseHandler extends AbstractBusinessPlugin implements ResponseHandlerInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param mixed $response |
18
|
|
|
* |
19
|
|
|
* @throws \Xervice\Controller\Business\Exception\ControllerException |
20
|
|
|
*/ |
21
|
6 |
|
public function handleResponse($response): void |
22
|
|
|
{ |
23
|
6 |
|
$controllerName = $response['_controller']; |
24
|
6 |
|
$action = $response['_action']; |
25
|
|
|
|
26
|
6 |
|
$this->validateController($controllerName); |
27
|
|
|
|
28
|
5 |
|
$controller = new $controllerName(); |
29
|
5 |
|
if (method_exists($controller, 'setKernel')) { |
30
|
5 |
|
$controller->setKernel($this->getFactory()->createKernelBridge()); |
31
|
|
|
} |
32
|
|
|
|
33
|
5 |
|
$this->validateAction($controller, $action, $controllerName); |
34
|
|
|
|
35
|
4 |
|
$response = $this->cleanResponse($response); |
36
|
|
|
|
37
|
4 |
|
$this->getFactory()->createOutputProcessor()->processOutput( |
38
|
4 |
|
$controller->$action( |
39
|
4 |
|
$this->getFactory()->createSymfonyRequest(), |
40
|
4 |
|
...array_values($response) |
41
|
|
|
) |
42
|
|
|
); |
43
|
4 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param array $response |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
4 |
|
private function cleanResponse(array $response): array |
51
|
|
|
{ |
52
|
4 |
|
return array_filter( |
53
|
4 |
|
$response, |
54
|
|
|
function ($value) { |
55
|
4 |
|
return $value{0} !== '_'; |
56
|
4 |
|
}, |
57
|
4 |
|
ARRAY_FILTER_USE_KEY |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $controllerName |
63
|
|
|
* |
64
|
|
|
* @throws \Xervice\Controller\Business\Exception\ControllerException |
65
|
|
|
*/ |
66
|
6 |
|
private function validateController(string $controllerName): void |
67
|
|
|
{ |
68
|
6 |
|
if (!class_exists($controllerName)) { |
69
|
1 |
|
throw new ControllerException( |
70
|
1 |
|
sprintf( |
71
|
1 |
|
'Controller %s was not found', |
72
|
1 |
|
$controllerName |
73
|
|
|
) |
74
|
|
|
); |
75
|
|
|
} |
76
|
5 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param mixed $controller |
80
|
|
|
* @param string $action |
81
|
|
|
* @param string $controllerName |
82
|
|
|
* |
83
|
|
|
* @throws \Xervice\Controller\Business\Exception\ControllerException |
84
|
|
|
*/ |
85
|
5 |
|
private function validateAction($controller, string $action, string $controllerName): void |
86
|
|
|
{ |
87
|
5 |
|
if (!method_exists($controller, $action)) { |
88
|
1 |
|
throw new ControllerException( |
89
|
1 |
|
sprintf( |
90
|
1 |
|
'Method %s not found in controller %s', |
91
|
1 |
|
$action, |
92
|
1 |
|
$controllerName |
93
|
|
|
) |
94
|
|
|
); |
95
|
|
|
} |
96
|
4 |
|
} |
97
|
|
|
|
98
|
|
|
} |