Passed
Push — master ( deef18...62a4bf )
by Jelmer
10:19
created

RequestHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php declare(strict_types = 1);
2
3
namespace jschreuder\Middle;
4
5
use Psr\Http\Server\MiddlewareInterface;
6
use Psr\Http\Server\RequestHandlerInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
10
final class RequestHandler implements RequestHandlerInterface
11
{
12
    /** @var  \SplStack */
13
    private $stack;
14
15
    private $called = false;
16
17 6
    public function __construct(\SplStack $stack)
18
    {
19 6
        $this->stack = $stack;
20 6
    }
21
22 4
    public function handle(ServerRequestInterface $request): ResponseInterface
23
    {
24 4
        if ($this->stack->count() === 0) {
25 2
            throw new \RuntimeException('No more middleware\'s to call on.');
26
        }
27 4
        if ($this->called) {
28
            throw new \RuntimeException('Already processed, cannot be ran twice.');
29
        }
30
31
        /** @var  MiddlewareInterface $next */
32 4
        $next = $this->stack->pop();
33 4
        $this->called = true;
34
35 4
        return $next->process($request, new self($this->stack));
36
    }
37
}
38