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

Dispatcher::error()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
nc 1
nop 1
dl 0
loc 4
c 0
b 0
f 0
cc 1
ccs 2
cts 2
cp 1
crap 1
rs 10
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 Dispatcher
16
{
17
    /**
18
     * @var Processor
19
     */
20
    private $processor;
21
22
    /**
23
     * @var Resolver
24
     */
25
    private $resolver;
26
27
    /**
28
     * @var callable[]
29
     */
30
    private $handlers = [];
31
32
    /**
33
     * @param Processor $processor
34
     * @param Resolver $resolver
35
     */
36 8
    public function __construct(Processor $processor, Resolver $resolver)
37
    {
38 8
        $this->processor = $processor;
39 8
        $this->resolver  = $resolver;
40 8
    }
41
42
    /**
43
     * @param Resolver $resolver
44
     *
45
     * @return self
46
     */
47 8
    public static function instance(Resolver $resolver = null): self
48
    {
49 8
        return new self(new Processor(), $resolver ?: new Resolver\EmptyResolver());
50
    }
51
52
    /**
53
     * @param string   $signal
54
     * @param callable $handler
55
     *
56
     * @return self
57
     */
58 6
    public function register(string $signal, callable $handler): self
59
    {
60 6
        $this->handlers[$signal] = $handler instanceof Handler ? $handler : Handler::recognize($handler);
61
62 6
        $this->processor->interrupt($signal, function (...$arguments) use ($signal) {
63 1
            return $this->dispatch($signal, ...$arguments);
64 6
        });
65
66 6
        return $this;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 7
    public function dispatch($signal, ...$arguments): Task
73
    {
74 7
        if (\is_object($signal)) {
75 1
            \array_unshift($arguments, $signal);
76
77 1
            $signal = \get_class($signal);
78
        }
79
80 7
        $handler   = $this->handlers[$signal] ?? Handler::error(new Exception\UnknownSignal($signal));
81 7
        $arguments = $this->resolve($handler, $arguments);
82
83 7
        return $this->processor->execute($handler, ...$arguments);
84
    }
85
86
    /**
87
     * @param callable $handler
88
     * @param array    $arguments
89
     *
90
     * @return array
91
     */
92 7
    private function resolve(callable $handler, array $arguments): array
93
    {
94 7
        $resolved = $this->resolver->resolve($handler);
0 ignored issues
show
Bug introduced by
$handler of type callable is incompatible with the type PHPinnacle\Ensign\Handler expected by parameter $handler of PHPinnacle\Ensign\Resolver::resolve(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
        $resolved = $this->resolver->resolve(/** @scrutinizer ignore-type */ $handler);
Loading history...
95
96 7
        foreach ($resolved as $position => $argument) {
97 1
            \array_splice($arguments, $position, 0, [$argument]);
98
        }
99
100 7
        return $arguments;
101
    }
102
}
103