Passed
Pull Request — master (#27)
by Dmitriy
02:04
created

MiddlewareStack.php$0 ➔ __construct()   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 3
cts 3
cp 1
rs 10
cc 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Middleware\Dispatcher;
6
7
use Psr\EventDispatcher\EventDispatcherInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use RuntimeException;
13
use Yiisoft\Middleware\Dispatcher\Event\AfterMiddleware;
14
use Yiisoft\Middleware\Dispatcher\Event\BeforeMiddleware;
15
16
final class MiddlewareStack implements RequestHandlerInterface
17
{
18
    /**
19
     * Contains a stack of middleware wrapped in handlers.
20
     * Each handler points to the handler of middleware that will be processed next.
21
     *
22
     * @var RequestHandlerInterface|null stack of middleware
23
     */
24
    private ?RequestHandlerInterface $stack = null;
25
    private EventDispatcherInterface $eventDispatcher;
26
    private RequestHandlerInterface $fallbackHandler;
27
    private array $middlewares;
28
29
    /**
30
     * @param EventDispatcherInterface $eventDispatcher Event dispatcher to use for triggering before/after middleware
31
     * events.
32
     */
33 8
    public function __construct(array $middlewares, RequestHandlerInterface $fallbackHandler, EventDispatcherInterface $eventDispatcher)
34
    {
35 8
        if ($middlewares === []) {
36 1
            throw new RuntimeException('Stack is empty.');
37
        }
38
39 7
        $this->middlewares = $middlewares;
40 7
        $this->fallbackHandler = $fallbackHandler;
41 7
        $this->eventDispatcher = $eventDispatcher;
42 7
    }
43
44 7
    public function handle(ServerRequestInterface $request): ResponseInterface
45
    {
46 7
        if ($this->stack === null) {
47 7
            $this->build();
48
        }
49
50
        /** @psalm-suppress PossiblyNullReference */
51 7
        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

51
        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...
52
    }
53
54 7
    private function build(): void
55
    {
56 7
        $handler = $this->fallbackHandler;
57 7
        foreach ($this->middlewares as $middleware) {
58 7
            $handler = $this->wrap($middleware, $handler);
59
        }
60
61 7
        $this->stack = $handler;
62 7
    }
63
64
    /**
65
     * Wrap handler by middlewares.
66
     */
67 7
    private function wrap(MiddlewareInterface $middleware, RequestHandlerInterface $handler): RequestHandlerInterface
68
    {
69 7
        return new class($middleware, $handler, $this->eventDispatcher) implements RequestHandlerInterface {
70
            private MiddlewareInterface $middleware;
71
            private RequestHandlerInterface $handler;
72
            private EventDispatcherInterface $eventDispatcher;
73
74
            public function __construct(
75
                MiddlewareInterface $middleware,
76
                RequestHandlerInterface $handler,
77
                EventDispatcherInterface $eventDispatcher
78
            ) {
79 7
                $this->middleware = $middleware;
80 7
                $this->handler = $handler;
81 7
                $this->eventDispatcher = $eventDispatcher;
82 7
            }
83
84
            public function handle(ServerRequestInterface $request): ResponseInterface
85
            {
86 7
                $this->eventDispatcher->dispatch(new BeforeMiddleware($this->middleware, $request));
87
88
                try {
89 7
                    return $response = $this->middleware->process($request, $this->handler);
90
                } finally {
91 7
                    $this->eventDispatcher->dispatch(new AfterMiddleware($this->middleware, $response ?? null));
92
                }
93
            }
94
        };
95
    }
96
}
97