RequestHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
    private \SplStack $stack;
13
    private bool $called = false;
14
15 6
    public function __construct(\SplStack $stack)
16
    {
17 6
        $this->stack = $stack;
18
    }
19
20 4
    public function handle(ServerRequestInterface $request): ResponseInterface
21
    {
22 4
        if ($this->stack->count() === 0) {
23 1
            throw new \RuntimeException('No more middleware\'s to call on.');
24
        }
25 4
        if ($this->called) {
26 1
            throw new \RuntimeException('Already processed, cannot be ran twice.');
27
        }
28
29
        /** @var  MiddlewareInterface $next */
30 4
        $next = $this->stack->pop();
31 4
        $this->called = true;
32
33 4
        return $next->process($request, new self($this->stack));
34
    }
35
}
36