Completed
Push — master ( 11c68a...3647eb )
by Tomasz
03:03
created

HandlerContainer::addAlias()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4286
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
namespace Thunder\Serializard\HandlerContainer;
3
4
/**
5
 * @author Tomasz Kowalczyk <[email protected]>
6
 */
7
final class HandlerContainer implements HandlerContainerInterface
8
{
9
    private $handlers = array();
10
    private $aliases = array();
11
12
    public function add($class, $root, $handler)
13
    {
14
        if(false === is_callable($handler)) {
15
            throw new \RuntimeException(sprintf('Invalid handler for class %s!', $class));
16
        }
17
18
        $this->aliases[$class] = $root;
19
        $this->handlers[$class] = $handler;
20
    }
21
22
    public function addAlias($alias, $class)
23
    {
24
        $handler = $this->getHandler($class);
25
26
        if(null === $handler) {
27
            throw new \RuntimeException(sprintf('Handler for class %s does not exist!', $class));
28
        }
29
30
        $this->handlers[$alias] = $handler;
31
        $this->aliases[$alias] = $this->aliases[$class];
32
    }
33
34
    public function getRoot($class)
35
    {
36
        return $this->aliases[$class];
37
    }
38
39
    public function getHandler($class)
40
    {
41
        return array_key_exists($class, $this->handlers) ? $this->handlers[$class] : null;
42
    }
43
}
44