Passed
Pull Request — master (#195)
by Dmitriy
02:34
created

RouterCollector::getSummary()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.0538

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 2
nop 0
dl 0
loc 23
ccs 7
cts 18
cp 0.3889
crap 5.0538
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router\Debug;
6
7
use Psr\Container\ContainerInterface;
8
use ReflectionObject;
9
use Yiisoft\Router\CurrentRoute;
10
use Yiisoft\Router\Route;
11
use Yiisoft\Router\RouteCollectionInterface;
12
use Yiisoft\Yii\Debug\Collector\CollectorTrait;
13
use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface;
14
15
final class RouterCollector implements SummaryCollectorInterface
16
{
17
    use CollectorTrait;
18
19
    private float $matchTime = 0;
20
21 1
    public function __construct(private ContainerInterface $container)
22
    {
23 1
    }
24
25 1
    public function collect(float $matchTime): void
26
    {
27 1
        if (!$this->isActive()) {
28
            return;
29
        }
30 1
        $this->matchTime = $matchTime;
31
    }
32
33 1
    public function getCollected(): array
34
    {
35
        /**
36
         * @var RouteCollectionInterface|null $routeCollection
37
         */
38 1
        $routeCollection = $this->container->has(RouteCollectionInterface::class)
39 1
            ? $this->container->get(RouteCollectionInterface::class)
40
            : null;
41
42 1
        $currentRoute = $this->getCurrentRoute();
43 1
        $route = $this->getRouteByCurrentRoute($currentRoute);
44 1
        [$middlewares, $action] = $this->getMiddlewaresAndAction($route);
45
46 1
        $result = [
47 1
            'currentRoute' => null,
48 1
        ];
49 1
        if ($currentRoute !== null && $route !== null) {
50
            $result['currentRoute'] = [
51
                'matchTime' => $this->matchTime,
52
                'name' => $route->getData('name'),
53
                'pattern' => $route->getData('pattern'),
54
                'arguments' => $currentRoute->getArguments(),
55
                'host' => $route->getData('host'),
56
                'uri' => (string) $currentRoute->getUri(),
57
                'action' => $action,
58
                'middlewares' => $middlewares,
59
            ];
60
        }
61 1
        if ($routeCollection !== null) {
62 1
            $result['routesTree'] = $routeCollection->getRouteTree();
63 1
            $result['routes'] = $routeCollection->getRoutes();
64 1
            $result['routeTime'] = $this->matchTime;
65
        }
66 1
        return $result;
67
    }
68
69 1
    public function getSummary(): array
70
    {
71 1
        $currentRoute = $this->getCurrentRoute();
72 1
        $route = $this->getRouteByCurrentRoute($currentRoute);
73
74 1
        if ($currentRoute === null || $route === null) {
75 1
            return [
76 1
                'router' => null,
77 1
            ];
78
        }
79
80
        [$middlewares, $action] = $this->getMiddlewaresAndAction($route);
81
82
        return [
83
            'router' => [
84
                'matchTime' => $this->matchTime,
85
                'name' => $route->getData('name'),
86
                'pattern' => $route->getData('pattern'),
87
                'arguments' => $currentRoute->getArguments(),
88
                'host' => $route->getData('host'),
89
                'uri' => (string) $currentRoute->getUri(),
90
                'action' => $action,
91
                'middlewares' => $middlewares,
92
            ],
93
        ];
94
    }
95
96
    /**
97
     * @psalm-suppress MixedReturnStatement, MixedInferredReturnType
98
     */
99 1
    private function getCurrentRoute(): ?CurrentRoute
100
    {
101 1
        return $this->container->has(CurrentRoute::class) ? $this->container->get(CurrentRoute::class) : null;
102
    }
103
104 1
    private function getRouteByCurrentRoute(?CurrentRoute $currentRoute): ?Route
105
    {
106 1
        if ($currentRoute === null) {
107
            return null;
108
        }
109 1
        $reflection = new ReflectionObject($currentRoute);
110
111 1
        $reflectionProperty = $reflection->getProperty('route');
112 1
        $reflectionProperty->setAccessible(true);
113
114
        /**
115
         * @var Route $value
116
         */
117 1
        $value = $reflectionProperty->getValue($currentRoute);
118 1
        return $value;
119
    }
120
121 1
    private function getMiddlewaresAndAction(?Route $route): array
122
    {
123 1
        if ($route === null) {
124 1
            return [[], null];
125
        }
126
        $reflection = new ReflectionObject($route);
127
128
        $reflectionProperty = $reflection->getProperty('middlewareDefinitions');
129
        $reflectionProperty->setAccessible(true);
130
        /**
131
         * @var array[]|callable[]|string[] $middlewareDefinitions
132
         */
133
        $middlewareDefinitions = $reflectionProperty->getValue($route);
134
        $action = array_pop($middlewareDefinitions);
135
        return [$middlewareDefinitions, $action];
136
    }
137
}
138