StackRunner::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
crap 1
1
<?php
2
3
namespace IdNet\StackRunner;
4
5
use Interop\Http\Factory\ResponseFactoryInterface;
6
use Interop\Http\Server\RequestHandlerInterface;
7
use Invoker\InvokerInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
11
class StackRunner implements RequestHandlerInterface
12
{
13
    /** @var InvokerInterface */
14
    protected $invoker;
15
16
    protected $stack = [];
17
18
    protected $current = 0;
19
20
    /** @var ResponseFactoryInterface */
21
    protected $responseFactory;
22
23 2
    public function __construct(
24
        array $stack,
25
        InvokerInterface $invoker,
26
        ResponseFactoryInterface $responseFactory
27
    ) {
28 2
        $this->invoker = $invoker;
29 2
        $this->stack = $stack;
30 2
        $this->responseFactory = $responseFactory;
31 2
    }
32
33 2
    public function handle(ServerRequestInterface $request): ResponseInterface
34
    {
35 2
        if (!isset($this->stack[$this->current])) {
36 1
            return $this->responseFactory->createResponse();
37
        }
38
39 1
        $middleware = $this->stack[$this->current];
40 1
        $this->current++;
41
42 1
        return $this->invoker->call([$middleware, 'process'], [
43 1
            'request' => $request,
44 1
            'handler' => $this,
45
        ]);
46
    }
47
}
48