Passed
Pull Request — master (#9)
by PHPinnacle
03:55
created

Processor::__construct()   A

Complexity

Conditions 2
Paths 1

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 1
nop 1
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 Processor
18
{
19
    /**
20
     * @var Executor
21
     */
22
    private $executor;
23
24
    /**
25
     * @var callable[]
26
     */
27
    private $interruptions = [];
28
29
    /**
30
     * @param Executor $executor
31
     */
32 8
    public function __construct(Executor $executor = null)
33
    {
34 8
        $this->executor = $executor ?: new Executor\SimpleExecutor();
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->executor->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
        $step = 1;
86
87 3
        while ($generator->valid()) {
88 3
            $token->guard();
89
90
            try {
91 3
                $value = $this->intercept($generator->key(), $generator->current());
92
93 3
                $generator->send(yield $this->adapt($value, $token));
94 2
            } catch (\Exception $error) {
95 1
                $this->throw($generator, $error, $step);
96 2
            } finally {
97 3
                $step++;
98
            }
99
        }
100
101 2
        return $this->adapt($generator->getReturn(), $token);
102
    }
103
104
    /**
105
     * @param \Generator $generator
106
     * @param \Exception $error
107
     * @param int        $step
108
     */
109 1
    private function throw(\Generator $generator, \Exception $error, int $step)
110
    {
111
        try {
112 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

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