ControllerExtension   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 52
c 4
b 1
f 0
dl 0
loc 113
rs 10
wmc 23

5 Methods

Rating   Name   Duplication   Size   Complexity  
A onBeforeInit() 0 17 3
A clearBuffer() 0 9 3
B onAfterInit() 0 24 7
B beforeCallActionHandler() 0 23 7
A afterCallActionHandler() 0 16 3
1
<?php
2
3
namespace LeKoala\DebugBar\Extension;
4
5
use LeKoala\DebugBar\DebugBar;
6
use SilverStripe\Core\Extension;
7
use SilverStripe\Control\Controller;
8
use LeKoala\DebugBar\Collector\HeaderCollector;
9
use SilverStripe\CMS\Controllers\ContentController;
0 ignored issues
show
Bug introduced by
The type SilverStripe\CMS\Controllers\ContentController was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use LeKoala\DebugBar\Collector\SilverStripeCollector;
11
12
/**
13
 * A controller extension to log times and render the Debug Bar
14
 */
15
class ControllerExtension extends Extension
16
{
17
    public function onBeforeInit()
18
    {
19
        DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) {
20
            // We must set the current controller when it's available and before it's pushed out of stack
21
            /** @var \LeKoala\DebugBar\Collector\SilverStripeCollector $ssCollector */
22
            $ssCollector =  $debugbar->getCollector('silverstripe');
23
            $ssCollector->setController(Controller::curr());
24
25
            /** @var DebugBar\DataCollector\TimeDataCollector $timeData */
26
            $timeData = $debugbar->getCollector('time');
27
            if (!$timeData) {
0 ignored issues
show
introduced by
$timeData is of type LeKoala\DebugBar\DebugBa...ector\TimeDataCollector, thus it always evaluated to true.
Loading history...
28
                return;
29
            }
30
            if ($timeData->hasStartedMeasure('pre_request')) {
31
                $timeData->stopMeasure("pre_request");
32
            }
33
            $timeData->startMeasure("init", get_class($this->owner) . ' init');
34
        });
35
    }
36
37
    public function onAfterInit()
38
    {
39
        // Avoid requirements being called to early due to RootURLController
40
        if ($this->owner instanceof ContentController) {
41
            DebugBar::includeRequirements();
42
        }
43
44
        DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) {
45
            // Add the headers Collector
46
            if (!$debugbar->hasCollector('Headers') && DebugBar::config()->get('header_collector')) {
47
                $debugbar->addCollector(new HeaderCollector($this->owner));
48
            }
49
            /** @var DebugBar\DataCollector\TimeDataCollector $timeData */
50
            $timeData = $debugbar->getCollector('time');
51
            if (!$timeData) {
0 ignored issues
show
introduced by
$timeData is of type LeKoala\DebugBar\DebugBa...ector\TimeDataCollector, thus it always evaluated to true.
Loading history...
52
                return;
53
            }
54
            if ($timeData->hasStartedMeasure("cms_init")) {
55
                $timeData->stopMeasure("cms_init");
56
            }
57
            if ($timeData->hasStartedMeasure("init")) {
58
                $timeData->stopMeasure("init");
59
            }
60
            $timeData->startMeasure("handle", get_class($this->owner) . ' handle request');
61
        });
62
    }
63
64
    /**
65
     * @param HTTPRequest $request
0 ignored issues
show
Bug introduced by
The type LeKoala\DebugBar\Extension\HTTPRequest was not found. Did you mean HTTPRequest? If so, make sure to prefix the type with \.
Loading history...
66
     * @param string $action
67
     */
68
    public function beforeCallActionHandler($request, $action)
69
    {
70
        // If we don't have an action, getViewer will be called immediatly
71
        // If we have custom routes, request action is different than action
72
        $allParams     = $request->allParams();
73
        $requestAction = null;
74
        if (!empty($allParams['Action'])) {
75
            $requestAction = $allParams['Action'];
76
        }
77
        if (!$this->owner->hasMethod($action) || ($requestAction && $requestAction != $action)) {
78
            self::clearBuffer();
79
        }
80
81
        DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugBar) use ($action) {
82
            /** @var DebugBar\DataCollector\TimeDataCollector $timeData */
83
            $timeData = $debugBar->getCollector('time');
84
            if (!$timeData) {
0 ignored issues
show
introduced by
$timeData is of type LeKoala\DebugBar\DebugBa...ector\TimeDataCollector, thus it always evaluated to true.
Loading history...
85
                return;
86
            }
87
            if ($timeData->hasStartedMeasure("handle")) {
88
                $timeData->stopMeasure("handle");
89
            }
90
            $timeData->startMeasure("action", get_class($this->owner) . " action $action");
91
        });
92
    }
93
94
    /**
95
     * @param HTTPRequest $request
96
     * @param string $action
97
     * @param mixed $result
98
     */
99
    public function afterCallActionHandler($request, $action, $result)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

99
    public function afterCallActionHandler(/** @scrutinizer ignore-unused */ $request, $action, $result)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $result is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

99
    public function afterCallActionHandler($request, $action, /** @scrutinizer ignore-unused */ $result)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
    {
101
        self::clearBuffer();
102
103
        DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugBar) use ($action) {
104
            /** @var DebugBar\DataCollector\TimeDataCollector $timeData */
105
            $timeData = $debugBar->getCollector('time');
106
            if (!$timeData) {
0 ignored issues
show
introduced by
$timeData is of type LeKoala\DebugBar\DebugBa...ector\TimeDataCollector, thus it always evaluated to true.
Loading history...
107
                return;
108
            }
109
            if ($timeData->hasStartedMeasure("action")) {
110
                $timeData->stopMeasure("action");
111
            }
112
            $timeData->startMeasure(
113
                "after_action",
114
                get_class($this->owner) . " after action $action"
115
            );
116
        });
117
    }
118
119
    protected static function clearBuffer()
120
    {
121
        if (!DebugBar::$bufferingEnabled) {
122
            return;
123
        }
124
        $buffer = ob_get_clean();
125
        if (!empty($buffer)) {
126
            unset($_REQUEST['debug_request']); // Disable further messages that we can't intercept
127
            SilverStripeCollector::setDebugData($buffer);
128
        }
129
    }
130
}
131