Passed
Pull Request — master (#16)
by
unknown
01:45
created

MiddlewareQueue::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
eloc 1
cc 1
nc 1
nop 0
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 Yiisoft\Middleware\Dispatcher\Event\AfterMiddleware;
13
use Yiisoft\Middleware\Dispatcher\Event\BeforeMiddleware;
14
15
final class MiddlewareQueue implements MiddlewareQueueInterface
16
{
17
    /**
18
     * Contains a queue of middleware wrapped in handlers.
19
     * Each handler points to the handler of middleware that will be processed next.
20
     *
21
     * @var RequestHandlerInterface|null queue of middleware
22
     */
23
    private ?RequestHandlerInterface $queue = null;
24
25
    private EventDispatcherInterface $eventDispatcher;
26
27 7
    public function __construct(EventDispatcherInterface $eventDispatcher)
28
    {
29 7
        $this->eventDispatcher = $eventDispatcher;
30 7
    }
31
32 7
    public function build(array $middlewares, RequestHandlerInterface $fallbackHandler): MiddlewareQueueInterface
33
    {
34 7
        $handler = $fallbackHandler;
35 7
        $middlewares = array_reverse($middlewares);
36 7
        $firstMiddleware = array_pop($middlewares);
37 7
        foreach ($middlewares as $middleware) {
38 3
            $handler = $this->wrap($middleware, $handler);
39
        }
40
41 7
        $new = clone $this;
42 7
        $new->queue = $this->wrap($firstMiddleware, $handler);
43
44 7
        return $new;
45
    }
46
47 7
    public function handle(ServerRequestInterface $request): ResponseInterface
48
    {
49 7
        if ($this->isEmpty()) {
50
            throw new \RuntimeException('Queue is empty.');
51
        }
52
53 7
        return $this->queue->handle($request);
54
    }
55
56 7
    public function reset(): void
57
    {
58 7
        $this->queue = null;
59 7
    }
60
61 7
    public function isEmpty(): bool
62
    {
63 7
        return $this->queue === null;
64
    }
65
66
    /**
67
     * Wraps handler by middlewares
68
     */
69 7
    private function wrap(MiddlewareInterface $middleware, RequestHandlerInterface $handler): RequestHandlerInterface
70
    {
71 7
        return new class($middleware, $handler, $this->eventDispatcher) implements RequestHandlerInterface {
72
            private MiddlewareInterface $middleware;
73
            private RequestHandlerInterface $handler;
74
            private EventDispatcherInterface $eventDispatcher;
75
76
            public function __construct(MiddlewareInterface $middleware, RequestHandlerInterface $handler, EventDispatcherInterface $eventDispatcher)
77
            {
78 7
                $this->middleware = $middleware;
79 7
                $this->handler = $handler;
80 7
                $this->eventDispatcher = $eventDispatcher;
81 7
            }
82
83
            public function handle(ServerRequestInterface $request): ResponseInterface
84
            {
85 7
                $this->eventDispatcher->dispatch(new BeforeMiddleware($this->middleware, $request));
86
87 7
                $response = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
88
                try {
89 7
                    return $response = $this->middleware->process($request, $this->handler);
90
                } finally {
91 7
                    $this->eventDispatcher->dispatch(new AfterMiddleware($this->middleware, $response));
92
                }
93
            }
94
        };
95
    }
96
}
97