Passed
Pull Request — master (#225)
by
unknown
17:44 queued 07:08
created

MiddlewareHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 24
rs 10
c 1
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A handle() 0 8 1
1
<?php
2
3
namespace Yiisoft\Yii\Web\RequestHandler;
4
5
use Psr\EventDispatcher\EventDispatcherInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Server\MiddlewareInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
use Yiisoft\Yii\Web\Event\AfterMiddleware;
11
use Yiisoft\Yii\Web\Event\BeforeMiddleware;
12
13
final class MiddlewareHandler implements RequestHandlerInterface
14
{
15
    private MiddlewareInterface $middleware;
16
    private RequestHandlerInterface $handler;
17
    private EventDispatcherInterface $dispatcher;
18
19
    public function __construct(
20
        MiddlewareInterface $middleware,
21
        RequestHandlerInterface $handler,
22
        EventDispatcherInterface $dispatcher
23
    ) {
24
        $this->middleware = $middleware;
25
        $this->handler = $handler;
26
        $this->dispatcher = $dispatcher;
27
    }
28
29
    public function handle(ServerRequestInterface $request): ResponseInterface
30
    {
31
        $this->dispatcher->dispatch(new BeforeMiddleware($this->middleware, $request));
32
33
        try {
34
            return $response = $this->middleware->process($request, $this->handler);
35
        } finally {
36
            $this->dispatcher->dispatch(new AfterMiddleware($this->middleware, $response));
37
        }
38
    }
39
}
40