Test Failed
Push — master ( 0f63fa...5627de )
by PHPinnacle
02:54
created

Processor::recoil()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 2
dl 0
loc 16
ccs 7
cts 8
cp 0.875
crap 3.0175
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
declare(strict_types = 1);
12
13
namespace PHPinnacle\Ensign;
14
15
use Amp\LazyPromise;
16
use Amp\Coroutine;
17
use PHPinnacle\Identity\UUID;
18
19
final class Processor implements Contract\Processor
20
{
21
    /**
22
     * @var callable[]
23
     */
24
    private $interruptions = [];
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 3
    public function interrupt(string $interrupt, callable $handler): void
30
    {
31 3
        $this->interruptions[$interrupt] = $handler;
32 3
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 6
    public function execute(callable $handler, array $arguments): Task
38
    {
39 6
        $id    = UUID::random();
40 6
        $token = new Token($id);
41
42 6
        return new Task($id, new LazyPromise(function () use ($handler, $arguments, $token) {
43 6
            return $this->adapt($handler(...$arguments), $token);
44 6
        }), $token);
45
    }
46
47
    /**
48
     * @param mixed $value
49
     * @param Token $token
50
     *
51
     * @return mixed
52
     */
53 5
    private function adapt($value, Token $token)
54
    {
55 5
        return $value instanceof \Generator ? new Coroutine($this->recoil($value, $token)) : $value;
56
    }
57
58
    /**
59
     * @param \Generator $generator
60
     * @param Token      $token
61
     *
62
     * @return mixed
63
     */
64 2
    private function recoil(\Generator $generator, Token $token)
65
    {
66 2
        while ($generator->valid()) {
67 2
            $token->guard();
68
69
            try {
70 2
                $value = $this->intercept($generator->key(), $generator->current());
71
72 2
                $generator->send(yield $this->adapt($value, $token));
73 1
            } catch (\Exception $error) {
74
                /** @scrutinizer ignore-call */
75
                $generator->throw($error);
76
            }
77
        }
78
79 1
        return $this->adapt($generator->getReturn(), $token);
80
    }
81
82
    /**
83
     * @param int|string $key
84
     * @param mixed      $value
85
     *
86
     * @return mixed
87
     */
88 2
    private function intercept($key, $value)
89
    {
90 2
        $interrupt = \is_string($key) ? $key : $value;
91
92 2
        if (\is_object($interrupt)) {
93 1
            $interrupt = \get_class($interrupt);
94
        }
95
96 2
        if (!\is_string($interrupt) || !isset($this->interruptions[$interrupt])) {
97 2
            return $value;
98
        }
99
100
        $interceptor = $this->interruptions[$interrupt];
101
102
        $value = \is_array($value) ? $value : [$value];
103
104
        return $interceptor(...$value);
105
    }
106
}
107