Passed
Push — master ( 392556...a36e60 )
by PHPinnacle
06:42 queued 04:41
created

HandlerRegistry   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 39
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 5 1
A get() 0 4 1
A has() 0 3 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 HandlerRegistry
16
{
17
    /**
18
     * @var callable[]
19
     */
20
    private $handlers = [];
21
22
    /**
23
     * @param string   $signal
24
     * @param callable $handler
25
     *
26
     * @return self
27
     */
28 5
    public function add(string $signal, callable $handler): self
29
    {
30 5
        $this->handlers[$signal] = $handler;
31
32 5
        return $this;
33
    }
34
35
    /**
36
     * @param string $signal
37
     *
38
     * @return bool
39
     */
40 2
    public function has(string $signal): bool
41
    {
42 2
        return isset($this->handlers[$signal]);
43
    }
44
45
    /**
46
     * @param string $signal
47
     *
48
     * @return callable
49
     */
50
    public function get(string $signal): callable
51
    {
52 6
        return $this->handlers[$signal] ?? static function () use ($signal) {
53 1
            throw new Exception\UnknownSignal($signal);
54 6
        };
55
    }
56
}
57