Completed
Push — master ( 4f1190...3b2a6f )
by Nikolas
03:34
created

ActionRegistry::restrictAccess()   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 1
1
<?php
2
namespace rtens\domin;
3
4
class ActionRegistry {
5
6
    /** @var null|AccessControl */
7
    private $access;
8
9
    /** @var array|Action[] indexed by ID */
10
    private $actions = [];
11
12
    /**
13
     * @return Action[] indexed by ID
14
     */
15
    public function getAllActions() {
16
        $actions = $this->actions;
17
18
        if ($this->access) {
19
            $actions = array_filter($actions, function ($id) {
20
                return $this->access->isVisible($id);
21
            }, ARRAY_FILTER_USE_KEY);
22
        }
23
24
        return $actions;
25
    }
26
27
    /**
28
     * @param string $id
29
     * @return Action
30
     * @throws \Exception
31
     */
32
    public function getAction($id) {
33
        if (!array_key_exists($id, $this->actions) || $this->access && !$this->access->isVisible($id)) {
34
            throw new \Exception("Action [$id] is not registered.");
35
        }
36
37
        return $this->actions[$id];
38
    }
39
40
    /**
41
     * @param string $id
42
     * @param Action $action
43
     * @throws \Exception
44
     * @return Action
45
     */
46
    public function add($id, Action $action) {
47
        if (array_key_exists($id, $this->actions)) {
48
            throw new \Exception("Action [$id] is already registered.");
49
        }
50
51
        $this->actions[$id] = $action;
52
53
        return $action;
54
    }
55
56
    /**
57
     * @param AccessControl $access
58
     */
59
    public function restrictAccess(AccessControl $access) {
60
        $this->access = $access;
61
    }
62
63
}