Completed
Push — master ( d5349d...2edaf3 )
by Yann
9s
created

EnumRegistry::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 Yokai\EnumBundle\Registry;
4
5
use Yokai\EnumBundle\Enum\EnumInterface;
6
use Yokai\EnumBundle\Exception\DuplicatedEnumException;
7
use Yokai\EnumBundle\Exception\InvalidEnumException;
8
9
/**
10
 * @author Yann Eugoné <[email protected]>
11
 */
12
class EnumRegistry implements EnumRegistryInterface
13
{
14
    /**
15
     * @var EnumInterface[]
16
     */
17
    private $enums;
18
19
    /**
20
     * @inheritdoc
21
     */
22 3
    public function add(EnumInterface $enum)
23
    {
24 3
        if ($this->has($enum->getName())) {
25 1
            throw DuplicatedEnumException::alreadyRegistered($enum->getName());
26
        }
27
28 3
        $this->enums[$enum->getName()] = $enum;
29 3
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34 2
    public function get($name)
35
    {
36 2
        if (!$this->has($name)) {
37 1
            throw InvalidEnumException::nonexistent($name);
38
        }
39
40 1
        return $this->enums[$name];
41
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46 3
    public function has($name)
47
    {
48 3
        return isset($this->enums[$name]);
49
    }
50
}
51