1
|
|
|
<?php |
2
|
|
|
namespace rtens\domin; |
3
|
|
|
|
4
|
|
|
use rtens\domin\delivery\FieldRegistry; |
5
|
|
|
use rtens\domin\delivery\ParameterReader; |
6
|
|
|
use rtens\domin\execution\ExecutionResult; |
7
|
|
|
use rtens\domin\execution\FailedResult; |
8
|
|
|
use rtens\domin\execution\MissingParametersResult; |
9
|
|
|
use rtens\domin\execution\NoResult; |
10
|
|
|
use rtens\domin\execution\NotPermittedResult; |
11
|
|
|
use rtens\domin\execution\ValueResult; |
12
|
|
|
|
13
|
|
|
class Executor { |
14
|
|
|
|
15
|
|
|
/** @var ActionRegistry */ |
16
|
|
|
private $actions; |
17
|
|
|
|
18
|
|
|
/** @var FieldRegistry */ |
19
|
|
|
private $fields; |
20
|
|
|
|
21
|
|
|
/** @var ParameterReader */ |
22
|
|
|
private $paramReader; |
23
|
|
|
|
24
|
|
|
/** @var null|AccessControl */ |
25
|
|
|
private $access; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ActionRegistry $actions <- |
29
|
|
|
* @param FieldRegistry $fields <- |
30
|
|
|
* @param ParameterReader $reader <- |
31
|
|
|
*/ |
32
|
|
|
public function __construct(ActionRegistry $actions, FieldRegistry $fields, ParameterReader $reader) { |
33
|
|
|
$this->actions = $actions; |
34
|
|
|
$this->fields = $fields; |
35
|
|
|
$this->paramReader = $reader; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function restrictAccess(AccessControl $access) { |
39
|
|
|
$this->access = $access; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param $id |
44
|
|
|
* @return ExecutionResult |
45
|
|
|
*/ |
46
|
|
|
public function execute($id) { |
47
|
|
|
try { |
48
|
|
|
$action = $this->actions->getAction($id); |
49
|
|
|
|
50
|
|
|
list($params, $missing) = $this->readParameters($action); |
51
|
|
|
|
52
|
|
|
if (!empty($missing)) { |
53
|
|
|
return new MissingParametersResult($missing); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($this->access && !$this->access->isExecutionPermitted($id, $params)) { |
57
|
|
|
return new NotPermittedResult(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$returned = $action->execute($params); |
61
|
|
|
|
62
|
|
|
if (is_null($returned)) { |
63
|
|
|
return new NoResult(); |
64
|
|
|
} else if ($returned instanceof ExecutionResult) { |
65
|
|
|
return $returned; |
66
|
|
|
} else { |
67
|
|
|
return new ValueResult($returned); |
68
|
|
|
} |
69
|
|
|
} catch (\Exception $e) { |
70
|
|
|
return new FailedResult($e); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function readParameters(Action $action) { |
75
|
|
|
$failed = []; |
76
|
|
|
$params = []; |
77
|
|
|
foreach ($action->parameters() as $parameter) { |
78
|
|
|
try { |
79
|
|
|
$params[$parameter->getName()] = $this->inflate($parameter); |
80
|
|
|
} catch (\Exception $e) { |
81
|
|
|
$failed[$parameter->getName()] = $e; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return [$params, $failed]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function inflate(Parameter $parameter) { |
89
|
|
|
if ($this->paramReader->has($parameter)) { |
90
|
|
|
$inflated = $this->fields->getField($parameter) |
91
|
|
|
->inflate($parameter, $this->paramReader->read($parameter)); |
92
|
|
|
|
93
|
|
|
if ($parameter->getType()->is($inflated)) { |
94
|
|
|
return $inflated; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($parameter->isRequired()) { |
99
|
|
|
throw new \Exception("[{$parameter->getName()}] is required."); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return null; |
103
|
|
|
} |
104
|
|
|
} |