FormatContainer::add()   A
last analyzed

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\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