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

Registry   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 60
loc 60
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\Admin\Registry;
4
5
use Exception;
6
use LAG\AdminBundle\Admin\AdminInterface;
7
8
/**
9
 * Admin registry to store Admins along the request.
10
 */
11 View Code Duplication
class Registry
1 ignored issue
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...
12
{
13
    /**
14
     * Admins registry.
15
     *
16
     * @var AdminInterface[]
17
     */
18
    protected $admins = [];
19
20
    /**
21
     * Add an Admin to the registry.
22
     *
23
     * @param AdminInterface $admin
24
     * @throws Exception
25
     */
26 1
    public function add(AdminInterface $admin)
27
    {
28 1
        if (array_key_exists($admin->getName(), $this->admins)) {
29 1
            throw new Exception('An Admin with the name "'.$admin->getName().'" has already been registered');
30
        }
31 1
        $this->admins[$admin->getName()] = $admin;
32 1
    }
33
34
    /**
35
     * Return an Admin from the registry.
36
     *
37
     * @param string $name
38
     * @return AdminInterface
39
     * @throws Exception
40
     */
41 1
    public function get($name)
42
    {
43 1
        if (!array_key_exists($name, $this->admins)) {
44 1
            throw new Exception('No Admin with the name "'.$name.'" has been found');
45
        }
46
47 1
        return $this->admins[$name];
48
    }
49
50
    /**
51
     * Return true if an Admin with the name $name has been registered.
52
     *
53
     * @param string $name
54
     * @return bool
55
     */
56 1
    public function has($name)
57
    {
58 1
        return array_key_exists($name, $this->admins);
59
    }
60
61
    /**
62
     * Return all the registered Admins.
63
     *
64
     * @return AdminInterface[]
65
     */
66 1
    public function all()
67
    {
68 1
        return $this->admins;
69
    }
70
}
71