Completed
Pull Request — master (#6)
by Tomasz
01:42
created

FormatContainer::add()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

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