Completed
Pull Request — master (#90)
by Arnaud
16:16
created

Registry::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 7
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
3
namespace LAG\AdminBundle\Action\Registry;
4
5
use Exception;
6
use LAG\AdminBundle\Action\ActionInterface;
7
8 View Code Duplication
class Registry
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
9
{
10
    /**
11
     * Actions registry.
12
     *
13
     * @var ActionInterface[]
14
     */
15
    protected $actions = [];
16
    
17
    /**
18
     * Add an Action to the registry.
19
     *
20
     * @param $id
21
     * @param ActionInterface $action
22
     *
23
     * @throws Exception
24
     */
25 1
    public function add($id, ActionInterface $action)
26
    {
27 1
        if (array_key_exists($id, $this->actions)) {
28 1
            throw new Exception('An Action with the name "'.$id.'" has already been registered');
29
        }
30 1
        $this->actions[$id] = $action;
31 1
    }
32
    
33
    /**
34
     * Return an Action from the registry.
35
     *
36
     * @param string $id
37
     * @return ActionInterface
38
     * @throws Exception
39
     */
40 1
    public function get($id)
41
    {
42 1
        if (!array_key_exists($id, $this->actions)) {
43 1
            throw new Exception('No Action with the service id "'.$id.'" has been found');
44
        }
45
        
46 1
        return $this->actions[$id];
47
    }
48
    
49
    /**
50
     * Return true if an Action with the name $id has been registered.
51
     *
52
     * @param string $id
53
     * @return bool
54
     */
55 1
    public function has($id)
56
    {
57 1
        return array_key_exists($id, $this->actions);
58
    }
59
    
60
    /**
61
     * Return all the registered Actions.
62
     *
63
     * @return ActionInterface[]
64
     */
65 1
    public function all()
66
    {
67 1
        return $this->actions;
68
    }
69
}
70