Passed
Pull Request — master (#8)
by PHPinnacle
03:25
created

Kernel   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Test Coverage

Coverage 94.87%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 122
ccs 37
cts 39
cp 0.9487
rs 10
c 0
b 0
f 0
wmc 17

7 Methods

Rating   Name   Duplication   Size   Complexity  
A recoil() 0 19 3
A execute() 0 10 1
A __construct() 0 3 2
A intercept() 0 15 6
A adapt() 0 3 2
A throw() 0 6 2
A interrupt() 0 3 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
namespace PHPinnacle\Ensign;
12
13
use Amp\Coroutine;
14
use Amp\LazyPromise;
15
use PHPinnacle\Ensign\Exception\BadActionCall;
16
use PHPinnacle\Identity\UUID;
17
18
final class Kernel
19
{
20
    /**
21
     * @var Processor
22
     */
23
    private $processor;
24
25
    /**
26
     * @var callable[]
27
     */
28
    private $interruptions = [];
29
30
    /**
31
     * @param Processor $processor
32
     */
33 8
    public function __construct(Processor $processor = null)
34
    {
35 8
        $this->processor = $processor ?: new Processor\SimpleProcessor();
36 8
    }
37
38
    /**
39
     * @param string   $interrupt
40
     * @param callable $handler
41
     *
42
     * @return void
43
     */
44 5
    public function interrupt(string $interrupt, callable $handler): void
45
    {
46 5
        $this->interruptions[$interrupt] = $handler;
47 5
    }
48
49
    /**
50
     * @param callable $handler
51
     * @param array    $arguments
52
     *
53
     * @return Action
54
     */
55 8
    public function execute(callable $handler, array $arguments): Action
56
    {
57 8
        $id = UUID::random();
58
59 8
        $token   = new Token($id);
60 8
        $promise = new LazyPromise(function () use ($handler, $arguments, $token) {
61 8
            return $this->adapt($this->processor->execute($handler, $arguments), $token);
62 8
        });
63
64 8
        return new Action($id, $promise, $token);
65
    }
66
67
    /**
68
     * @param mixed $value
69
     * @param Token $token
70
     *
71
     * @return mixed
72
     */
73 7
    private function adapt($value, Token $token)
74
    {
75 7
        return $value instanceof \Generator ? new Coroutine($this->recoil($value, $token)) : $value;
0 ignored issues
show
Bug introduced by
It seems like $this->recoil($value, $token) can also be of type null and Amp\Coroutine; however, parameter $generator of Amp\Coroutine::__construct() does only seem to accept Generator, maybe add an additional type check? ( Ignorable by Annotation )

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

75
        return $value instanceof \Generator ? new Coroutine(/** @scrutinizer ignore-type */ $this->recoil($value, $token)) : $value;
Loading history...
76
    }
77
78
    /**
79
     * @param \Generator $generator
80
     * @param Token      $token
81
     *
82
     * @return mixed
83
     */
84 3
    private function recoil(\Generator $generator, Token $token)
85
    {
86 3
        $step = 1;
87
88 3
        while ($generator->valid()) {
89 3
            $token->guard();
90
91
            try {
92 3
                $value = $this->intercept($generator->key(), $generator->current());
93
94 3
                $generator->send(yield $this->adapt($value, $token));
95 2
            } catch (\Exception $error) {
96 1
                $this->throw($generator, $error, $step);
97 2
            } finally {
98 3
                $step++;
99
            }
100
        }
101
102 2
        return $this->adapt($generator->getReturn(), $token);
103
    }
104
105
    /**
106
     * @param \Generator $generator
107
     * @param \Exception $error
108
     * @param int        $step
109
     */
110 1
    private function throw(\Generator $generator, \Exception $error, int $step)
111
    {
112
        try {
113 1
            $generator->throw($error);
0 ignored issues
show
Bug introduced by
The method throw() does not exist on Generator. ( Ignorable by Annotation )

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

113
            $generator->/** @scrutinizer ignore-call */ 
114
                        throw($error);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
114
        } catch (\Throwable $error) {
115
            throw new BadActionCall($step, $error);
116
        }
117 1
    }
118
119
    /**
120
     * @param int|string $key
121
     * @param mixed      $value
122
     *
123
     * @return mixed
124
     */
125 3
    private function intercept($key, $value)
126
    {
127 3
        $interrupt = \is_string($key) ? $key : $value;
128
129 3
        if (\is_object($interrupt)) {
130 2
            $interrupt = \get_class($interrupt);
131
        }
132
133 3
        if (!\is_string($interrupt) || !isset($this->interruptions[$interrupt])) {
134 2
            return $value;
135
        }
136
137 1
        $value = \is_array($value) ? $value : [$value];
138
139 1
        return $this->processor->execute($this->interruptions[$interrupt], $value);
140
    }
141
}
142