Passed
Push — master ( 4ca9e9...d2e70b )
by PHPinnacle
02:43
created

Handler::__clone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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 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 9
    private function __construct(callable $callable, Arguments $arguments)
32
    {
33 9
        $this->callable  = $callable;
34 9
        $this->arguments = $arguments;
35 9
    }
36
37
    /**
38
     * @param callable       $callable
39
     * @param Arguments|null $arguments
40
     *
41
     * @return self
42
     */
43 9
    public static function define(callable $callable, Arguments $arguments = null): self
44
    {
45 9
        return new self($callable, $arguments ?: Arguments::empty());
46
    }
47
48
    /**
49
     * @param \Exception $error
50
     *
51
     * @return self
52
     */
53
    public static function error(\Exception $error): self
54
    {
55 3
        return self::define(function () use ($error) {
56 3
            throw $error;
57 3
        });
58
    }
59
60
    /**
61
     * @param string $signal
62
     *
63
     * @return self
64
     */
65 2
    public static function unknown(string $signal): self
66
    {
67 2
        return self::error(new Exception\UnknownSignal($signal));
68
    }
69
70
    /**
71
     * @param mixed ...$arguments
72
     *
73
     * @return mixed
74
     */
75 9
    public function __invoke(...$arguments)
76
    {
77 9
        $callable  = $this->callable;
78 9
        $arguments = Arguments::list($arguments)->inject($this->arguments);
79
80 9
        return $callable(...$arguments);
81
    }
82
}
83