Test Failed
Pull Request — master (#1095)
by Aleksei
10:19
created

InterceptorPipeline::handle()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 29
rs 9.0444
cc 6
nc 5
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Core;
6
7
use Psr\EventDispatcher\EventDispatcherInterface;
8
use Spiral\Core\Event\InterceptorCalling;
9
use Spiral\Core\Exception\InterceptorException;
10
use Spiral\Interceptors\Context\CallContext;
11
use Spiral\Interceptors\Context\Target;
12
use Spiral\Interceptors\HandlerInterface;
13
use Spiral\Interceptors\InterceptorInterface;
14
15
/**
16
 * Provides the ability to modify the call to the domain core on it's way to the action.
17
 *
18
 * @deprecated use {@see \Spiral\Interceptors\Handler\InterceptorPipeline} instead
19
 */
20
final class InterceptorPipeline implements CoreInterface, HandlerInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Spiral\Core\CoreInterface has been deprecated: Use {@see HandlerInterface} instead. ( Ignorable by Annotation )

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

20
final class InterceptorPipeline implements /** @scrutinizer ignore-deprecated */ CoreInterface, HandlerInterface

This interface has been deprecated. The supplier of the interface has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.

Loading history...
21
{
22
    private ?CoreInterface $core = null;
23
    private ?HandlerInterface $handler = null;
24
25
    /** @var list<CoreInterceptorInterface|InterceptorInterface> */
0 ignored issues
show
Bug introduced by
The type Spiral\Core\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...
26
    private array $interceptors = [];
27
28
    private int $position = 0;
29
    private ?CallContext $context = null;
30
31
    public function __construct(
32
        private readonly ?EventDispatcherInterface $dispatcher = null
33
    ) {
34
    }
35
36
    public function addInterceptor(CoreInterceptorInterface|InterceptorInterface $interceptor): void
37
    {
38
        $this->interceptors[] = $interceptor;
39
    }
40
41
    public function withCore(CoreInterface $core): self
42
    {
43
        $pipeline = clone $this;
44
        $pipeline->core = $core;
45
        $pipeline->handler = null;
46
        return $pipeline;
47
    }
48
49
    public function withHandler(HandlerInterface $handler): self
50
    {
51
        $pipeline = clone $this;
52
        $pipeline->handler = $handler;
53
        $pipeline->core = null;
54
        return $pipeline;
55
    }
56
57
    /**
58
     * @throws \Throwable
59
     */
60
    public function callAction(string $controller, string $action, array $parameters = []): mixed
61
    {
62
        if ($this->context === null) {
63
            return $this->handle(
64
                new CallContext(Target::fromPathArray([$controller, $action]), $parameters),
65
            );
66
        }
67
68
        if ($this->context->getTarget()->getPath() === [$controller, $action]) {
69
            return $this->handle($this->context->withArguments($parameters));
70
        }
71
72
        return $this->handle(
73
            $this->context->withTarget(
74
                Target::fromPathArray([$controller, $action]),
75
            )->withArguments($parameters)
76
        );
77
    }
78
79
    /**
80
     * @throws \Throwable
81
     */
82
    public function handle(CallContext $context): mixed
83
    {
84
        if ($this->core === null && $this->handler === null) {
85
            throw new InterceptorException('Unable to invoke pipeline without last handler.');
0 ignored issues
show
Deprecated Code introduced by
The class Spiral\Core\Exception\InterceptorException has been deprecated: will be removed in Spiral v4.0 Use {@see \Spiral\Interceptors\Exception\InterceptorException} instead. ( Ignorable by Annotation )

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

85
            throw /** @scrutinizer ignore-deprecated */ new InterceptorException('Unable to invoke pipeline without last handler.');
Loading history...
86
        }
87
88
        $path = $context->getTarget()->getPath();
89
90
        if (isset($this->interceptors[$this->position])) {
91
            $interceptor = $this->interceptors[$this->position];
92
            $handler = $this->nextWithContext($context);
93
94
            $this->dispatcher?->dispatch(
95
                new InterceptorCalling(
0 ignored issues
show
Deprecated Code introduced by
The class Spiral\Core\Event\InterceptorCalling has been deprecated: use {@see \Spiral\Interceptors\Event\InterceptorCalling} instead ( Ignorable by Annotation )

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

95
                /** @scrutinizer ignore-deprecated */ new InterceptorCalling(
Loading history...
96
                    controller: $path[0] ?? '',
97
                    action: $path[1] ?? '',
98
                    parameters: $context->getArguments(),
99
                    interceptor: $interceptor,
100
                )
101
            );
102
103
            return $interceptor instanceof CoreInterceptorInterface
104
                ? $interceptor->process($path[0] ?? '', $path[1] ?? '', $context->getArguments(), $handler)
105
                : $interceptor->intercept($context, $handler);
106
        }
107
108
        return $this->core === null
109
            ? $this->handler->handle($context)
0 ignored issues
show
Bug introduced by
The method handle() does not exist on null. ( Ignorable by Annotation )

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

109
            ? $this->handler->/** @scrutinizer ignore-call */ handle($context)

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...
110
            : $this->core->callAction($path[0] ?? '', $path[1] ?? '', $context->getArguments());
111
    }
112
113
    private function nextWithContext(CallContext $context): self
114
    {
115
        $pipeline = clone $this;
116
        $pipeline->context = $context;
117
        ++$pipeline->position;
118
        return $pipeline;
119
    }
120
}
121