Completed
Pull Request — master (#90)
by Arnaud
07:17 queued 28s
created

Registry::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace LAG\AdminBundle\Action\Registry;
4
5
use Exception;
6
use LAG\AdminBundle\Action\ActionInterface;
7
8
/**
9
 * Store the Action in a typed collection
10
 */
11 View Code Duplication
class Registry
12
{
13
    /**
14
     * Actions registry.
15
     *
16
     * @var ActionInterface[]
17
     */
18
    protected $actions = [];
19
    
20
    /**
21
     * Add an Action to the registry.
22
     *
23
     * @param $id
24
     * @param ActionInterface $action
25 1
     *
26
     * @throws Exception
27 1
     */
28 1
    public function add($id, ActionInterface $action)
29
    {
30 1
        if (array_key_exists($id, $this->actions)) {
31 1
            throw new Exception('An Action with the name "'.$id.'" has already been registered');
32
        }
33
        $this->actions[$id] = $action;
34
    }
35
    
36
    /**
37
     * Return an Action from the registry.
38
     *
39
     * @param string $id
40 1
     * @return ActionInterface
41
     * @throws Exception
42 1
     */
43 1
    public function get($id)
44
    {
45
        if (!array_key_exists($id, $this->actions)) {
46 1
            throw new Exception('No Action with the service id "'.$id.'" has been found');
47
        }
48
        
49
        return $this->actions[$id];
50
    }
51
    
52
    /**
53
     * Return true if an Action with the name $id has been registered.
54
     *
55 1
     * @param string $id
56
     * @return bool
57 1
     */
58
    public function has($id)
59
    {
60
        return array_key_exists($id, $this->actions);
61
    }
62
    
63
    /**
64
     * Return all the registered Actions.
65 1
     *
66
     * @return ActionInterface[]
67 1
     */
68
    public function all()
69
    {
70
        return $this->actions;
71
    }
72
}
73