Passed
Push — master ( a4ffc4...615af5 )
by Alexander
03:23
created

RouterCollector   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 20
c 3
b 0
f 0
dl 0
loc 41
ccs 16
cts 18
cp 0.8889
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getIndexData() 0 7 2
A collect() 0 6 2
A getCollected() 0 11 3
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector;
6
7
use JetBrains\PhpStorm\ArrayShape;
8
use Psr\Container\ContainerInterface;
9
use Yiisoft\Router\CurrentRoute;
10
use Yiisoft\Router\RouteCollectionInterface;
11
12
final class RouterCollector implements RouterCollectorInterface, IndexCollectorInterface
13
{
14
    use CollectorTrait;
15
16
    private ContainerInterface $container;
17
    private float $matchTime = 0;
18
19 1
    public function __construct(ContainerInterface $container)
20
    {
21 1
        $this->container = $container;
22
    }
23
24 1
    public function collect(float $matchTime): void
25
    {
26 1
        if (!$this->isActive()) {
27
            return;
28
        }
29 1
        $this->matchTime = $matchTime;
30
    }
31
32 1
    public function getCollected(): array
33
    {
34 1
        $routeCollection = $this->container->has(RouteCollectionInterface::class)
35 1
            ? $this->container->get(RouteCollectionInterface::class)
36
            : null;
37
38 1
        return $routeCollection === null ? [] :
39
            [
40 1
                'routesTree' => $routeCollection->getRouteTree(),
41 1
                'routes' => $routeCollection->getRoutes(),
42 1
                'routeTime' => $this->matchTime,
43
            ];
44
    }
45
46 1
    #[ArrayShape(['routeMatchTime' => 'float|int', 'matchedRoute' => 'string'])]
47
    public function getIndexData(): array
48
    {
49 1
        $currentRoute = $this->container->has(CurrentRoute::class) ? $this->container->get(CurrentRoute::class) : null;
50
        return [
51 1
            'routeMatchTime' => $this->matchTime,
52 1
            'matchedRoute' => $currentRoute?->getName(),
53
        ];
54
    }
55
}
56