1
|
|
|
<?php |
2
|
|
|
namespace rtens\domin\delivery\web; |
3
|
|
|
|
4
|
|
|
use rtens\domin\Action; |
5
|
|
|
use rtens\domin\delivery\RendererRegistry; |
6
|
|
|
use rtens\domin\execution\FailedResult; |
7
|
|
|
use rtens\domin\execution\MissingParametersResult; |
8
|
|
|
use rtens\domin\execution\RedirectResult; |
9
|
|
|
use rtens\domin\execution\ValueResult; |
10
|
|
|
use rtens\domin\Executor; |
11
|
|
|
|
12
|
|
|
class ActionResult { |
13
|
|
|
|
14
|
|
|
/** @var array */ |
15
|
|
|
private $model; |
16
|
|
|
|
17
|
|
|
/** @var Element[] */ |
18
|
|
|
private $headElements = []; |
19
|
|
|
|
20
|
|
|
/** @var Action */ |
21
|
|
|
private $action; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
private $actionId; |
25
|
|
|
|
26
|
|
|
/** @var BreadCrumbsTrail */ |
27
|
|
|
private $crumbs; |
28
|
|
|
|
29
|
|
|
/** @var Executor */ |
30
|
|
|
private $executor; |
31
|
|
|
|
32
|
|
|
/** @var RendererRegistry */ |
33
|
|
|
private $renderers; |
34
|
|
|
|
35
|
|
|
public function __construct(Executor $executor, RendererRegistry $renderers, Action $action, $actionId, BreadCrumbsTrail $crumbs) { |
36
|
|
|
$this->action = $action; |
37
|
|
|
$this->actionId = $actionId; |
38
|
|
|
$this->crumbs = $crumbs; |
39
|
|
|
$this->executor = $executor; |
40
|
|
|
$this->renderers = $renderers; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getModel() { |
44
|
|
|
$this->executeFirst(); |
45
|
|
|
return $this->model; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getHeadElements() { |
49
|
|
|
$this->executeFirst(); |
50
|
|
|
return $this->headElements; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function wasExecuted() { |
54
|
|
|
$this->executeFirst(); |
55
|
|
|
return !$this->model['error'] && !$this->model['missing']; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function executeFirst() { |
59
|
|
|
if (!$this->model) { |
|
|
|
|
60
|
|
|
$this->execute(); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function execute() { |
65
|
|
|
$result = $this->executor->execute($this->actionId); |
66
|
|
|
|
67
|
|
|
$this->model = [ |
68
|
|
|
'error' => null, |
69
|
|
|
'missing' => null, |
70
|
|
|
'success' => null, |
71
|
|
|
'redirect' => null, |
72
|
|
|
'output' => null |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
call_user_func([$this, 'handle' . (new \ReflectionClass($result))->getShortName()], $result); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function handleFailedResult(FailedResult $result) { |
79
|
|
|
$this->model['error']['message'] = htmlentities($result->getMessage()); |
80
|
|
|
$this->model['error']['details'] = htmlentities($result->getDetails()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function handleMissingParametersResult(MissingParametersResult $result) { |
84
|
|
|
$this->model['missing'] = $result->getMissingNames(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function handleNoResult() { |
88
|
|
|
$this->model['success'] = true; |
89
|
|
|
|
90
|
|
|
if ($this->crumbs->hasCrumbs()) { |
91
|
|
|
$this->model['redirect'] = $this->crumbs->getLastCrumb()->getTarget(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function handleNotPermittedResult() { |
96
|
|
|
$this->model['error'] = 'You are not permitted to execute this action.'; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function handleRedirectResult(RedirectResult $result) { |
100
|
|
|
$this->model['success'] = true; |
101
|
|
|
$this->model['redirect'] = $result->getUrl(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected function handleValueResult(ValueResult $result) { |
105
|
|
|
$value = $result->getValue(); |
106
|
|
|
$renderer = $this->renderers->getRenderer($value); |
107
|
|
|
|
108
|
|
|
if ($renderer instanceof WebRenderer) { |
109
|
|
|
$this->headElements = $renderer->headElements($value); |
110
|
|
|
} |
111
|
|
|
$this->model['output'] = $renderer->render($value); |
112
|
|
|
|
113
|
|
|
if (!$this->action->isModifying()) { |
114
|
|
|
$this->crumbs->updateCrumbs($this->action, $this->actionId); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.