Passed
Pull Request — master (#224)
by Sergei
03:05
created

RouterCollector::collect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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