Completed
Push — wip-platform ( 03cba6...2999ab )
by
unknown
05:53 queued 02:49
created

AdminCollector   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
B collect() 0 84 6
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
1
<?php
2
3
/*
4
 *
5
 * Copyright (C) 2015-2017 Libre Informatique
6
 *
7
 * This file is licenced under the GNU LGPL v3.
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Blast\Bundle\CoreBundle\Profiler;
13
14
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use Sonata\AdminBundle\Mapper\BaseMapper;
18
use Sonata\AdminBundle\Mapper\BaseGroupedMapper;
19
20
class AdminCollector extends DataCollector
21
{
22
    const TYPE_NOT_MANAGED = 'Not Managed';
23
24
    /**
25
     * @var Collector
26
     */
27
    private $collector;
28
29
    public function collect(Request $request, Response $response, \Exception $exception = null)
30
    {
31
        $this->data = [
32
            DataCollection::DESTINATION_TOOLBAR  => [],
33
            DataCollection::DESTINATION_PROFILER => [],
34
        ];
35
36
        $collectedData = $this->collector->getData();
37
38
        $hooks = 0;
39
40
        foreach ($collectedData as $k => $dataCollection) {
41
            $data = $dataCollection->getData();
42
43
            if (preg_replace('/\#[0-9]*\W/', '', $k) === 'Managed classes') {
44
                $this->addToProfiler($k, 'Managed classes', [
45
                    '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...
46
                    'class'           => count($data),
47
                ]);
48
            }
49
50
            if (preg_replace('/^\#[0-9]*\W/', '', $k) === 'Hook') {
51
                ++$hooks;
52
            }
53
54
            if ($data instanceof BaseGroupedMapper || $data instanceof BaseMapper) {
55
                $entity = $data->getAdmin()->getClass();
56
                $admin = $data->getAdmin();
57
58
                $this->addToProfiler($k, 'entity', [
59
                    'display' => DataCollection::DESTINATION_PROFILER,
60
                    'class'   => $entity,
61
                    'file'    => $this->getClassLink($entity),
62
                ]);
63
64
                $this->addToProfiler($k, 'admin', [
65
                    'display' => DataCollection::DESTINATION_PROFILER,
66
                    'class'   => get_class($admin),
67
                    'file'    => $this->getClassLink(get_class($admin)),
68
                ]);
69
70
                // Not really usefull because other type of mapper have not been tested
71
                //
72
                // $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...
73
                //     '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...
74
                //     '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...
75
                //     '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...
76
                // ]);
77
78
                $this->addToProfiler($k, 'form tabs / groups', [
79
                    'display' => DataCollection::DESTINATION_PROFILER,
80
                    'class'   => count($admin->getFormTabs()) . ' / ' . count($admin->getFormGroups()),
81
                ]);
82
83
                $this->addToProfiler($k, 'form', [
84
                    'display'         => DataCollection::DESTINATION_PROFILER,
85
                    'Tabs and Groups' => [
86
                        'tabs'   => $admin->getFormTabs(),
87
                        'groups' => $admin->getFormGroups(),
88
                    ],
89
                ]);
90
91
                $this->addToProfiler($k, 'show tabs / groups', [
92
                    'display' => DataCollection::DESTINATION_PROFILER,
93
                    'class'   => count($admin->getShowTabs()) . ' / ' . count($admin->getShowGroups()),
94
                ]);
95
96
                $this->addToProfiler($k, 'show', [
97
                    'display'         => DataCollection::DESTINATION_PROFILER,
98
                    'Tabs and Groups' => [
99
                        'tabs'   => $admin->getShowTabs(),
100
                        'groups' => $admin->getShowGroups(),
101
                    ],
102
                ]);
103
            } else {
104
                $this->addToProfiler($k, $dataCollection->getName(), $dataCollection);
105
            }
106
        }
107
108
        $this->addToProfiler('Hook registered', 'Hooks', [
109
            '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...
110
            'class'           => $hooks,
111
        ]);
112
    }
113
114
    public function getData($name = null)
115
    {
116
        if ($name === null) {
117
            return $this->data;
118
        }
119
120
        if (array_key_exists($name, $this->data)) {
121
            return $this->data[$name];
122
        } else {
123
            return self::TYPE_NOT_MANAGED;
124
        }
125
    }
126
127
    public function getName()
128
    {
129
        return 'blast.admin_collector';
130
    }
131
132
    /**
133
     * @return Collector
134
     */
135
    public function getCollector()
136
    {
137
        return $this->collector;
138
    }
139
140
    /**
141
     * @param Collector collector
142
     *
143
     * @return self
144
     */
145
    public function setCollector(Collector $collector)
146
    {
147
        $this->collector = $collector;
148
149
        return $this;
150
    }
151
152
    private function getClassLink($class)
153
    {
154
        $reflector = new \ReflectionClass($class);
155
156
        return $reflector->getFileName();
157
    }
158
159
    private function addToProfiler($rootKey, $key, $data)
160
    {
161
        if ($data instanceof DataCollection) {
162
            $this->data[$data->getDestination()][$rootKey][$key] = $data->getData();
163
        } else {
164
            switch ($data['display']) {
165
                case DataCollection::DESTINATION_TOOLBAR:
166
                case DataCollection::DESTINATION_PROFILER:
167
                    $this->data[$data['display']][$rootKey][$key] = $data;
168
                    break;
169
                case DataCollection::DESTINATION_BOTH:
170
                    $this->data[DataCollection::DESTINATION_TOOLBAR][$rootKey][$key] = $data;
171
                    $this->data[DataCollection::DESTINATION_PROFILER][$rootKey][$key] = $data;
172
                    break;
173
                default:
174
                    $this->data[DataCollection::DESTINATION_PROFILER][$rootKey][$key] = $data;
175
            }
176
        }
177
    }
178
}
179