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

Handler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 6 1
A unknown() 0 3 1
A error() 0 4 1
A __construct() 0 4 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 Handler
16
{
17
    /**
18
     * @var callable
19
     */
20
    private $callable;
21
22
    /**
23
     * @var Arguments
24
     */
25
    private $arguments;
26
27
    /**
28
     * @param callable  $callable
29
     * @param Arguments $arguments
30
     */
31 12
    public function __construct(callable $callable, Arguments $arguments = null)
32
    {
33 12
        $this->callable  = $callable;
34 12
        $this->arguments = $arguments ?: Arguments::empty();
35 12
    }
36
37
    /**
38
     * @param \Exception $error
39
     *
40
     * @return self
41
     */
42
    public static function error(\Exception $error): self
43
    {
44 3
        return new self(function () use ($error) {
45 3
            throw $error;
46 3
        });
47
    }
48
49
    /**
50
     * @param string $signal
51
     *
52
     * @return self
53
     */
54 2
    public static function unknown(string $signal): self
55
    {
56 2
        return self::error(new Exception\UnknownSignal($signal));
57
    }
58
59
    /**
60
     * @param mixed ...$arguments
61
     *
62
     * @return mixed
63
     */
64 11
    public function __invoke(...$arguments)
65
    {
66 11
        $callable  = $this->callable;
67 11
        $arguments = (new Arguments($arguments))->inject($this->arguments);
68
69 11
        return $callable(...$arguments);
70
    }
71
}
72