Completed
Pull Request — master (#8)
by Adam
02:32
created

SimpleHandlerFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Equip\Queue\Handler;
4
5
use Equip\Queue\Exception\HandlerException;
6
7
class SimpleHandlerFactory implements HandlerFactoryInterface
8
{
9
    /**
10
     * @var array
11
     */
12
    private $handlers;
13
14
    /**
15
     * @param array $handlers
16
     */
17 4
    public function __construct(array $handlers = [])
18
    {
19 4
        $this->handlers = $handlers;
20 4
    }
21
22
    /**
23
     * @inheritdoc
24
     */
25 4
    public function get($handler)
26
    {
27 4
        $route = $this->getHandler($handler);
28 4
        if (!$route) {
29 1
            throw HandlerException::notFound($handler);
30
        }
31
32 3
        if (is_string($route) && class_exists($route)) {
33 1
            $route = new $route;
34
        }
35
36 3
        if (is_callable($route)) {
37 2
            return $route;
38
        }
39
40 1
        throw HandlerException::invalidHandler($handler);
41
    }
42
43
    /**
44
     * Get the handler
45
     *
46
     * @param string $handler
47
     *
48
     * @return mixed|null
49
     */
50 4
    private function getHandler($handler)
51
    {
52 4
        return isset($this->handlers[$handler]) ? $this->handlers[$handler] : null;
53
    }
54
}
55