Completed
Push — master ( 40c477...3d9760 )
by Woody
01:36
created

Delegate::nextDelegate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Northwoods\Broker;
4
5
use Interop\Http\ServerMiddleware\DelegateInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use SplObjectStorage;
8
9
class Delegate implements DelegateInterface
10
{
11
    /**
12
     * @var SplObjectStorage
13
     */
14
    private $middleware;
15
16
    /**
17
     * @var DelegateInterface $nextDelegate
18
     */
19
    private $nextDelegate;
20
21 4
    public function __construct(SplObjectStorage $middleware, DelegateInterface $nextDelegate)
22
    {
23 4
        $this->middleware = clone $middleware;
24 4
        $this->nextDelegate = $nextDelegate;
25 4
    }
26
27
    // DelegateInterface
28 4
    public function process(ServerRequestInterface $request)
29
    {
30
        /** @var MiddlewareInterface */
31 4
        $middleware = $this->middleware->current();
32
33 4
        if (empty($middleware)) {
34 4
            return $this->nextDelegate->process($request);
35
        }
36
37
        /** @var callable */
38
        $condition = $this->middleware[$middleware];
39
40
        /** @var DelegateInterface */
41
        $delegate = $this->nextDelegate();
42
43
        if ($condition($request)) {
44
            return $middleware->process($request, $delegate);
45
        } else {
46
            return $delegate->process($request);
47
        }
48
    }
49
50
    /**
51
     * @return DelegateInterface
52
     */
53
    private function nextDelegate()
54
    {
55
        $copy = clone $this;
56
        $copy->middleware->next();
57
58
        return $copy;
59
    }
60
}
61