ActionRegistry::getAction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
namespace rtens\domin;
3
4
class ActionRegistry {
5
6
    /** @var array|Action[] indexed by ID */
7
    private $actions = [];
8
9
    /**
10
     * @return Action[] indexed by ID
11
     */
12
    public function getAllActions() {
13
        return $this->actions;
14
    }
15
16
    /**
17
     * @param string $id
18
     * @return Action
19
     * @throws \Exception
20
     */
21
    public function getAction($id) {
22
        if (!array_key_exists($id, $this->actions)) {
23
            throw new \Exception("Action [$id] is not registered.");
24
        }
25
26
        return $this->actions[$id];
27
    }
28
29
    /**
30
     * @param string $id
31
     * @param Action $action
32
     * @throws \Exception
33
     * @return Action
34
     */
35
    public function add($id, Action $action) {
36
        if (array_key_exists($id, $this->actions)) {
37
            throw new \Exception("Action [$id] is already registered.");
38
        }
39
40
        $this->actions[$id] = $action;
41
42
        return $action;
43
    }
44
}