Passed
Push — master ( b913e4...d67be8 )
by Jelmer
04:50
created

Delegate   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90.91%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 28
ccs 10
cts 11
cp 0.9091
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A process() 0 15 3
1
<?php declare(strict_types = 1);
2
3
namespace jschreuder\Middle;
4
5
use Interop\Http\ServerMiddleware\DelegateInterface;
6
use Interop\Http\ServerMiddleware\MiddlewareInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
10
// @todo prevent calling ->process() twice
11
final class Delegate implements DelegateInterface
12
{
13
    /** @var  \SplStack */
14
    private $stack;
15
16
    private $called = false;
17
18 6
    public function __construct(\SplStack $stack)
19
    {
20 6
        $this->stack = $stack;
21 6
    }
22
23 4
    public function process(ServerRequestInterface $request): ResponseInterface
24
    {
25 4
        if ($this->stack->count() === 0) {
26 2
            throw new \RuntimeException('No more middleware\'s to call on.');
27
        }
28 4
        if ($this->called) {
29
            throw new \RuntimeException('Already processed, cannot be ran twice.');
30
        }
31
32
        /** @var  MiddlewareInterface $next */
33 4
        $next = $this->stack->pop();
34 4
        $this->called = true;
35
36 4
        return $next->process($request, new self($this->stack));
37
    }
38
}
39