Passed
Pull Request — master (#190)
by Dmitriy
15:49 queued 04:09
created

RouterCollector   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 17
eloc 60
c 6
b 1
f 0
dl 0
loc 107
ccs 16
cts 18
cp 0.8889
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A collect() 0 6 2
A getIndexData() 0 23 3
A getMiddlewaresAndAction() 0 11 2
A getRouteByCurrentRoute() 0 11 2
A getCollected() 0 31 5
A getCurrentRoute() 0 3 2
A __construct() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector;
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
13
class RouterCollector implements CollectorInterface, IndexCollectorInterface
14
{
15
    use CollectorTrait;
16
17
    private float $matchTime = 0;
18 1
19
    public function __construct(private ContainerInterface $container)
20 1
    {
21
    }
22
23 1
    public function collect(float $matchTime): void
24
    {
25 1
        if (!$this->isActive()) {
26
            return;
27
        }
28 1
        $this->matchTime = $matchTime;
29
    }
30
31 1
    public function getCollected(): array
32
    {
33 1
        $routeCollection = $this->container->has(RouteCollectionInterface::class)
34 1
            ? $this->container->get(RouteCollectionInterface::class)
35
            : null;
36
37 1
        $currentRoute = $this->getCurrentRoute();
38
        $route = $this->getRouteByCurrentRoute($currentRoute);
39 1
        [$middlewares, $action] = $this->getMiddlewaresAndAction($route);
40 1
41 1
        $result = [
42
            'currentRoute' => null,
43
        ];
44
        if ($currentRoute !== null && $route !== null) {
45 1
            $result['currentRoute'] = [
46
                'matchTime' => $this->matchTime,
47 1
                'name' => $route->getData('name'),
48
                'pattern' => $route->getData('pattern'),
49
                'arguments' => $currentRoute->getArguments(),
50 1
                'host' => $route->getData('host'),
51 1
                'uri' => (string) $currentRoute->getUri(),
52
                'action' => $action,
53
                'middlewares' => $middlewares,
54
            ];
55
        }
56
        if ($routeCollection !== null) {
57
            $result['routesTree'] = $routeCollection->getRouteTree();
58
            $result['routes'] = $routeCollection->getRoutes();
59
            $result['routeTime'] = $this->matchTime;
60
        }
61
        return $result;
62
    }
63
64
    public function getIndexData(): array
65
    {
66
        $currentRoute = $this->getCurrentRoute();
67
        $route = $this->getRouteByCurrentRoute($currentRoute);
68
69
        if ($currentRoute === null || $route === null) {
70
            return [
71
                'router' => null,
72
            ];
73
        }
74
75
        [$middlewares, $action] = $this->getMiddlewaresAndAction($route);
76
77
        return [
78
            'router' => [
79
                'matchTime' => $this->matchTime,
80
                'name' => $route->getData('name'),
81
                'pattern' => $route->getData('pattern'),
82
                'arguments' => $currentRoute->getArguments(),
83
                'host' => $route->getData('host'),
84
                'uri' => (string) $currentRoute->getUri(),
85
                'action' => $action,
86
                'middlewares' => $middlewares,
87
            ],
88
        ];
89
    }
90
91
    private function getCurrentRoute(): ?CurrentRoute
92
    {
93
        return $this->container->has(CurrentRoute::class) ? $this->container->get(CurrentRoute::class) : null;
94
    }
95
96
    private function getRouteByCurrentRoute(?CurrentRoute $currentRoute): ?Route
97
    {
98
        if ($currentRoute === null) {
99
            return null;
100
        }
101
        $reflection = new ReflectionObject($currentRoute);
102
103
        $reflectionProperty = $reflection->getProperty('route');
104
        $reflectionProperty->setAccessible(true);
105
106
        return $reflectionProperty->getValue($currentRoute);
107
    }
108
109
    private function getMiddlewaresAndAction(?Route $route): array
110
    {
111
        if ($route === null) {
112
            return [[], null];
113
        }
114
        $reflection = new ReflectionObject($route);
115
        $reflectionProperty = $reflection->getProperty('middlewareDefinitions');
116
        $reflectionProperty->setAccessible(true);
117
        $middlewareDefinitions = $reflectionProperty->getValue($route);
118
        $action = array_pop($middlewareDefinitions);
119
        return [$middlewareDefinitions, $action];
120
    }
121
}
122