RequestHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 24
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 14 3
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