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

MiddlewareAggregator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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