Passed
Push — master ( f832ac...3bedb1 )
by PHPinnacle
05:03 queued 02:22
created

Kernel::adapt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
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
namespace PHPinnacle\Ensign;
12
13
use Amp\Coroutine;
14
use Amp\LazyPromise;
15
use PHPinnacle\Identity\UUID;
16
17
final class Kernel
18
{
19
    /**
20
     * @var Contract\Processor
21
     */
22
    private $processor;
23
24
    /**
25
     * @var callable[]
26
     */
27
    private $interruptions = [];
28
29
    /**
30
     * @param Contract\Processor $processor
31
     */
32 8
    public function __construct(Contract\Processor $processor = null)
33
    {
34 8
        $this->processor = $processor ?: new Processor\SimpleProcessor();
35 8
    }
36
37
    /**
38
     * @param string   $interrupt
39
     * @param callable $handler
40
     *
41
     * @return void
42
     */
43 5
    public function interrupt(string $interrupt, callable $handler): void
44
    {
45 5
        $this->interruptions[$interrupt] = $handler;
46 5
    }
47
48
    /**
49
     * @param callable $handler
50
     * @param array    $arguments
51
     *
52
     * @return Action
53
     */
54 8
    public function execute(callable $handler, array $arguments): Action
55
    {
56 8
        $id = UUID::random();
57
58 8
        $token   = new Token($id);
59 8
        $promise = new LazyPromise(function () use ($handler, $arguments, $token) {
60 8
            return $this->adapt($this->processor->execute($handler, $arguments), $token);
61 8
        });
62
63 8
        return new Action($id, $promise, $token);
64
    }
65
66
    /**
67
     * @param mixed $value
68
     * @param Token $token
69
     *
70
     * @return mixed
71
     */
72 7
    private function adapt($value, Token $token)
73
    {
74 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

74
        return $value instanceof \Generator ? new Coroutine(/** @scrutinizer ignore-type */ $this->recoil($value, $token)) : $value;
Loading history...
75
    }
76
77
    /**
78
     * @param \Generator $generator
79
     * @param Token      $token
80
     *
81
     * @return mixed
82
     */
83 3
    private function recoil(\Generator $generator, Token $token)
84
    {
85 3
        while ($generator->valid()) {
86 3
            $token->guard();
87
88
            try {
89 3
                $value = $this->intercept($generator->key(), $generator->current());
90
91 3
                $generator->send(yield $this->adapt($value, $token));
92 2
            } catch (\Exception $error) {
93
                /** @scrutinizer ignore-call */
94 1
                $generator->throw($error);
95
            }
96
        }
97
98 2
        return $this->adapt($generator->getReturn(), $token);
99
    }
100
101
    /**
102
     * @param int|string $key
103
     * @param mixed      $value
104
     *
105
     * @return mixed
106
     */
107 3
    private function intercept($key, $value)
108
    {
109 3
        $interrupt = \is_string($key) ? $key : $value;
110
111 3
        if (\is_object($interrupt)) {
112 2
            $interrupt = \get_class($interrupt);
113
        }
114
115 3
        if (!\is_string($interrupt) || !isset($this->interruptions[$interrupt])) {
116 2
            return $value;
117
        }
118
119 1
        $interceptor = $this->interruptions[$interrupt];
120
121 1
        $value = \is_array($value) ? $value : [$value];
122
123 1
        return $interceptor(...$value);
124
    }
125
}
126