Passed
Push — master ( 2f01ca...ac7767 )
by PHPinnacle
06:39
created

HandlerMap::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of PHPinnacle/Ensign.
4
 *
5
 * (c) PHPinnacle Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types = 1);
12
13
namespace PHPinnacle\Ensign;
14
15
final class HandlerMap
16
{
17
    /**
18
     * @var ArgumentsResolver
19
     */
20
    private $resolver;
21
22
    /**
23
     * @var Handler[]
24
     */
25
    private $handlers = [];
26
27
    /**
28
     * @param ArgumentsResolver $resolver
29
     */
30 9
    public function __construct(ArgumentsResolver $resolver = null)
31
    {
32 9
        $this->resolver = $resolver ?: new Resolver\EmptyResolver();
33 9
    }
34
35
    /**
36
     * @param string   $name
37
     * @param callable $callable
38
     *
39
     * @return self
40
     */
41 7
    public function register(string $name, callable $callable): self
42
    {
43 7
        $this->handlers[$name] = $this->handler($callable);
44
45 7
        return $this;
46
    }
47
48
    /**
49
     * @param string $name
50
     *
51
     * @return Handler|null
52
     */
53 8
    public function get(string $name): ?Handler
54
    {
55 8
        return $this->handlers[$name] ?? null;
56
    }
57
58
    /**
59
     * @param callable $callable
60
     *
61
     * @return Handler
62
     */
63 7
    private function handler(callable $callable): Handler
64
    {
65 7
        if ($callable instanceof Handler) {
66 1
            return $callable;
67
        }
68
69 6
        return Handler::define($callable, $this->resolver->resolve($callable));
70
    }
71
}
72