Next   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 13
c 0
b 0
f 0
dl 0
loc 41
ccs 13
cts 13
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 15 3
1
<?php declare(strict_types=1);
2
3
namespace Phact\Router\Invoker;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use Psr\Http\Server\MiddlewareInterface;
7
use Psr\Http\Server\RequestHandlerInterface;
8
use Psr\Http\Message\ResponseInterface;
9
10
class Next implements RequestHandlerInterface
11
{
12
    /**
13
     * @var RequestHandlerInterface
14
     */
15
    private $handler;
16
17
    /**
18
     * @var MiddlewareInterface[]
19
     */
20
    private $middlewareStack;
21
22
    /**
23
     * Next constructor.
24
     * @param RequestHandlerInterface $handler
25
     * @param array $middlewareStack
26
     */
27 17
    public function __construct(RequestHandlerInterface $handler, array $middlewareStack = [])
28
    {
29 17
        $this->handler = $handler;
30 17
        $this->middlewareStack = $middlewareStack;
31 17
    }
32
33
    /**
34
     * @inheritDoc
35
     */
36 17
    public function handle(ServerRequestInterface $request) : ResponseInterface
37
    {
38 17
        if ($this->middlewareStack === null) {
39 1
            throw new \LogicException('Next handler already called');
40
        }
41
42 17
        if (empty($this->middlewareStack)) {
43 15
            return $this->handler->handle($request);
44
        }
45
46 6
        $middleware = array_shift($this->middlewareStack);
47 6
        $next = clone $this;
48 6
        $this->middlewareStack = null;
49
50 6
        return $middleware->process($request, $next);
51
    }
52
}
53