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

Registry   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 62
loc 62
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 7 7 2
A get() 8 8 2
A has() 4 4 1
A all() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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