Executor::execute()   C
last analyzed

Complexity

Conditions 7
Paths 15

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
c 0
b 0
f 0
rs 6.7272
cc 7
eloc 19
nc 15
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\execution\access\AccessControl;
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\ValueResult;
13
14
class Executor {
15
16
    /** @var ActionRegistry */
17
    private $actions;
18
19
    /** @var FieldRegistry */
20
    private $fields;
21
22
    /** @var ParameterReader */
23
    private $paramReader;
24
25
    /** @var AccessControl */
26
    private $access;
27
28
    public function __construct(ActionRegistry $actions, FieldRegistry $fields, ParameterReader $reader, AccessControl $access) {
29
        $this->actions = $actions;
30
        $this->fields = $fields;
31
        $this->paramReader = $reader;
32
        $this->access = $access;
33
    }
34
35
    /**
36
     * @param $id
37
     * @return ExecutionResult
38
     */
39
    public function execute($id) {
40
        if (!$this->access->isPermitted($id)) {
41
            return new NotPermittedResult();
42
        }
43
44
        try {
45
            $action = $this->actions->getAction($id);
46
47
            list($params, $missing) = $this->readParameters($action);
48
49
            if (!empty($missing)) {
50
                return new MissingParametersResult($missing);
51
            }
52
53
            if (!$this->access->isExecutionPermitted($id, $params)) {
54
                return new NotPermittedResult();
55
            }
56
57
            $returned = $action->execute($params);
58
59
            if (is_null($returned)) {
60
                return new NoResult();
61
            } else if ($returned instanceof ExecutionResult) {
62
                return $returned;
63
            } else {
64
                return new ValueResult($returned);
65
            }
66
        } catch (\Exception $e) {
67
            return new FailedResult($e);
68
        }
69
    }
70
71
    private function readParameters(Action $action) {
72
        $failed = [];
73
        $params = [];
74
        foreach ($action->parameters() as $parameter) {
75
            try {
76
                if ($this->paramReader->has($parameter)) {
77
                    $inflated = $this->fields->getField($parameter)
78
                        ->inflate($parameter, $this->paramReader->read($parameter));
79
80
                    if ($parameter->getType()->is($inflated)) {
81
                        $params[$parameter->getName()] = $inflated;
82
                        continue;
83
                    }
84
                }
85
86
                if ($parameter->isRequired()) {
87
                    throw new \Exception("[{$parameter->getName()}] is required.");
88
                }
89
            } catch (\Exception $e) {
90
                $failed[$parameter->getName()] = $e;
91
            }
92
        }
93
94
        return [$params, $failed];
95
    }
96
}