Passed
Push — develop ( df587f...f7c208 )
by Schlaefer
34s
created

RequestHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * @author  PhileCMS
4
 * @link    https://philecms.com
5
 * @license http://opensource.org/licenses/MIT
6
 */
7
8
namespace Phile\Http;
9
10
use Interop\Http\Factory\ResponseFactoryInterface;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
use Psr\Http\Server\RequestHandlerInterface;
14
15
/**
16
 * Implements a PSR-15 compatible request handler
17
 */
18
class RequestHandler implements RequestHandlerInterface
19
{
20
    /** @var Iterator the middleware to process */
21
    protected $queue;
22
23
    /** @var ResponseFactoryInterface PSR-17 HTTP response factory */
24
    protected $responseFactory;
25
26
    /** @var integer level */
27
    protected $level = 0;
28
29
    /**
30
     * Constructor
31
     *
32
     * @param ResponseFactoryInterface $responseFactory
33
     */
34 4
    public function __construct(MiddlewareQueue $queue, ResponseFactoryInterface $responseFactory)
35
    {
36 4
        $this->responseFactory = $responseFactory;
37 4
        $this->queue = $queue->getIterator();
0 ignored issues
show
Documentation Bug introduced by
It seems like $queue->getIterator() of type object<Traversable> is incompatible with the declared type object<Phile\Http\Iterator> of property $queue.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38 4
    }
39
40
    /**
41
     * Handle the request and return a response.
42
     *
43
     * @param ServerRequestInterface $request
44
     * @return ResponseInterface
45
     */
46 4
    public function handle(ServerRequestInterface $request): ResponseInterface
47
    {
48 4
        if ($this->level++ === 0) {
49 4
            $this->queue->rewind();
50
        }
51 4
        $current = $this->queue->current();
52 4
        if (!$current) {
53
            return $this->responseFactory->createResponse();
54
        }
55 4
        $this->queue->next();
56 4
        $result = call_user_func_array([$current, 'process'], [$request, $this]);
57 4
        $this->level--;
58 4
        return $result;
59
    }
60
}
61