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

ExecutionResource   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
c 2
b 0
f 0
lcom 1
cbo 10
dl 0
loc 99
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A baseHeadElements() 0 8 1
A handleGet() 0 3 1
A handlePost() 0 3 1
B doExecute() 0 29 5
A getAction() 0 7 2
A assembleBreadCrumbs() 0 8 1
1
<?php
2
namespace rtens\domin\delivery\web\resources;
3
4
use rtens\domin\Action;
5
use rtens\domin\delivery\ParameterReader;
6
use rtens\domin\delivery\web\ActionForm;
7
use rtens\domin\delivery\web\ActionResult;
8
use rtens\domin\delivery\web\BreadCrumb;
9
use rtens\domin\delivery\web\BreadCrumbsTrail;
10
use rtens\domin\delivery\web\HeadElements;
11
use rtens\domin\delivery\web\WebApplication;
12
use rtens\domin\Executor;
13
14
class ExecutionResource {
15
16
    const FORCE_ARG = '__force';
17
18
    /** @var WebApplication */
19
    private $app;
20
21
    /** @var BreadCrumbsTrail */
22
    private $crumbs;
23
24
    /** @var ParameterReader */
25
    private $reader;
26
27
    public function __construct(WebApplication $app, ParameterReader $reader, BreadCrumbsTrail $crumbs) {
28
        $this->app = $app;
29
        $this->crumbs = $crumbs;
30
        $this->reader = $reader;
31
    }
32
33
    private static function baseHeadElements() {
34
        return [
35
            HeadElements::jquery(),
36
            HeadElements::jqueryUi(), // not actually needed but it needs to be included before bootstrap.js too avoid conflicts
37
            HeadElements::bootstrap(),
38
            HeadElements::bootstrapJs(),
39
        ];
40
    }
41
42
    /**
43
     * @param string $actionId
44
     * @param bool $force
45
     * @return string
46
     * @throws \Exception
47
     */
48
    public function handleGet($actionId, $force = false) {
49
        return $this->doExecute($actionId, $force);
50
    }
51
52
    /**
53
     * @param string $actionId
54
     * @return string
55
     * @throws \Exception
56
     */
57
    public function handlePost($actionId) {
58
        return $this->doExecute($actionId, true);
59
    }
60
61
    private function doExecute($actionId, $mayBeModifying = false) {
62
        $action = $this->getAction($actionId);
63
        $headElements = self::baseHeadElements();
64
65
        $form = new ActionForm($this->reader, $this->app->fields, $action, $actionId);
66
        $headElements = array_merge($headElements, $form->getHeadElements());
67
68
        if ($mayBeModifying || !$action->isModifying()) {
69
            $executor = new Executor($this->app->actions, $this->app->fields, $this->reader);
70
            $result = new ActionResult($executor, $this->app->renderers, $action, $actionId, $this->crumbs);
71
            $headElements = array_merge($headElements, $result->getHeadElements());
72
        }
73
74
        global $model;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
75
        $model = [
76
            'name' => $this->app->name,
77
            'caption' => $action->caption(),
78
            'menu' => $this->app->menu->render(),
79
            'breadcrumbs' => $this->assembleBreadCrumbs(),
80
            'action' => $form->getModel(),
81
            'result' => isset($result) ? $result->getModel() : null,
82
            'headElements' => HeadElements::filter($headElements),
83
            'executed' => isset($result) && $result->wasExecuted()
84
        ];
85
86
        ob_start();
87
        include __DIR__ . '/ExecutionTemplate.html.php';
88
        return ob_get_clean();
89
    }
90
91
    /**
92
     * @param $actionId
93
     * @return Action
94
     * @throws \Exception
95
     */
96
    private function getAction($actionId) {
97
        try {
98
            return $this->app->actions->getAction($actionId);
99
        } catch (\Exception $e) {
100
            throw new \Exception("Action [$actionId] is not registered.", 0, $e);
101
        }
102
    }
103
104
    private function assembleBreadCrumbs() {
105
        return array_map(function (BreadCrumb $crumb) {
106
            return [
107
                'target' => $crumb->getTarget(),
108
                'caption' => $crumb->getCaption()
109
            ];
110
        }, $this->crumbs->getCrumbs());
111
    }
112
}