Completed
Push — master ( ce8189...62192d )
by Tomasz
22s
created

FormatContainer::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

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 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
namespace Thunder\Serializard\FormatContainer;
3
4
use Thunder\Serializard\Exception\FormatNotFoundException;
5
use Thunder\Serializard\Format\FormatInterface;
6
7
/**
8
 * @author Tomasz Kowalczyk <[email protected]>
9
 */
10
final class FormatContainer implements FormatContainerInterface
11
{
12
    private $formats = [];
13
14 16
    public function add($alias, FormatInterface $handler)
15
    {
16 16
        if(false === \is_string($alias)) {
17 1
            throw new \InvalidArgumentException('Format alias must be a string.');
18
        }
19 15
        if(array_key_exists($alias, $this->formats)) {
20
            throw new \InvalidArgumentException(sprintf('Format with alias `%s` already exists.', $alias));
21
        }
22
23 15
        $this->formats[$alias] = $handler;
24 15
    }
25
26 15
    public function get($alias)
27
    {
28 15
        if(false === array_key_exists($alias, $this->formats)) {
29 3
            throw FormatNotFoundException::fromAlias($alias);
30
        }
31
32 12
        return $this->formats[$alias];
33
    }
34
}
35