Completed
Push — master ( 312098...b95ba9 )
by Nikolas
40:06
created

ActionResult::handleNotPermittedResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->model of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
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
}