Completed
Pull Request — master (#47)
by Robbie
01:27
created

SilverStripeCollector::getWidgets()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 93
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 93
rs 8.3103
c 0
b 0
f 0
cc 4
eloc 68
nc 6
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace LeKoala\DebugBar\Collector;
4
5
use DebugBar\DataCollector\AssetProvider;
6
use DebugBar\DataCollector\DataCollector;
7
use DebugBar\DataCollector\Renderable;
8
use LeKoala\DebugBar\DebugBar;
9
use LeKoala\DebugBar\Proxy\TemplateParserProxy;
10
use SilverStripe\Admin\LeftAndMain;
11
use SilverStripe\Control\Controller;
12
use SilverStripe\Control\Cookie;
13
use SilverStripe\Control\Session;
14
use SilverStripe\Core\Convert;
15
use SilverStripe\i18n\i18n;
16
use SilverStripe\Security\Member;
17
use SilverStripe\SiteConfig\SiteConfig;
18
use SilverStripe\View\Requirements;
19
20
class SilverStripeCollector extends DataCollector implements Renderable, AssetProvider
21
{
22
    protected static $debug = [];
23
    protected static $controller;
24
25
    public function collect()
26
    {
27
        $data = array(
28
            'debugcount' => count(self::$debug),
29
            'debug' => self::$debug,
30
            'session' => self::getSessionData(),
31
            'config' => self::getConfigData(),
32
            'locale' => i18n::get_locale(),
33
            'version' => LeftAndMain::create()->CMSVersion(),
34
            'cookies' => self::getCookieData(),
35
            'parameters' => self::getRequestParameters(),
36
            'requirements' => self::getRequirementsData(),
37
            'user' => Member::currentUserID() ? Member::currentUser()->Title : 'Not logged in',
0 ignored issues
show
Deprecated Code introduced by
The method SilverStripe\Security\Member::currentUserID() has been deprecated with message: 5.0.0 use Security::getCurrentUser()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Deprecated Code introduced by
The method SilverStripe\Security\Member::currentUser() has been deprecated with message: 5.0.0 use Security::getCurrentUser()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
38
            'templates' => self::getTemplateData(),
39
        );
40
        return $data;
41
    }
42
43
    /**
44
     * Returns the names of all the templates rendered.
45
     *
46
     * @return array
47
     */
48
    public static function getTemplateData()
49
    {
50
        if (TemplateParserProxy::getCached()) {
51
            return array(
52
                'templates' => array(
53
                    'NOTE: Rendered templates will not display when cached, please flush to view the list.'
54
                ),
55
                'count' => '-'
56
            );
57
        }
58
59
        $templates = TemplateParserProxy::getTemplatesUsed();
60
        return array(
61
            'templates' => $templates,
62
            'count' => count($templates)
63
        );
64
    }
65
66
    public static function getRequirementsData()
67
    {
68
        $backend = Requirements::backend();
69
70
        $requirements = array_merge(
71
            $backend->getCSS(),
72
            $backend->getJavascript()
73
        );
74
75
        $output = [];
76
        foreach ($requirements as $asset => $specs) {
77
            $output[] = $asset . ': ' . Convert::raw2json($specs);
78
        }
79
        return $output;
80
    }
81
82
    public static function getRequestParameters()
83
    {
84
        if (!self::$controller) {
85
            return [];
86
        }
87
        $request = self::$controller->getRequest();
88
89
        $p = [];
90
        foreach ($request->getVars() as $k => $v) {
91
            $p["GET - $k"] = $v;
92
        }
93
        foreach ($request->postVars() as $k => $v) {
94
            $p["POST - $k"] = $v;
95
        }
96
        foreach ($request->params() as $k => $v) {
97
            $p["ROUTE - $k"] = $v;
98
        }
99
        return $p;
100
    }
101
102
    public static function getCookieData()
103
    {
104
        return Cookie::get_all();
105
    }
106
107
    public static function getSessionData()
108
    {
109
        $data = DebugBar::getRequest()->getSession()->getAll();
110
        $filtered = [];
111
112
        // Filter not useful data
113
        foreach ($data as $k => $v) {
114
            if (strpos($k, 'gf_') === 0) {
115
                continue;
116
            }
117
            if ($k === 'PHPDEBUGBAR_STACK_DATA') {
118
                continue;
119
            }
120
            $filtered[$k] = $v;
121
        }
122
        return $filtered;
123
    }
124
125
    public static function getConfigData()
126
    {
127
        return SiteConfig::current_site_config()->toMap();
128
    }
129
130
    /**
131
     * This method will try to matches all messages into a proper array
132
     *
133
     * @param string $data
134
     */
135
    public static function setDebugData($data)
136
    {
137
        $matches = null;
138
139
        preg_match_all("/<p class=\"message warning\">\n(.*?)<\/p>/s", $data, $matches);
140
141
        if (!empty($matches[1])) {
142
            self::$debug = $matches[1];
143
        }
144
    }
145
146
    /**
147
     * @param Controller $controller
148
     * @return $this
149
     */
150
    public function setController($controller)
151
    {
152
        self::$controller = $controller;
153
        return $this;
154
    }
155
156
    /**
157
     * @return Controller
158
     */
159
    public function getController()
160
    {
161
        return self::$controller;
162
    }
163
164
    public function getName()
165
    {
166
        return 'silverstripe';
167
    }
168
169
    public function getWidgets()
170
    {
171
        $name = $this->getName();
172
173
        $userIcon = 'user-times';
174
        $userText = 'Not logged in';
175
        if (Member::currentUserID()) {
0 ignored issues
show
Deprecated Code introduced by
The method SilverStripe\Security\Member::currentUserID() has been deprecated with message: 5.0.0 use Security::getCurrentUser()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
176
            $member = Member::currentUser();
0 ignored issues
show
Deprecated Code introduced by
The method SilverStripe\Security\Member::currentUser() has been deprecated with message: 5.0.0 use Security::getCurrentUser()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
177
            $memberTag = $member->getTitle() . ' (#' . $member->ID . ')';
178
179
            $userIcon = 'user';
180
            $userText = 'Logged in as ' . $memberTag;
181
182
            // Masquerade integration
183
            if (DebugBar::getRequest()->getSession()->get('Masquerade.Old.loggedInAs')) {
184
                $userIcon = 'user-secret';
185
                $userText = 'Masquerading as member ' . $memberTag;
186
            }
187
        }
188
189
        $widgets = array(
190
            'user' => array(
191
                'icon' => $userIcon,
192
                'tooltip' => $userText,
193
                "default" => "",
194
            ),
195
            "version" => array(
196
                "icon" => "desktop",
197
                "tooltip" => LeftAndMain::create()->CMSVersion(),
198
                "default" => ""
199
            ),
200
            "locale" => array(
201
                "icon" => "globe",
202
                "tooltip" => i18n::get_locale(),
203
                "default" => "",
204
            ),
205
            "session" => array(
206
                "icon" => "archive",
207
                "widget" => "PhpDebugBar.Widgets.VariableListWidget",
208
                "map" => "$name.session",
209
                "default" => "{}"
210
            ),
211
            "cookies" => array(
212
                "icon" => "asterisk",
213
                "widget" => "PhpDebugBar.Widgets.VariableListWidget",
214
                "map" => "$name.cookies",
215
                "default" => "{}"
216
            ),
217
            "parameters" => array(
218
                "icon" => "arrow-right",
219
                "widget" => "PhpDebugBar.Widgets.VariableListWidget",
220
                "map" => "$name.parameters",
221
                "default" => "{}"
222
            ),
223
            "config" => array(
224
                "icon" => "gear",
225
                "widget" => "PhpDebugBar.Widgets.VariableListWidget",
226
                "map" => "$name.config",
227
                "default" => "{}"
228
            ),
229
            "requirements" => array(
230
                "icon" => "file-o ",
231
                "widget" => "PhpDebugBar.Widgets.ListWidget",
232
                "map" => "$name.requirements",
233
                "default" => "{}"
234
            ),
235
            'templates' => array(
236
                'icon' => 'edit',
237
                'widget' => 'PhpDebugBar.Widgets.ListWidget',
238
                'map' => "$name.templates.templates",
239
                'default' => '{}'
240
            ),
241
            'templates:badge' => array(
242
                'map' => "$name.templates.count",
243
                'default' => 0
244
            )
245
        );
246
247
        if (!empty(self::$debug)) {
248
            $widgets["debug"] = array(
249
                "icon" => "list-alt",
250
                "widget" => "PhpDebugBar.Widgets.ListWidget",
251
                "map" => "$name.debug",
252
                "default" => "[]"
253
            );
254
            $widgets["debug:badge"] = array(
255
                "map" => "$name.debugcount",
256
                "default" => "null"
257
            );
258
        }
259
260
        return $widgets;
261
    }
262
263
    /**
264
     * @return array
265
     */
266 View Code Duplication
    public function getAssets()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
267
    {
268
        return array(
269
            'base_path' => '/' . DEBUGBAR_DIR . '/javascript',
270
            'base_url' => DEBUGBAR_DIR . '/javascript',
271
            'css' => [],
272
            'js' => 'widgets.js',
273
        );
274
    }
275
}
276