Test Failed
Push — master ( b22efa...070fdf )
by Felipe
04:16 queued 02:04
created

MiddlewareAggregator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 38
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A process() 0 18 2
1
<?php
2
3
/**
4
 * This file is part of coisa/http.
5
 *
6
 * (c) Felipe Sayão Lobato Abreu <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
declare(strict_types=1);
13
14
namespace CoiSA\Http\Middleware;
15
16
use CoiSA\Http\Handler\MiddlewareHandler;
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use Psr\Http\Server\MiddlewareInterface;
20
use Psr\Http\Server\RequestHandlerInterface;
21
22
/**
23
 * Class MiddlewareAggregator
24
 *
25
 * @package CoiSA\Http\Middleware
26
 */
27
final class MiddlewareAggregator implements MiddlewareInterface
28
{
29
    /**
30
     * @var MiddlewareInterface[]
31
     */
32
    private $middlewares;
33
34
    /**
35
     * MiddlewareAggregator constructor.
36
     *
37
     * @param MiddlewareInterface ...$middlewares
38
     */
39
    public function __construct(MiddlewareInterface ...$middlewares)
40
    {
41
        $this->middlewares = $middlewares;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
48
    {
49
        $middleware = \current($this->middlewares);
50
51
        if (!$middleware) {
52
            \reset($this->middlewares);
53
54
            return $handler->handle($request);
55
        }
56
57
        \next($this->middlewares);
58
59
        $requestHandler = new MiddlewareHandler(
60
            $this,
61
            $handler
62
        );
63
64
        return $middleware->process($request, $requestHandler);
65
    }
66
}
67