Completed
Push — sensio-insight-fixes ( 787c3e )
by Yann
07:09
created

EnumRegistry   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 39
ccs 11
cts 11
cp 1
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 8 2
A get() 0 8 2
A has() 0 4 1
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
    public function add(EnumInterface $enum)
23
    {
24
        if ($this->has($enum->getName())) {
25
            throw DuplicatedEnumException::alreadyRegistered($enum->getName());
26
        }
27
28
        $this->enums[$enum->getName()] = $enum;
29
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34
    public function get($name)
35
    {
36
        if (!$this->has($name)) {
37
            throw InvalidEnumException::nonexistent($name);
38
        }
39
40
        return $this->enums[$name];
41
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46
    public function has($name)
47
    {
48
        return isset($this->enums[$name]);
49
    }
50
}
51