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

RequestHandler   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 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 28
ccs 10
cts 11
cp 0.9091
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 15 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
    /** @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