Passed
Pull Request — master (#1111)
by Aleksei
11:44
created

InterceptorPipeline::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Interceptors\Handler;
6
7
use Psr\EventDispatcher\EventDispatcherInterface;
8
use Spiral\Interceptors\Context\CallContext;
9
use Spiral\Interceptors\Event\InterceptorCalling;
10
use Spiral\Interceptors\Exception\InterceptorException;
11
use Spiral\Interceptors\HandlerInterface;
12
use Spiral\Interceptors\InterceptorInterface;
13
14
/**
15
 * Interceptor pipeline.
16
 *
17
 * WARNING: make sure you don't use any legacy interceptors because they aren't supported with this pipeline.
18
 */
19
final class InterceptorPipeline implements HandlerInterface
20
{
21
    private ?HandlerInterface $handler = null;
22
23
    /** @var list<InterceptorInterface> */
0 ignored issues
show
Bug introduced by
The type Spiral\Interceptors\Handler\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
    private array $interceptors = [];
25
26
    private int $position = 0;
27
28 9
    public function __construct(
29
        private readonly ?EventDispatcherInterface $dispatcher = null
30
    ) {
31 9
    }
32
33
    /**
34
     * @psalm-immutable
35
     */
36 8
    public function withInterceptors(InterceptorInterface ...$interceptors): self
37
    {
38 8
        $clone = clone $this;
39 8
        foreach ($interceptors as $interceptor) {
40 6
            $clone->interceptors[] = $interceptor;
41
        }
42
43 8
        return $clone;
44
    }
45
46 6
    public function withHandler(HandlerInterface $handler): self
47
    {
48 6
        $pipeline = clone $this;
49 6
        $pipeline->handler = $handler;
50 6
        return $pipeline;
51
    }
52
53
    /**
54
     * @throws \Throwable
55
     */
56 8
    public function handle(CallContext $context): mixed
57
    {
58 8
        if ($this->handler === null) {
59 2
            throw new InterceptorException('Unable to invoke pipeline without last handler.');
60
        }
61
62 6
        if (isset($this->interceptors[$this->position])) {
63 5
            $interceptor = $this->interceptors[$this->position];
64
65 5
            $this->dispatcher?->dispatch(new InterceptorCalling(context: $context, interceptor: $interceptor));
66
67 5
            return $interceptor->intercept($context, $this->next());
68
        }
69
70 3
        return $this->handler->handle($context);
71
    }
72
73 5
    private function next(): self
74
    {
75 5
        $pipeline = clone $this;
76 5
        ++$pipeline->position;
77 5
        return $pipeline;
78
    }
79
}
80