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

Handler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 64
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 5 1
A error() 0 4 1
A parameters() 0 3 1
A recognize() 0 5 1
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