Completed
Pull Request — master (#29)
by Yann
01:35
created

EnumRegistry   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 8 2
A get() 0 8 2
A has() 0 4 1
A all() 0 4 1
1
<?php
2
3
namespace Yokai\Enum;
4
5
use Yokai\Enum\Exception\DuplicatedEnumException;
6
use Yokai\Enum\Exception\InvalidEnumException;
7
8
/**
9
 * @author Yann Eugoné <[email protected]>
10
 */
11
class EnumRegistry
12
{
13
    /**
14
     * @var EnumInterface[]
15
     */
16
    private $enums;
17
18
    /**
19
     * @param EnumInterface $enum
20
     *
21
     * @throws DuplicatedEnumException
22
     */
23 3
    public function add(EnumInterface $enum)
24
    {
25 3
        if ($this->has($enum->getName())) {
26 1
            throw DuplicatedEnumException::alreadyRegistered($enum->getName());
27
        }
28
29 3
        $this->enums[$enum->getName()] = $enum;
30 3
    }
31
32
    /**
33
     * @param string $name
34
     *
35
     * @return EnumInterface
36
     * @throws InvalidEnumException
37
     */
38 2
    public function get($name)
39
    {
40 2
        if (!$this->has($name)) {
41 1
            throw InvalidEnumException::nonexistent($name);
42
        }
43
44 1
        return $this->enums[$name];
45
    }
46
47
    /**
48
     * @param string $name
49
     *
50
     * @return bool
51
     */
52 3
    public function has($name)
53
    {
54 3
        return isset($this->enums[$name]);
55
    }
56
57
    /**
58
     * @return EnumInterface[]
59
     */
60 1
    public function all()
61
    {
62 1
        return $this->enums;
63
    }
64
}
65