Completed
Push — master ( 4f1190...3b2a6f )
by Nikolas
03:34
created

Executor::execute()   C

Complexity

Conditions 7
Paths 14

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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