Completed
Pull Request — master (#154)
by Arnaud
03:49
created

AdminDataCollector::collect()   B

Complexity

Conditions 8
Paths 42

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 26
c 1
b 0
f 0
nc 42
nop 3
dl 0
loc 45
rs 8.4444
ccs 0
cts 34
cp 0
crap 72
1
<?php
2
3
namespace LAG\AdminBundle\Debug\DataCollector;
4
5
use Exception;
6
use LAG\AdminBundle\Configuration\ApplicationConfigurationStorage;
7
use LAG\AdminBundle\Menu\Provider\MenuProvider;
8
use LAG\AdminBundle\Resource\Registry\ResourceRegistryInterface;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
12
13
class AdminDataCollector extends DataCollector
14
{
15
    /**
16
     * @var ResourceRegistryInterface
17
     */
18
    private $registry;
19
20
    /**
21
     * @var ApplicationConfigurationStorage
22
     */
23
    private $applicationConfigurationStorage;
24
25
    /**
26
     * @var MenuProvider
27
     */
28
    private $menuProvider;
29
30
    /**
31
     * AdminDataCollector constructor.
32
     */
33
    public function __construct(
34
        ResourceRegistryInterface $registry,
35
        ApplicationConfigurationStorage $applicationConfigurationStorage,
36
        MenuProvider $menuProvider
37
    ) {
38
        $this->registry = $registry;
39
        $this->applicationConfigurationStorage = $applicationConfigurationStorage;
40
        $this->menuProvider = $menuProvider;
41
    }
42
43
    public function collect(Request $request, Response $response, Exception $exception = null)
44
    {
45
        $data = [
46
            'admins' => [],
47
            'application' => [],
48
            'menus' => [],
49
        ];
50
51
        foreach ($this->registry->all() as $resource) {
52
            $data['admins'][$resource->getName()] = [
53
                'entity_class' => $resource->getEntityClass(),
54
                'configuration' => $resource->getConfiguration(),
55
            ];
56
        }
57
58
        foreach ($this->applicationConfigurationStorage->getConfiguration()->getParameters() as $name => $parameter) {
59
            if (is_array($parameter)) {
60
                $parameter = print_r($parameter, true);
61
            }
62
63
            if (is_bool($parameter)) {
64
                $parameter = $parameter ? 'true' : 'false';
65
            }
66
            $data['application'][$name] = $parameter;
67
        }
68
69
        foreach ($this->menuProvider->all() as $menuName => $menu) {
70
            $data['menus'][$menuName] = [
71
                'attributes' => $menu->getAttributes(),
72
                'displayed' => $menu->isDisplayed(),
73
            ];
74
75
            foreach ($menu->getChildren() as $childName => $child) {
76
                $data['menus'][$menuName]['children'][$childName] = [
77
                    'attributes' => $child->getAttributes(),
78
                    'displayed' => $child->isDisplayed(),
79
                    'uri' => $child->getUri(),
80
                ];
81
            }
82
        }
83
84
        $data['application']['admin'] = $request->attributes->get('_admin');
85
        $data['application']['action'] = $request->attributes->get('_action');
86
87
        $this->data = $data;
88
    }
89
90
    /**
91
     * Returns the name of the collector.
92
     *
93
     * @return string The collector name
94
     */
95
    public function getName()
96
    {
97
        return 'admin.data_collector';
98
    }
99
100
    public function reset()
101
    {
102
        $this->data = [];
103
    }
104
105
    public function getData()
106
    {
107
        return $this->data;
108
    }
109
}
110