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

ActionRegistry   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 60
wmc 9
lcom 1
cbo 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllActions() 0 11 2
A getAction() 0 7 4
A add() 0 9 2
A restrictAccess() 0 3 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
}