Passed
Pull Request — master (#1095)
by Aleksei
25:10
created

InterceptorPipeline::handle()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6.0038

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 29
ccs 20
cts 21
cp 0.9524
rs 9.0444
cc 6
nc 5
nop 1
crap 6.0038
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 409
    public function __construct(
32
        private readonly ?EventDispatcherInterface $dispatcher = null
33
    ) {
34 409
    }
35
36 400
    public function addInterceptor(CoreInterceptorInterface|InterceptorInterface $interceptor): void
37
    {
38 400
        $this->interceptors[] = $interceptor;
39
    }
40
41 163
    public function withCore(CoreInterface $core): self
42
    {
43 163
        $pipeline = clone $this;
44 163
        $pipeline->core = $core;
45 163
        $pipeline->handler = null;
46 163
        return $pipeline;
47
    }
48
49 1
    public function withHandler(HandlerInterface $handler): self
50
    {
51 1
        $pipeline = clone $this;
52 1
        $pipeline->handler = $handler;
53 1
        $pipeline->core = null;
54 1
        return $pipeline;
55
    }
56
57
    /**
58
     * @throws \Throwable
59
     */
60 166
    public function callAction(string $controller, string $action, array $parameters = []): mixed
61
    {
62 166
        if ($this->context === null) {
63 166
            return $this->handle(
64 166
                new CallContext(Target::fromPathArray([$controller, $action]), $parameters),
65 166
            );
66
        }
67
68 150
        if ($this->context->getTarget()->getPath() === [$controller, $action]) {
69 149
            return $this->handle($this->context->withArguments($parameters));
70
        }
71
72 1
        return $this->handle(
73 1
            $this->context->withTarget(
74 1
                Target::fromPathArray([$controller, $action]),
75 1
            )->withArguments($parameters)
76 1
        );
77
    }
78
79
    /**
80
     * @throws \Throwable
81
     */
82 167
    public function handle(CallContext $context): mixed
83
    {
84 167
        if ($this->core === null && $this->handler === null) {
85 3
            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 164
        $path = $context->getTarget()->getPath();
89
90 164
        if (isset($this->interceptors[$this->position])) {
91 161
            $interceptor = $this->interceptors[$this->position];
92 161
            $handler = $this->nextWithContext($context);
93
94 161
            $this->dispatcher?->dispatch(
95 161
                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 161
                    controller: $path[0] ?? '',
97 161
                    action: $path[1] ?? '',
98 161
                    parameters: $context->getArguments(),
99 161
                    interceptor: $interceptor,
100 161
                )
101 161
            );
102
103 161
            return $interceptor instanceof CoreInterceptorInterface
104 160
                ? $interceptor->process($path[0] ?? '', $path[1] ?? '', $context->getArguments(), $handler)
105 135
                : $interceptor->intercept($context, $handler);
106
        }
107
108 152
        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 152
            : $this->core->callAction($path[0] ?? '', $path[1] ?? '', $context->getArguments());
111
    }
112
113 161
    private function nextWithContext(CallContext $context): self
114
    {
115 161
        $pipeline = clone $this;
116 161
        $pipeline->context = $context;
117 161
        ++$pipeline->position;
118 161
        return $pipeline;
119
    }
120
}
121