Passed
Pull Request — master (#175)
by Arnaud
03:44
created

AdminDataCollector::collect()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 26
nc 3
nop 3
dl 0
loc 45
ccs 21
cts 21
cp 1
crap 6
rs 8.8817
c 1
b 0
f 0
1
<?php
2
3
namespace LAG\AdminBundle\Debug\DataCollector;
4
5
use Exception;
6
use LAG\AdminBundle\Bridge\KnpMenu\Provider\MenuProvider;
7
use LAG\AdminBundle\Configuration\ApplicationConfigurationStorage;
8
use LAG\AdminBundle\Exception\ConfigurationException;
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
    /**
17
     * @var ResourceRegistryInterface
18
     */
19
    private $registry;
20
21
    /**
22
     * @var ApplicationConfigurationStorage
23
     */
24
    private $storage;
25
26
    /**
27
     * @var MenuProvider
28
     */
29
    private $menuProvider;
30
31
    /**
32
     * AdminDataCollector constructor.
33 2
     */
34
    public function __construct(
35
        ResourceRegistryInterface $registry,
36
        ApplicationConfigurationStorage $storage,
37
        MenuProvider $menuProvider
38 2
    ) {
39 2
        $this->registry = $registry;
40 2
        $this->storage = $storage;
41 2
        $this->menuProvider = $menuProvider;
42
    }
43 2
44
    public function collect(Request $request, Response $response, Exception $exception = null)
45
    {
46 2
        $data = [
47
            'admins' => [],
48
            'application' => [],
49
            'menus' => [],
50
        ];
51 2
52 2
        if ($exception instanceof ConfigurationException) {
53 2
            $data['error'] = 'An error has occurred during the configuration resolving. Data can not displayed';
54 2
            $data['configuration'] = $exception->getConfiguration();
55
        } else {
56
            foreach ($this->registry->all() as $resource) {
57
                $data['admins'][$resource->getName()] = [
58 2
                    'entity_class' => $resource->getEntityClass(),
59 2
                    'configuration' => $resource->getConfiguration(),
60
                ];
61
            }
62 2
63 2
            // When the application configuration is not defined or resolved, we can not access to the admin/menus
64 2
            // configuration
65 2
            if ($this->storage->isFrozen()) {
66
                $data['application'] = $this->storage->getConfiguration()->all();
67
68 2
                foreach ($this->menuProvider->all() as $menuName => $menu) {
69 2
                    $data['menus'][$menuName] = [
70 2
                        'attributes' => $menu->getAttributes(),
71 2
                        'displayed' => $menu->isDisplayed(),
72 2
                    ];
73
74
                    foreach ($menu->getChildren() as $childName => $child) {
75
                        $data['menus'][$menuName]['children'][$childName] = [
76
                            'attributes' => $child->getAttributes(),
77 2
                            'displayed' => $child->isDisplayed(),
78 2
                            'uri' => $child->getUri(),
79
                        ];
80 2
                    }
81 2
                }
82
            }
83
        }
84
85
        $data['application']['admin'] = $request->attributes->get('_admin');
86
        $data['application']['action'] = $request->attributes->get('_action');
87
88 2
        $this->data = $data;
89
    }
90 2
91
    /**
92
     * Returns the name of the collector.
93 2
     *
94
     * @return string The collector name
95 2
     */
96 2
    public function getName()
97
    {
98 2
        return 'admin.data_collector';
99
    }
100 2
101
    public function reset()
102
    {
103
        $this->data = [];
104
    }
105
106
    public function getData()
107
    {
108
        return $this->data;
109
    }
110
}
111