Passed
Pull Request — master (#160)
by Dmitriy
04:52 queued 02:01
created

RouterCollector   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 18
dl 0
loc 38
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCollected() 0 11 3
A getIndexData() 0 7 2
A __construct() 0 2 1
A collect() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector\Web;
6
7
use Psr\Container\ContainerInterface;
8
use Yiisoft\Router\CurrentRoute;
9
use Yiisoft\Router\RouteCollectionInterface;
10
use Yiisoft\Yii\Debug\Collector\CollectorInterface;
11
use Yiisoft\Yii\Debug\Collector\CollectorTrait;
12
use Yiisoft\Yii\Debug\Collector\IndexCollectorInterface;
13
14
class RouterCollector implements CollectorInterface, IndexCollectorInterface
15
{
16
    use CollectorTrait;
0 ignored issues
show
Coding Style introduced by
There must be a blank line following the last trait import statement
Loading history...
17
    private float $matchTime = 0;
18
19
    public function __construct(private ContainerInterface $container)
20
    {
21
    }
22
23
    public function collect(float $matchTime): void
24
    {
25
        if (!$this->isActive()) {
26
            return;
27
        }
28
        $this->matchTime = $matchTime;
29
    }
30
31
    public function getCollected(): array
32
    {
33
        $routeCollection = $this->container->has(RouteCollectionInterface::class)
34
            ? $this->container->get(RouteCollectionInterface::class)
35
            : null;
36
37
        return $routeCollection === null ? [] :
38
            [
39
                'routesTree' => $routeCollection->getRouteTree(),
40
                'routes' => $routeCollection->getRoutes(),
41
                'routeTime' => $this->matchTime,
42
            ];
43
    }
44
45
    public function getIndexData(): array
46
    {
47
        $currentRoute = $this->container->has(CurrentRoute::class) ? $this->container->get(CurrentRoute::class) : null;
48
        return [
49
            'router' => [
50
                'matchTime' => $this->matchTime,
51
                'matchedRoute' => $currentRoute?->getName(),
52
            ],
53
        ];
54
    }
55
}
56