ControllerExtension   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 6
Bugs 2 Features 0
Metric Value
eloc 55
c 6
b 2
f 0
dl 0
loc 117
rs 10
wmc 24

5 Methods

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

102
    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...
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

102
    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...
103
    {
104
        self::clearBuffer();
105
106
        DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugBar) use ($action) {
107
            /** @var DebugBar\DataCollector\TimeDataCollector $timeData */
108
            $timeData = $debugBar->getCollector('time');
109
            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...
110
                return;
111
            }
112
            if ($timeData->hasStartedMeasure("action")) {
113
                $timeData->stopMeasure("action");
114
            }
115
            $timeData->startMeasure(
116
                "after_action",
117
                get_class($this->owner) . " after action $action"
118
            );
119
        });
120
    }
121
122
    protected static function clearBuffer()
123
    {
124
        if (!DebugBar::$bufferingEnabled) {
125
            return;
126
        }
127
        $buffer = ob_get_clean();
128
        if (!empty($buffer)) {
129
            unset($_REQUEST['debug_request']); // Disable further messages that we can't intercept
130
            SilverStripeCollector::setDebugData($buffer);
131
        }
132
    }
133
}
134