AdminDataCollector::reset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
ccs 0
cts 0
cp 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Debug\DataCollector;
6
7
use LAG\AdminBundle\Application\Configuration\ApplicationConfiguration;
8
use LAG\AdminBundle\Request\Extractor\ParametersExtractorInterface;
9
use LAG\AdminBundle\Resource\Registry\ResourceRegistryInterface;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
13
14
class AdminDataCollector extends DataCollector
15
{
16
    public function __construct(
17
        private ResourceRegistryInterface $registry,
18
        private ApplicationConfiguration $applicationConfiguration,
19
        private ParametersExtractorInterface $parametersExtractor,
20 1
    ) {
21
    }
22
23
    public function collect(Request $request, Response $response, \Throwable $exception = null): void
24
    {
25 1
        $data = [
26 1
            'resources' => [],
27 1
            'application' => [],
28 1
        ];
29
30 1
        foreach ($this->registry->all() as $resource) {
31
            $reflection = new \ReflectionClass($resource);
32
33 1
            foreach ($reflection->getProperties() as $reflectionProperty) {
34
                if ($reflectionProperty->isInitialized($resource)) {
35
                    $data['resources'][$resource->getName()][$reflectionProperty->getName()] = $reflectionProperty->getValue($resource);
36
                }
37
            }
38 1
        }
39 1
40 1
        // When the application configuration is not defined or resolved, we can not access to the admin/menus
41 1
        // configuration
42
        if ($this->applicationConfiguration->isFrozen()) {
43
            $data['application'] = $this->applicationConfiguration->toArray();
44
        }
45
46
        if ($this->parametersExtractor->supports($request)) {
47 1
            $data['application']['resource'] = $this->parametersExtractor->getResourceName($request);
48 1
            $data['application']['operation'] = $this->parametersExtractor->getOperationName($request);
49 1
        }
50
51 1
        $this->data = $data;
52 1
    }
53
54 1
    public function getName(): string
55 1
    {
56
        return self::class;
57
    }
58
59
    public function reset()
60
    {
61
        $this->data = [];
62 1
    }
63
64 1
    public function getData(): array
65
    {
66
        return $this->data;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data could return the type Symfony\Component\VarDumper\Cloner\Data which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
67 1
    }
68
}
69