Completed
Pull Request — master (#47)
by Robbie
01:29
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\Proxy\TemplateParserProxy;
9
use SilverStripe\Admin\LeftAndMain;
10
use SilverStripe\Control\Controller;
11
use SilverStripe\Control\Cookie;
12
use SilverStripe\Control\Session;
13
use SilverStripe\i18n\i18n;
14
use SilverStripe\Security\Member;
15
use SilverStripe\SiteConfig\SiteConfig;
16
use SilverStripe\View\Requirements;
17
18
class SilverStripeCollector extends DataCollector implements Renderable, AssetProvider
19
{
20
21
    protected static $debug = [];
22
    protected static $controller;
23
24
    public function collect()
25
    {
26
        $data = array(
27
            'debugcount' => count(self::$debug),
28
            'debug' => self::$debug,
29
            'session' => self::getSessionData(),
30
            'config' => self::getConfigData(),
31
            'locale' => i18n::get_locale(),
32
            'version' => LeftAndMain::create()->CMSVersion(),
33
            'cookies' => self::getCookieData(),
34
            'parameters' => self::getRequestParameters(),
35
            'requirements' => self::getRequirementsData(),
36
            '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...
37
            'templates' => self::getTemplateData(),
38
        );
39
        return $data;
40
    }
41
42
    /**
43
     * Returns the names of all the templates rendered.
44
     *
45
     * @return array
46
     */
47
    public static function getTemplateData()
48
    {
49
        if (TemplateParserProxy::getCached()) {
50
            return array(
51
                'templates' => array(
52
                    'NOTE: Rendered templates will not display when cached, please flush to view the list.'
53
                ),
54
                'count' => '-'
55
            );
56
        }
57
58
        $templates = TemplateParserProxy::getTemplatesUsed();
59
        return array(
60
            'templates' => $templates,
61
            'count' => count($templates)
62
        );
63
    }
64
65
    public static function getRequirementsData()
66
    {
67
        ob_start();
68
        Requirements::debug();
69
        $requirements = ob_get_clean();
70
71
        $matches = null;
72
73
        preg_match_all("/<li>(.*?)<\/li>/s", $requirements, $matches);
74
75
        if (!empty($matches[1])) {
76
            return $matches[1];
77
        }
78
        return [];
79
    }
80
81
    public static function getRequestParameters()
82
    {
83
        if (!self::$controller) {
84
            return [];
85
        }
86
        $request = self::$controller->getRequest();
87
88
        $p = [];
89
        foreach ($request->getVars() as $k => $v) {
90
            $p["GET - $k"] = $v;
91
        }
92
        foreach ($request->postVars() as $k => $v) {
93
            $p["POST - $k"] = $v;
94
        }
95
        foreach ($request->params() as $k => $v) {
96
            $p["ROUTE - $k"] = $v;
97
        }
98
        return $p;
99
    }
100
101
    public static function getCookieData()
102
    {
103
        return Cookie::get_all();
104
    }
105
106
    public static function getSessionData()
107
    {
108
        $data = Controller::curr()->getRequest()->getSession()->getAll();
109
        $filtered = [];
110
111
        // Filter not useful data
112
        foreach ($data as $k => $v) {
113
            if (strpos($k, 'gf_') === 0) {
114
                continue;
115
            }
116
            if ($k === 'PHPDEBUGBAR_STACK_DATA') {
117
                continue;
118
            }
119
            $filtered[$k] = $v;
120
        }
121
        return $filtered;
122
    }
123
124
    public static function getConfigData()
125
    {
126
        return SiteConfig::current_site_config()->toMap();
127
    }
128
129
    /**
130
     * This method will try to matches all messages into a proper array
131
     *
132
     * @param string $data
133
     */
134
    public static function setDebugData($data)
135
    {
136
        $matches = null;
137
138
        preg_match_all(
139
            "/<p class=\"message warning\">\n(.*?)<\/p>/s", $data, $matches
140
        );
141
142
        if (!empty($matches[1])) {
143
            self::$debug = $matches[1];
144
        }
145
    }
146
147
    /**
148
     * @param Controller $controller
149
     * @return $this
150
     */
151
    public function setController($controller)
152
    {
153
        self::$controller = $controller;
154
        return $this;
155
    }
156
157
    /**
158
     * @return Controller
159
     */
160
    public function getController()
161
    {
162
        return self::$controller;
163
    }
164
165
    public function getName()
166
    {
167
        return 'silverstripe';
168
    }
169
170
    public function getWidgets()
171
    {
172
        $name = $this->getName();
173
174
        $userIcon = 'user-times';
175
        $userText = 'Not logged in';
176
        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...
177
            $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...
178
            $memberTag = $member->getTitle() . ' (#' . $member->ID . ')';
179
180
            $userIcon = 'user';
181
            $userText = 'Logged in as ' . $memberTag;
182
183
            // Masquerade integration
184
            if (Controller::curr()->getRequest()->getSession()->get('Masquerade.Old.loggedInAs')) {
185
                $userIcon = 'user-secret';
186
                $userText = 'Masquerading as member ' . $memberTag;
187
            }
188
        }
189
190
        $widgets = array(
191
            'user' => array(
192
                'icon' => $userIcon,
193
                'tooltip' => $userText,
194
                "default" => "",
195
            ),
196
            "version" => array(
197
                "icon" => "desktop",
198
                "tooltip" => LeftAndMain::create()->CMSVersion(),
199
                "default" => ""
200
            ),
201
            "locale" => array(
202
                "icon" => "globe",
203
                "tooltip" => i18n::get_locale(),
204
                "default" => "",
205
            ),
206
            "session" => array(
207
                "icon" => "archive",
208
                "widget" => "PhpDebugBar.Widgets.VariableListWidget",
209
                "map" => "$name.session",
210
                "default" => "{}"
211
            ),
212
            "cookies" => array(
213
                "icon" => "asterisk",
214
                "widget" => "PhpDebugBar.Widgets.VariableListWidget",
215
                "map" => "$name.cookies",
216
                "default" => "{}"
217
            ),
218
            "parameters" => array(
219
                "icon" => "arrow-right",
220
                "widget" => "PhpDebugBar.Widgets.VariableListWidget",
221
                "map" => "$name.parameters",
222
                "default" => "{}"
223
            ),
224
            "config" => array(
225
                "icon" => "gear",
226
                "widget" => "PhpDebugBar.Widgets.VariableListWidget",
227
                "map" => "$name.config",
228
                "default" => "{}"
229
            ),
230
            "requirements" => array(
231
                "icon" => "file-o ",
232
                "widget" => "PhpDebugBar.Widgets.ListWidget",
233
                "map" => "$name.requirements",
234
                "default" => "{}"
235
            ),
236
            'templates' => array(
237
                'icon' => 'edit',
238
                'widget' => 'PhpDebugBar.Widgets.ListWidget',
239
                'map' => "$name.templates.templates",
240
                'default' => '{}'
241
            ),
242
            'templates:badge' => array(
243
                'map' => "$name.templates.count",
244
                'default' => 0
245
            )
246
        );
247
248
        if (!empty(self::$debug)) {
249
            $widgets["debug"] = array(
250
                "icon" => "list-alt",
251
                "widget" => "PhpDebugBar.Widgets.ListWidget",
252
                "map" => "$name.debug",
253
                "default" => "[]"
254
            );
255
            $widgets["debug:badge"] = array(
256
                "map" => "$name.debugcount",
257
                "default" => "null"
258
            );
259
        }
260
261
        return $widgets;
262
    }
263
264
    /**
265
     * @return array
266
     */
267 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...
268
    {
269
        return array(
270
            'base_path' => '/' . DEBUGBAR_DIR . '/javascript',
271
            'base_url' => DEBUGBAR_DIR . '/javascript',
272
            'css' => [],
273
            'js' => 'widgets.js',
274
        );
275
    }
276
}
277