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

HandlerContainer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
c 2
b 0
f 1
lcom 1
cbo 0
dl 0
loc 37
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 9 2
A addAlias() 0 11 2
A getRoot() 0 4 1
A getHandler() 0 4 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