Passed
Push — master ( 31b4e9...ba5ec6 )
by PHPinnacle
02:42
created

Handler::parameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
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 Handler
16
{
17
    /**
18
     * @var callable
19
     */
20
    private $action;
21
22
    /**
23
     * @var \ReflectionParameter[]
24
     */
25
    private $parameters;
26
27
    /**
28
     * @param callable $action
29
     * @param array    $parameters
30
     */
31 11
    public function __construct(callable $action, array $parameters = [])
32
    {
33 11
        $this->action     = $action;
34 11
        $this->parameters = $parameters;
35 11
    }
36
37
    /**
38
     * @param callable $action
39
     *
40
     * @return self
41
     */
42 10
    public static function recognize(callable $action): self
43
    {
44 10
        $reflection = new \ReflectionMethod(\Closure::fromCallable($action), '__invoke');
45
46 10
        return new self($action, $reflection->getParameters());
47
    }
48
49
    /**
50
     * @param \Throwable $error
51
     *
52
     * @return self
53
     */
54
    public static function error(\Throwable $error): self
55
    {
56 1
        return new self(function () use ($error) {
57 1
            throw $error;
58 1
        });
59
    }
60
61
    /**
62
     * @return \ReflectionParameter[]
63
     */
64 4
    public function parameters(): array
65
    {
66 4
        return $this->parameters;
67
    }
68
69
    /**
70
     * @param mixed ...$arguments
71
     *
72
     * @return mixed
73
     */
74 7
    public function __invoke(...$arguments)
75
    {
76 7
        $action = $this->action;
77
78 7
        return $action(...$arguments);
79
    }
80
}
81