ControllerExtension::beforeCallActionHandler()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 13
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 23
rs 8.8333
1
<?php
2
3
namespace LeKoala\DebugBar\Extension;
4
5
use LeKoala\DebugBar\DebugBar;
6
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...
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Core\Extension;
9
use LeKoala\DebugBar\Collector\HeaderCollector;
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/ModelAsController
40
        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...
41
            return;
42
        }
43
        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...
44
            return;
45
        }
46
        DebugBar::includeRequirements();
47
48
        DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) {
49
            // Add the headers Collector
50
            if (!$debugbar->hasCollector('Headers') && DebugBar::config()->get('header_collector')) {
51
                $debugbar->addCollector(new HeaderCollector($this->owner));
52
            }
53
            /** @var DebugBar\DataCollector\TimeDataCollector $timeData */
54
            $timeData = $debugbar->getCollector('time');
55
            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...
56
                return;
57
            }
58
            if ($timeData->hasStartedMeasure("cms_init")) {
59
                $timeData->stopMeasure("cms_init");
60
            }
61
            if ($timeData->hasStartedMeasure("init")) {
62
                $timeData->stopMeasure("init");
63
            }
64
            $timeData->startMeasure("handle", get_class($this->owner) . ' handle request');
65
        });
66
    }
67
68
    /**
69
     * @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...
70
     * @param string $action
71
     */
72
    public function beforeCallActionHandler($request, $action)
73
    {
74
        // If we don't have an action, getViewer will be called immediatly
75
        // If we have custom routes, request action is different than action
76
        $allParams     = $request->allParams();
77
        $requestAction = null;
78
        if (!empty($allParams['Action'])) {
79
            $requestAction = $allParams['Action'];
80
        }
81
        if (!$this->owner->hasMethod($action) || ($requestAction && $requestAction != $action)) {
82
            self::clearBuffer();
83
        }
84
85
        DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugBar) use ($action) {
86
            /** @var DebugBar\DataCollector\TimeDataCollector $timeData */
87
            $timeData = $debugBar->getCollector('time');
88
            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...
89
                return;
90
            }
91
            if ($timeData->hasStartedMeasure("handle")) {
92
                $timeData->stopMeasure("handle");
93
            }
94
            $timeData->startMeasure("action", get_class($this->owner) . " action $action");
95
        });
96
    }
97
98
    /**
99
     * @param HTTPRequest $request
100
     * @param string $action
101
     * @param mixed $result
102
     */
103
    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

103
    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

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