Passed
Push — master ( 8f9974...195c48 )
by Divine Niiquaye
13:24
created

RouteHandler::__construct()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.009

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 25
ccs 13
cts 14
cp 0.9286
crap 5.009
rs 9.5222
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Rade\Handler;
19
20
use Biurad\Http\Request;
21
use Flight\Routing\Handlers\RouteHandler as BaseRouteHandler;
22
use Flight\Routing\Route;
23
use Psr\Http\Message\ServerRequestInterface;
24
use Rade\DI\Container;
25
use Rade\Event\ControllerEvent;
26
use Rade\Event\RequestEvent;
27
28
/**
29
 * Default route's handler for rade framework.
30
 *
31
 * @author Divine Niiquaye Ibok <[email protected]>
32
 */
33
class RouteHandler extends BaseRouteHandler
34
{
35 4
    public function __construct(Container $container)
36
    {
37 4
        $handlerResolver = static function ($handler, array $parameters) use ($container) {
38 4
            $request = $parameters[ServerRequestInterface::class] ?? null;
39
40 4
            if ($container->has('events.dispatcher')) {
41 4
                $container->get('events.dispatcher')->dispatch($event = new RequestEvent($container, $request));
0 ignored issues
show
Bug introduced by
It seems like $request can also be of type null; however, parameter $request of Rade\Event\RequestEvent::__construct() does only seem to accept Psr\Http\Message\ServerRequestInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
                $container->get('events.dispatcher')->dispatch($event = new RequestEvent($container, /** @scrutinizer ignore-type */ $request));
Loading history...
Bug introduced by
The method dispatch() does not exist on Rade\DI\Container. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
                $container->get('events.dispatcher')->/** @scrutinizer ignore-call */ dispatch($event = new RequestEvent($container, $request));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
43 4
                if ($event->hasResponse()) {
44
                    return $event->getResponse();
45
                }
46
47 4
                $request = $event->getRequest();
48 4
                $container->get('events.dispatcher')->dispatch($event = new ControllerEvent($container, $handler, $parameters, $request));
49 4
                [$handler, $parameters] = [$event->getController(), $event->getArguments()];
50
            }
51
52 4
            if ($request instanceof Request && $container->has('request_stack')) {
53 4
                $container->get('request_stack')->push($request->getRequest());
0 ignored issues
show
Bug introduced by
The method push() does not exist on Rade\DI\Container. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
                $container->get('request_stack')->/** @scrutinizer ignore-call */ push($request->getRequest());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
            }
55
56 4
            return $container->getResolver()->resolve($handler, $parameters);
57
        };
58
59 4
        parent::__construct($container->get('psr17.factory'), $handlerResolver);
60 4
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 4
    protected function resolveArguments(ServerRequestInterface $request, Route $route): array
66
    {
67 4
        $parameters = $route->getArguments();
68 4
        $parameters[\get_class($request)] = $request;
69
70 4
        foreach (\class_implements($request) as $psr7Interface) {
71 4
            if (\Stringable::class === $psr7Interface) {
72 4
                continue;
73
            }
74
75 4
            $parameters[$psr7Interface] = $request;
76
        }
77
78 4
        return $parameters;
79
    }
80
}
81