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

Delegate::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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