Completed
Push — master ( 73a483...7cce89 )
by Woody
03:00
created

Delegate   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
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 56
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 process() 0 8 2
A nextDelegate() 0 7 1
1
<?php
2
3
namespace Equip\Dispatch;
4
5
use Interop\Http\Middleware\DelegateInterface;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
class Delegate implements DelegateInterface
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 2
    public function __construct(array $middleware, callable $default)
31
    {
32 2
        $this->middleware = $middleware;
33 2
        $this->default = $default;
34 2
    }
35
36
    /**
37
     * Process the request using the current middleware.
38
     *
39
     * @param RequestInterface $request
40
     *
41
     * @return ResponseInterface
42
     */
43 2
    public function process(RequestInterface $request)
44
    {
45 2
        if (empty($this->middleware[$this->index])) {
46 2
            return call_user_func($this->default, $request);
47
        }
48
49 1
        return $this->middleware[$this->index]->process($request, $this->nextDelegate());
50
    }
51
52
    /**
53
     * Get a delegate pointing to the next middleware.
54
     *
55
     * @return static
56
     */
57 1
    private function nextDelegate()
58
    {
59 1
        $copy = clone $this;
60 1
        $copy->index++;
61
62 1
        return $copy;
63
    }
64
}
65