Passed
Pull Request — master (#190)
by Dmitriy
04:22 queued 01:36
created

MiddlewareStack::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Queue\Middleware;
6
7
use Closure;
8
9
final class MiddlewareStack implements MessageHandlerInterface
10
{
11
    /**
12
     * Contains a stack of middleware wrapped in handlers.
13
     * Each handler points to the handler of middleware that will be processed next.
14
     *
15
     * @var MessageHandlerInterface|null stack of middleware
16
     */
17
    private ?MessageHandlerInterface $stack = null;
18
19
    /**
20
     * @param Closure[] $middlewares Middlewares.
21
     * @param MessageHandlerInterface $finishHandler Fallback handler
22
     * events.
23
     */
24 33
    public function __construct(
25
        private array $middlewares,
26
        private MessageHandlerInterface $finishHandler,
27
    ) {
28 33
    }
29
30 33
    public function handle(Request $request): Request
31
    {
32 33
        if ($this->stack === null) {
33 33
            $this->build();
34
        }
35
36
        /** @psalm-suppress PossiblyNullReference */
37 33
        return $this->stack->handle($request);
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

37
        return $this->stack->/** @scrutinizer ignore-call */ handle($request);

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...
38
    }
39
40 33
    private function build(): void
41
    {
42 33
        $handler = $this->finishHandler;
43
44 33
        foreach ($this->middlewares as $middleware) {
45 21
            $handler = $this->wrap($middleware, $handler);
46
        }
47
48 33
        $this->stack = $handler;
49
    }
50
51
    /**
52
     * Wrap handler by middlewares.
53
     */
54 21
    private function wrap(Closure $middlewareFactory, MessageHandlerInterface $handler): MessageHandlerInterface
55
    {
56 21
        return new class ($middlewareFactory, $handler) implements MessageHandlerInterface {
57
            private ?MiddlewareInterface $middleware = null;
58
59
            public function __construct(
60
                private Closure $middlewareFactory,
61
                private MessageHandlerInterface $handler,
62
            ) {
63 21
            }
64
65
            public function handle(Request $request): Request
66
            {
67 21
                if ($this->middleware === null) {
68 21
                    $this->middleware = ($this->middlewareFactory)();
69
                }
70
71 21
                return $this->middleware->process($request, $this->handler);
0 ignored issues
show
Bug introduced by
The method process() 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

71
                return $this->middleware->/** @scrutinizer ignore-call */ process($request, $this->handler);

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...
72
            }
73 21
        };
74
    }
75
}
76