Completed
Push — master ( 79b5b2...0a236b )
by n
01:55
created

LazyRequestHandler::handle()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 14
cts 14
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 17
nc 3
nop 1
crap 3
1
<?php
2
3
namespace N1215\Jugoya;
4
5
use Interop\Http\Server\MiddlewareInterface;
6
use Interop\Http\Server\RequestHandlerInterface;
7
use N1215\Jugoya\Resolver\MiddlewareResolverInterface;
8
use N1215\Jugoya\Resolver\RequestHandlerResolverInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
12
class LazyRequestHandler implements RequestHandlerInterface
13
{
14
    /**
15
     * @var RequestHandlerResolverInterface
16
     */
17
    private $handlerResolver;
18
19
    /**
20
     * @var MiddlewareResolverInterface
21
     */
22
    private $middlewareResolver;
23
24
    /**
25
     * @var RequestHandlerInterface|callable|string $coreHandlerRef
26
     */
27
    private $coreHandlerRef;
28
29
    /**
30
     * @var MiddlewareInterface[]|callable[]|string[]
31
     */
32
    private $middlewareRefs = [];
33
34
    /**
35
     * @param RequestHandlerResolverInterface $handlerResolver
36
     * @param MiddlewareResolverInterface $middlewareResolver
37
     * @param RequestHandlerInterface|callable|string $coreHandlerRef
38
     * @param MiddlewareInterface[]|callable[]|string[] $middlewareRefs
39
     */
40 6
    public function __construct(
41
        RequestHandlerResolverInterface $handlerResolver,
42
        MiddlewareResolverInterface $middlewareResolver,
43
        $coreHandlerRef,
44
        array $middlewareRefs
45
    ) {
46 6
        $this->handlerResolver = $handlerResolver;
47 6
        $this->middlewareResolver = $middlewareResolver;
48 6
        $this->coreHandlerRef = $coreHandlerRef;
0 ignored issues
show
Documentation Bug introduced by
It seems like $coreHandlerRef can also be of type object<Interop\Http\Serv...equestHandlerInterface>. However, the property $coreHandlerRef is declared as type callable. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
49 6
        $this->middlewareRefs = $middlewareRefs;
0 ignored issues
show
Documentation Bug introduced by
It seems like $middlewareRefs of type array<integer,object<Int...areInterface>|callable> is incompatible with the declared type array<integer,callable> of property $middlewareRefs.

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...
50 6
    }
51
52
    /**
53
     * @param ServerRequestInterface $request
54
     * @return ResponseInterface
55
     */
56 6
    public function handle(ServerRequestInterface $request): ResponseInterface
57
    {
58 6
        $count = count($this->middlewareRefs);
59
60 6
        $coreHandler = $this->handlerResolver->resolve($this->coreHandlerRef);
61
        switch ($count) {
62 6
            case 0:
63 1
                return $coreHandler->handle($request);
64
65 5
            case 1:
66 1
                $middleware = $this->middlewareResolver->resolve($this->middlewareRefs[0]);
67 1
                return $middleware->process($request, $coreHandler);
68
69
            default:
70
                /** @var RequestHandlerInterface $handler */
71 4
                $handler = array_reduce(
72 4
                    array_reverse($this->middlewareRefs),
73 4
                    function(RequestHandlerInterface $handler, $middlewareRef) {
74 4
                        $middleware = $this->middlewareResolver->resolve($middlewareRef);
75 4
                        return new RequestHandler($handler, [$middleware]);
76 4
                    },
77
                    $coreHandler
78
                );
79
80
                return $handler->handle($request);
81
        }
82
    }
83
}
84