HandlerRegistry::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 8
    public function add(string $signal, callable $handler): self
29
    {
30 8
        $this->handlers[$signal] = $handler;
31
32 8
        return $this;
33
    }
34
35
    /**
36
     * @param string $signal
37
     *
38
     * @return bool
39
     */
40 4
    public function has(string $signal): bool
41
    {
42 4
        return isset($this->handlers[$signal]);
43
    }
44
45
    /**
46
     * @param string $signal
47
     *
48
     * @return callable
49
     */
50 10
    public function get(string $signal): callable
51
    {
52
        return $this->handlers[$signal] ?? static function () use ($signal) {
53 2
            throw new Exception\UnknownSignal($signal);
54 10
        };
55
    }
56
}
57