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

RouterCollector::collect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

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 3
cts 4
cp 0.75
crap 2.0625
rs 10
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