Completed
Push — master ( 6d01ad...9b4680 )
by
unknown
10:02 queued 06:58
created

AdminCollector   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 16
c 1
b 1
f 0
lcom 1
cbo 5
dl 0
loc 139
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
B collect() 0 64 4
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
 * 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
        foreach ($collectedData as $k => $dataCollection) {
40
            $data = $dataCollection->getData();
41
42
            if ($data instanceof BaseGroupedMapper || $data instanceof BaseMapper) {
43
                $entity = $data->getAdmin()->getClass();
44
                $admin = $data->getAdmin();
45
46
                $this->addToProfiler($k, 'entity', [
47
                    'display' => DataCollection::DESTINATION_PROFILER, // '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...
48
                    'class' => $entity,
49
                    'file' => $this->getClassLink($entity),
50
                ]);
51
52
                $this->addToProfiler($k, 'admin', [
53
                    'display' => DataCollection::DESTINATION_PROFILER,
54
                    'class' => get_class($admin),
55
                    'file' => $this->getClassLink(get_class($admin)),
56
                ]);
57
58
                $this->addToProfiler($k, 'mapper', [
59
                    'display' => DataCollection::DESTINATION_PROFILER,
60
                    'class' => get_class($data),
61
                    'file' => $this->getClassLink(get_class($data)),
62
                ]);
63
64
                $this->addToProfiler($k, 'form tabs / groups', [
65
                    'display' => DataCollection::DESTINATION_PROFILER,
66
                    'class' => count($admin->getFormTabs()) . ' / ' . count($admin->getFormGroups()),
67
                ]);
68
69
                $this->addToProfiler($k, 'form', [
70
                    'display' => DataCollection::DESTINATION_PROFILER,
71
                    'dump' => [
72
                        'tabs' => $admin->getFormGroups(),
73
                        'groups' => $admin->getFormTabs(),
74
                    ],
75
                ]);
76
77
                $this->addToProfiler($k, 'show tabs / groups', [
78
                    'display' => DataCollection::DESTINATION_PROFILER,
79
                    'class' => count($admin->getShowTabs()) . ' / ' . count($admin->getShowGroups()),
80
                ]);
81
82
                $this->addToProfiler($k, 'show', [
83
                    'display' => DataCollection::DESTINATION_PROFILER,
84
                    'dump' => [
85
                        'tabs' => $admin->getShowGroups(),
86
                        'groups' => $admin->getShowTabs(),
87
                    ],
88
                ]);
89
            } else {
90
                $this->addToProfiler($k, $dataCollection->getName(), $dataCollection);
91
            }
92
        }
93
    }
94
95
    public function getData($name = null)
96
    {
97
        if ($name === null) {
98
            return $this->data;
99
        }
100
101
        if (array_key_exists($name, $this->data)) {
102
            return $this->data[$name];
103
        } else {
104
            return self::TYPE_NOT_MANAGED;
105
        }
106
    }
107
108
    public function getName()
109
    {
110
        return 'blast.admin_collector';
111
    }
112
113
    /**
114
     * @return Collector
115
     */
116
    public function getCollector()
117
    {
118
        return $this->collector;
119
    }
120
121
    /**
122
     * @param Collector collector
123
     *
124
     * @return self
125
     */
126
    public function setCollector(Collector $collector)
127
    {
128
        $this->collector = $collector;
129
130
        return $this;
131
    }
132
133
    private function getClassLink($class)
134
    {
135
        $reflector = new \ReflectionClass($class);
136
137
        return $reflector->getFileName();
138
    }
139
140
    private function addToProfiler($rootKey, $key, $data)
141
    {
142
        if ($data instanceof DataCollection) {
143
            $this->data[$data->getDestination()][$rootKey][$key] = $data->getData();
144
        } else {
145
            switch ($data['display']) {
146
                case DataCollection::DESTINATION_TOOLBAR:
147
                case DataCollection::DESTINATION_PROFILER:
148
                    $this->data[$data['display']][$rootKey][$key] = $data;
149
                    break;
150
                case DataCollection::DESTINATION_BOTH:
151
                    $this->data[DataCollection::DESTINATION_TOOLBAR][$rootKey][$key] = $data;
152
                    $this->data[DataCollection::DESTINATION_PROFILER][$rootKey][$key] = $data;
153
                    break;
154
                default:
155
                    $this->data[DataCollection::DESTINATION_PROFILER][$rootKey][$key] = $data;
156
            }
157
        }
158
    }
159
}
160