|
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'] = htmlentities($result->getMessage()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function handleMissingParametersResult(MissingParametersResult $result) { |
|
83
|
|
|
$this->model['missing'] = $result->getMissingNames(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function handleNoResult() { |
|
87
|
|
|
$this->model['success'] = true; |
|
88
|
|
|
|
|
89
|
|
|
if ($this->crumbs->hasCrumbs()) { |
|
90
|
|
|
$this->model['redirect'] = $this->crumbs->getLastCrumb()->getTarget(); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
protected function handleNotPermittedResult() { |
|
95
|
|
|
$this->model['error'] = 'You are not permitted to execute this action.'; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
protected function handleRedirectResult(RedirectResult $result) { |
|
99
|
|
|
$this->model['success'] = true; |
|
100
|
|
|
$this->model['redirect'] = $result->getUrl(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function handleValueResult(ValueResult $result) { |
|
104
|
|
|
$value = $result->getValue(); |
|
105
|
|
|
$renderer = $this->renderers->getRenderer($value); |
|
106
|
|
|
|
|
107
|
|
|
if ($renderer instanceof WebRenderer) { |
|
108
|
|
|
$this->headElements = $renderer->headElements($value); |
|
109
|
|
|
} |
|
110
|
|
|
$this->model['output'] = $renderer->render($value); |
|
111
|
|
|
|
|
112
|
|
|
if (!$this->action->isModifying()) { |
|
113
|
|
|
$this->crumbs->updateCrumbs($this->action, $this->actionId); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
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.