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

Delegate   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 47.06%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 52
ccs 8
cts 17
cp 0.4706
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A process() 0 21 3
A nextDelegate() 0 7 1
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