Completed
Push — master ( bd4cbc...df0c76 )
by Arnaud
9s
created

Registry::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
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\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
class Registry
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 3
    public function add(AdminInterface $admin)
27
    {
28 3
        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 3
        $this->admins[$admin->getName()] = $admin;
32 3
    }
33
34
    /**
35
     * Return an Admin from the registry.
36
     *
37
     * @param string $name
38
     * @return AdminInterface
39
     * @throws Exception
40
     */
41 3
    public function get($name)
42
    {
43 3
        if (!array_key_exists($name, $this->admins)) {
44 1
            throw new Exception('No Admin with the name "'.$name.'" has been found');
45
        }
46
47 2
        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
    public function has($name)
57
    {
58
        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