Completed
Push — master ( ca7ebd...ffd42a )
by Nikolas
03:40
created

ActionListResource::assembleAllActions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
namespace rtens\domin\delivery\web\resources;
3
4
use rtens\domin\Action;
5
use rtens\domin\delivery\web\BreadCrumbsTrail;
6
use rtens\domin\delivery\web\HeadElements;
7
use rtens\domin\delivery\web\WebApplication;
8
9
class ActionListResource {
10
    const GROUP_ALL = 'All';
11
12
    /** @var WebApplication */
13
    private $app;
14
15
    /** @var BreadCrumbsTrail */
16
    private $crumbs;
17
18
    public function __construct(WebApplication $app, BreadCrumbsTrail $crumbs) {
19
        $this->app = $app;
20
        $this->crumbs = $crumbs;
21
    }
22
23
    /**
24
     * @return string
25
     */
26
    public function handleGet() {
27
        $this->crumbs->reset();
28
29
        return (new Template(__DIR__ . '/ActionListTemplate.html.php'))
30
            ->render([
31
                'name' => $this->app->name,
32
                'menu' => $this->app->menu->render(),
33
                'actions' => $this->assembleAllActions(),
34
                'headElements' => [
35
                    (string)HeadElements::jquery(),
36
                    (string)HeadElements::bootstrap(),
37
                    (string)HeadElements::bootstrapJs(),
38
                ]
39
            ]);
40
    }
41
42
    private function assembleAllActions() {
43
        return array_merge($this->assembleActionGroups(), [
44
            self::GROUP_ALL => $this->assembleActions($this->app->actions->getAllActions())
45
        ]);
46
    }
47
48
    private function assembleActionGroups() {
49
        $groups = [];
50
        foreach ($this->app->groups->getGroups() as $group) {
51
            $groups[$group] = $this->assembleActions($this->app->groups->getActionsOf($group));
52
        }
53
        return $groups;
54
    }
55
56
    /**
57
     * @param Action[] $actionsById
58
     * @return array
59
     */
60
    private function assembleActions($actionsById) {
61
        $actions = [];
62
        foreach ($actionsById as $id => $action) {
63
            if ($this->app->access->isPermitted($id)) {
64
                $actions[] = [
65
                    'caption' => $action->caption(),
66
                    'description' => $this->app->parser->shorten($action->description()),
67
                    'link' => ['href' => $id]
68
                ];
69
            }
70
        }
71
        return $actions;
72
    }
73
}