Passed
Pull Request — master (#1095)
by Aleksei
12:04
created

InterceptorPipeline::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 0
c 0
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 1
crap 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 408
    public function __construct(
32
        private readonly ?EventDispatcherInterface $dispatcher = null
33
    ) {
34 408
    }
35
36 399
    public function addInterceptor(CoreInterceptorInterface|InterceptorInterface $interceptor): void
37
    {
38 399
        $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
    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 165
    public function callAction(string $controller, string $action, array $parameters = []): mixed
61
    {
62 165
        if ($this->context === null) {
63 165
            return $this->handle(
64 165
                new CallContext(Target::fromPathArray([$controller, $action]), $parameters),
65 165
            );
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 166
    public function handle(CallContext $context): mixed
83
    {
84 166
        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 163
        $path = $context->getTarget()->getPath();
89
90 163
        if (isset($this->interceptors[$this->position])) {
91 160
            $interceptor = $this->interceptors[$this->position];
92 160
            $handler = $this->nextWithContext($context);
93
94 160
            $this->dispatcher?->dispatch(
95 160
                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 160
                    controller: $path[0] ?? '',
97 160
                    action: $path[1] ?? '',
98 160
                    parameters: $context->getArguments(),
99 160
                    interceptor: $interceptor,
100 160
                )
101 160
            );
102
103 160
            return $interceptor instanceof CoreInterceptorInterface
104 160
                ? $interceptor->process($path[0] ?? '', $path[1] ?? '', $context->getArguments(), $handler)
105 134
                : $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 160
    private function nextWithContext(CallContext $context): self
114
    {
115 160
        $pipeline = clone $this;
116 160
        $pipeline->context = $context;
117 160
        ++$pipeline->position;
118 160
        return $pipeline;
119
    }
120
}
121