AdminCollector   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 18
c 3
b 1
f 0
lcom 1
cbo 5
dl 0
loc 159
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getData() 0 12 3
A getName() 0 4 1
A getCollector() 0 4 1
A setCollector() 0 6 1
A getClassLink() 0 6 1
B addToProfiler() 0 19 5
B collect() 0 84 6
1
<?php
2
3
/*
4
 * This file is part of the Blast Project package.
5
 *
6
 * Copyright (C) 2015-2017 Libre Informatique
7
 *
8
 * This file is licenced under the GNU LGPL v3.
9
 * For the full copyright and license information, please view the LICENSE.md
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Blast\CoreBundle\Profiler;
14
15
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use Sonata\AdminBundle\Mapper\BaseMapper;
19
use Sonata\AdminBundle\Mapper\BaseGroupedMapper;
20
21
class AdminCollector extends DataCollector
22
{
23
    const TYPE_NOT_MANAGED = 'Not Managed';
24
25
    /**
26
     * @var Collector
27
     */
28
    private $collector;
29
30
    public function collect(Request $request, Response $response, \Exception $exception = null)
31
    {
32
        $this->data = [
33
            DataCollection::DESTINATION_TOOLBAR  => [],
34
            DataCollection::DESTINATION_PROFILER => [],
35
        ];
36
37
        $collectedData = $this->collector->getData();
38
39
        $hooks = 0;
40
41
        foreach ($collectedData as $k => $dataCollection) {
42
            $data = $dataCollection->getData();
43
44
            if (preg_replace('/\#[0-9]*\W/', '', $k) === 'Managed classes') {
45
                $this->addToProfiler($k, 'Managed classes', [
46
                    'display'         => DataCollection::DESTINATION_TOOLBAR, // 'toolbar', 'profiler', 'both'
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
47
                    'class'           => count($data),
48
                ]);
49
            }
50
51
            if (preg_replace('/^\#[0-9]*\W/', '', $k) === 'Hook') {
52
                $hooks++;
53
            }
54
55
            if ($data instanceof BaseGroupedMapper || $data instanceof BaseMapper) {
56
                $entity = $data->getAdmin()->getClass();
57
                $admin = $data->getAdmin();
58
59
                $this->addToProfiler($k, 'entity', [
60
                    'display' => DataCollection::DESTINATION_PROFILER,
61
                    'class'   => $entity,
62
                    'file'    => $this->getClassLink($entity),
63
                ]);
64
65
                $this->addToProfiler($k, 'admin', [
66
                    'display' => DataCollection::DESTINATION_PROFILER,
67
                    'class'   => get_class($admin),
68
                    'file'    => $this->getClassLink(get_class($admin)),
69
                ]);
70
71
                // Not really usefull because other type of mapper have not been tested
72
                //
73
                // $this->addToProfiler($k, 'mapper', [
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
74
                //     'display' => DataCollection::DESTINATION_PROFILER,
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
75
                //     'class'   => get_class($data),
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
76
                //     'file'    => $this->getClassLink(get_class($data)),
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
77
                // ]);
78
79
                $this->addToProfiler($k, 'form tabs / groups', [
80
                    'display' => DataCollection::DESTINATION_PROFILER,
81
                    'class'   => count($admin->getFormTabs()) . ' / ' . count($admin->getFormGroups()),
82
                ]);
83
84
                $this->addToProfiler($k, 'form', [
85
                    'display'         => DataCollection::DESTINATION_PROFILER,
86
                    'Tabs and Groups' => [
87
                        'tabs'   => $admin->getFormTabs(),
88
                        'groups' => $admin->getFormGroups(),
89
                    ],
90
                ]);
91
92
                $this->addToProfiler($k, 'show tabs / groups', [
93
                    'display' => DataCollection::DESTINATION_PROFILER,
94
                    'class'   => count($admin->getShowTabs()) . ' / ' . count($admin->getShowGroups()),
95
                ]);
96
97
                $this->addToProfiler($k, 'show', [
98
                    'display'         => DataCollection::DESTINATION_PROFILER,
99
                    'Tabs and Groups' => [
100
                        'tabs'   => $admin->getShowTabs(),
101
                        'groups' => $admin->getShowGroups(),
102
                    ],
103
                ]);
104
            } else {
105
                $this->addToProfiler($k, $dataCollection->getName(), $dataCollection);
106
            }
107
        }
108
109
        $this->addToProfiler('Hook registered', 'Hooks', [
110
            'display'         => DataCollection::DESTINATION_TOOLBAR, // 'toolbar', 'profiler', 'both'
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
111
            'class'           => $hooks,
112
        ]);
113
    }
114
115
    public function getData($name = null)
116
    {
117
        if ($name === null) {
118
            return $this->data;
119
        }
120
121
        if (array_key_exists($name, $this->data)) {
122
            return $this->data[$name];
123
        } else {
124
            return self::TYPE_NOT_MANAGED;
125
        }
126
    }
127
128
    public function getName()
129
    {
130
        return 'blast.admin_collector';
131
    }
132
133
    /**
134
     * @return Collector
135
     */
136
    public function getCollector()
137
    {
138
        return $this->collector;
139
    }
140
141
    /**
142
     * @param Collector collector
143
     *
144
     * @return self
145
     */
146
    public function setCollector(Collector $collector)
147
    {
148
        $this->collector = $collector;
149
150
        return $this;
151
    }
152
153
    private function getClassLink($class)
154
    {
155
        $reflector = new \ReflectionClass($class);
156
157
        return $reflector->getFileName();
158
    }
159
160
    private function addToProfiler($rootKey, $key, $data)
161
    {
162
        if ($data instanceof DataCollection) {
163
            $this->data[$data->getDestination()][$rootKey][$key] = $data->getData();
164
        } else {
165
            switch ($data['display']) {
166
                case DataCollection::DESTINATION_TOOLBAR:
167
                case DataCollection::DESTINATION_PROFILER:
168
                    $this->data[$data['display']][$rootKey][$key] = $data;
169
                    break;
170
                case DataCollection::DESTINATION_BOTH:
171
                    $this->data[DataCollection::DESTINATION_TOOLBAR][$rootKey][$key] = $data;
172
                    $this->data[DataCollection::DESTINATION_PROFILER][$rootKey][$key] = $data;
173
                    break;
174
                default:
175
                    $this->data[DataCollection::DESTINATION_PROFILER][$rootKey][$key] = $data;
176
            }
177
        }
178
    }
179
}
180