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

Handler::define()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 1
nc 1
nop 2
crap 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