Passed
Pull Request — master (#1104)
by Aleksei
26:20
created

InterceptorPipeline::callAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.9666
cc 3
nc 3
nop 3
crap 3
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 445
    public function __construct(
32
        private readonly ?EventDispatcherInterface $dispatcher = null
33
    ) {
34 445
    }
35
36 436
    public function addInterceptor(CoreInterceptorInterface|InterceptorInterface $interceptor): void
37
    {
38 436
        $this->interceptors[] = $interceptor;
39
    }
40
41 436
    public function withCore(CoreInterface $core): self
42
    {
43 436
        $pipeline = clone $this;
44 436
        $pipeline->core = $core;
45 436
        $pipeline->handler = null;
46 436
        return $pipeline;
47
    }
48
49 34
    public function withHandler(HandlerInterface $handler): self
50
    {
51 34
        $pipeline = clone $this;
52 34
        $pipeline->handler = $handler;
53 34
        $pipeline->core = null;
54 34
        return $pipeline;
55
    }
56
57
    /**
58
     * @throws \Throwable
59
     */
60 170
    public function callAction(string $controller, string $action, array $parameters = []): mixed
61
    {
62 170
        if ($this->context === null) {
63 26
            return $this->handle(
64 26
                new CallContext(Target::fromPathArray([$controller, $action]), $parameters),
65 26
            );
66
        }
67
68 154
        if ($this->context->getTarget()->getPath() === [$controller, $action]) {
69 153
            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 171
    public function handle(CallContext $context): mixed
83
    {
84 171
        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 168
        $path = $context->getTarget()->getPath();
89
90 168
        if (isset($this->interceptors[$this->position])) {
91 165
            $interceptor = $this->interceptors[$this->position];
92 165
            $handler = $this->nextWithContext($context);
93
94 165
            $this->dispatcher?->dispatch(
95 165
                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 165
                    controller: $path[0] ?? '',
97 165
                    action: $path[1] ?? '',
98 165
                    parameters: $context->getArguments(),
99 165
                    interceptor: $interceptor,
100 165
                )
101 165
            );
102
103 165
            return $interceptor instanceof CoreInterceptorInterface
104 164
                ? $interceptor->process($path[0] ?? '', $path[1] ?? '', $context->getArguments(), $handler)
105 139
                : $interceptor->intercept($context, $handler);
106
        }
107
108 156
        return $this->core === null
109 26
            ? $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 156
            : $this->core->callAction($path[0] ?? '', $path[1] ?? '', $context->getArguments());
111
    }
112
113 165
    private function nextWithContext(CallContext $context): self
114
    {
115 165
        $pipeline = clone $this;
116 165
        $pipeline->context = $context;
117 165
        ++$pipeline->position;
118 165
        return $pipeline;
119
    }
120
}
121