Completed
Push — master ( e327bd...2b2b50 )
by Nikolas
03:41
created

ExecuteResource   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 13
Bugs 0 Features 1
Metric Value
c 13
b 0
f 1
dl 0
loc 91
wmc 11
lcom 1
cbo 8
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A baseHeadElements() 0 8 1
A doGet() 0 3 1
A doPost() 0 3 1
B doExecute() 0 26 4
A createDefaultRenderer() 0 3 1
A getAction() 0 8 2
1
<?php
2
namespace rtens\domin\delivery\web\root;
3
4
use rtens\domin\Action;
5
use rtens\domin\delivery\web\ActionForm;
6
use rtens\domin\delivery\web\ActionResult;
7
use rtens\domin\delivery\web\BreadCrumbs;
8
use rtens\domin\delivery\web\HeadElements;
9
use rtens\domin\delivery\web\WebApplication;
10
use watoki\curir\cookie\CookieStore;
11
use watoki\curir\delivery\WebRequest;
12
use watoki\curir\delivery\WebResponse;
13
use watoki\curir\error\HttpError;
14
use watoki\curir\rendering\PhpRenderer;
15
use watoki\curir\Resource;
16
use watoki\factory\Factory;
17
18
class ExecuteResource extends Resource {
19
20
    const ACTION_ARG = '__action';
21
22
    /** @var CookieStore */
23
    private $cookies;
24
25
    /**
26
     * @param Factory $factory <-
27
     * @param WebApplication $app <-
28
     * @param CookieStore $cookies <-
29
     */
30
    public function __construct(Factory $factory, WebApplication $app, CookieStore $cookies) {
31
        parent::__construct($factory);
32
        $this->app = $app;
0 ignored issues
show
Bug introduced by
The property app does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33
        $this->cookies = $cookies;
34
    }
35
36
    private static function baseHeadElements() {
37
        return [
38
            HeadElements::jquery(),
39
            HeadElements::jqueryUi(), // not actually needed but it needs to be included before bootstrap.js too avoid conflicts
40
            HeadElements::bootstrap(),
41
            HeadElements::bootstrapJs(),
42
        ];
43
    }
44
45
    /**
46
     * @param string $__action
47
     * @param WebRequest $__request <-
48
     * @return array
49
     * @throws \Exception
50
     */
51
    public function doGet($__action, WebRequest $__request) {
52
        return $this->doExecute($__action, $__request);
53
    }
54
55
    /**
56
     * @param string $__action
57
     * @param WebRequest $__request <-
58
     * @return array
59
     */
60
    public function doPost($__action, WebRequest $__request) {
61
        return $this->doExecute($__action, $__request, true);
62
    }
63
64
    private function doExecute($actionId, WebRequest $request, $mayBeModifying = false) {
65
        $action = $this->getAction($actionId);
66
67
        $crumbs = new BreadCrumbs($this->cookies, $request);
68
        $form = new ActionForm($request, $this->app, $action, $actionId);
69
70
        $headElements = array_merge(
71
            self::baseHeadElements(),
72
            $form->getHeadElements()
73
        );
74
75
        if ($mayBeModifying || !$action->isModifying()) {
76
            $result = new ActionResult($request, $this->app, $action, $actionId, $crumbs);
77
            $headElements = array_merge($headElements, $result->getHeadElements());
78
        }
79
80
        return [
81
            'name' => $this->app->name,
82
            'caption' => $action->caption(),
83
            'menu' => $this->app->menu->render($request),
84
            'breadcrumbs' => $crumbs->readCrumbs(),
85
            'action' => $form->getModel(),
86
            'result' => isset($result) ? $result->getModel() : null,
87
            'headElements' => HeadElements::filter($headElements),
88
        ];
89
    }
90
91
    protected function createDefaultRenderer() {
92
        return new PhpRenderer();
93
    }
94
95
    /**
96
     * @param $actionId
97
     * @return Action
98
     * @throws HttpError
99
     */
100
    private function getAction($actionId) {
101
        try {
102
            return $this->app->actions->getAction($actionId);
103
        } catch (\Exception $e) {
104
            throw new HttpError(WebResponse::STATUS_NOT_FOUND, "Action does not exist.",
105
                "Action [$actionId] is not registered.", 0, $e);
106
        }
107
    }
108
}