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

FormatContainer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 21
c 0
b 0
f 0
ccs 8
cts 9
cp 0.8889
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 11 3
A get() 0 4 2
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