Passed
Pull Request — master (#10)
by PHPinnacle
06:17 queued 03:59
created

Processor::intercept()   A

Complexity

Conditions 6
Paths 12

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 7
nc 12
nop 2
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 6
rs 9.2222
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
use Amp;
16
use Amp\Promise;
17
use Amp\Success;
18
19
final class Processor
20
{
21
    /**
22
     * @var Executor
23
     */
24
    private $executor;
25
26
    /**
27
     * @var callable
28
     */
29
    private $resolver;
30
31
    /**
32
     * @var callable[]
33
     */
34
    private $interruptions = [];
35
36
    /**
37
     * @param Executor $executor
38
     */
39 9
    public function __construct(Executor $executor = null)
40
    {
41 9
        $this->executor = $executor ?: new Executor\SimpleExecutor;
42 3
        $this->resolver = function (string $interrupt, array $arguments) {
43 3
            if (!isset($this->interruptions[$interrupt])) {
44 1
                throw new Exception\UnknownInterrupt($interrupt);
45
            }
46
47 2
            return $this->execute($this->interruptions[$interrupt], $arguments);
48
        };
49 9
    }
50
51
    /**
52
     * @param string   $interrupt
53
     * @param callable $handler
54
     *
55
     * @return void
56
     */
57 6
    public function interrupt(string $interrupt, callable $handler): void
58
    {
59 6
        $this->interruptions[$interrupt] = $handler;
60 6
    }
61
62
    /**
63
     * @param callable $handler
64
     * @param array    $arguments
65
     *
66
     * @return Promise
67
     */
68
    public function execute(callable $handler, array $arguments): Promise
69
    {
70 9
        return Amp\call(function () use ($handler, $arguments) {
0 ignored issues
show
Bug introduced by
The function call was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

70
        return /** @scrutinizer ignore-call */ Amp\call(function () use ($handler, $arguments) {
Loading history...
71 9
            $result = $this->executor->execute($handler, $arguments);
72
73 8
            if ($result instanceof \Generator) {
74 4
                return new Subroutine($result, $this->resolver);
75
            }
76
77 6
            return $result;
78 9
        });
79
    }
80
}
81