Handler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 49
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 0 8 2
A nextHandler() 0 7 1
1
<?php
2
3
namespace Equip\Dispatch;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Server\RequestHandlerInterface;
8
9
class Handler implements RequestHandlerInterface
10
{
11
    /**
12
     * @var array
13
     */
14
    private $middleware;
15
16
    /**
17
     * @var callable
18
     */
19
    private $default;
20
21
    /**
22
     * @var integer
23
     */
24
    private $index = 0;
25
26
    /**
27
     * @param array $middleware
28
     * @param callable $default
29
     */
30 4
    public function __construct(array $middleware, callable $default)
31
    {
32 4
        $this->middleware = $middleware;
33 4
        $this->default = $default;
34 4
    }
35
36 4
    public function handle(ServerRequestInterface $request): ResponseInterface
37
    {
38 4
        if (empty($this->middleware[$this->index])) {
39 4
            return call_user_func($this->default, $request);
40
        }
41
42 2
        return $this->middleware[$this->index]->process($request, $this->nextHandler());
43
    }
44
45
    /**
46
     * Get a handler pointing to the next middleware.
47
     *
48
     * @return static
49
     */
50 2
    private function nextHandler()
51
    {
52 2
        $copy = clone $this;
53 2
        $copy->index++;
54
55 2
        return $copy;
56
    }
57
}
58