Passed
Push — master ( ac7767...ce6fb4 )
by PHPinnacle
06:06 queued 03:22
created

HandlerRegistry   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 55
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 2
A register() 0 5 1
A get() 0 3 1
A handler() 0 7 2
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 HandlerRegistry
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 10
    public function __construct(ArgumentsResolver $resolver = null)
31
    {
32 10
        $this->resolver = $resolver ?: new Resolver\EmptyResolver();
33 10
    }
34
35
    /**
36
     * @param string   $name
37
     * @param callable $callable
38
     *
39
     * @return self
40
     */
41 8
    public function register(string $name, callable $callable): self
42
    {
43 8
        $this->handlers[$name] = $this->handler($callable);
44
45 8
        return $this;
46
    }
47
48
    /**
49
     * @param string $name
50
     *
51
     * @return Handler|null
52
     */
53 9
    public function get(string $name): ?Handler
54
    {
55 9
        return $this->handlers[$name] ?? null;
56
    }
57
58
    /**
59
     * @param callable $callable
60
     *
61
     * @return Handler
62
     */
63 8
    private function handler(callable $callable): Handler
64
    {
65 8
        if ($callable instanceof Handler) {
66 1
            return $callable;
67
        }
68
69 7
        return new Handler($callable, $this->resolver->resolve($callable));
70
    }
71
}
72