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

RequestFilter::postRequest()   C

Complexity

Conditions 13
Paths 15

Size

Total Lines 46
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 5.1118
c 0
b 0
f 0
cc 13
eloc 22
nc 15
nop 2

How to fix   Complexity   

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;
4
5
use LeKoala\DebugBar\DebugBar;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Control\HTTPRequest;
8
use SilverStripe\Control\HTTPResponse;
9
use SilverStripe\Control\RequestFilter as BaseRequestFilter;
10
11
/**
12
 * A request filter to log pre request time
13
 */
14
class RequestFilter implements BaseRequestFilter
0 ignored issues
show
Deprecated Code introduced by
The interface SilverStripe\Control\RequestFilter has been deprecated with message: 4.0..5.0 Use HTTPMiddleware instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
15
{
16
17
    /**
18
     * Filter executed before a request processes
19
     *
20
     * {@inheritDoc}
21
     */
22
    public function preRequest(HTTPRequest $request)
0 ignored issues
show
Coding Style introduced by
preRequest uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
23
    {
24
        DebugBar::withDebugBar(function (DebugBar\DebugBar $debugbar) {
25
            /* @var $timeData DebugBar\DataCollector\TimeDataCollector */
26
            $timeData = $debugbar['time'];
27
            if (!$timeData) {
28
                return;
29
            }
30
            if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
31
                $timeData = $debugbar['time'];
32
                $timeData->addMeasure(
33
                    "framework boot",
34
                    $_SERVER['REQUEST_TIME_FLOAT'],
35
                    microtime(true)
36
                );
37
            }
38
            $timeData->startMeasure("pre_request", "pre request");
39
        });
40
    }
41
42
    /**
43
     * Filter executed AFTER a request
44
     *
45
     * {@inheritDoc}
46
     */
47
    public function postRequest(HTTPRequest $request, HTTPResponse $response)
48
    {
49
        $debugbar = DebugBar::getDebugBar();
50
        if (!$debugbar) {
51
            return;
52
        }
53
54
        // All queries have been displayed
55
        if (DebugBar::getShowQueries()) {
56
            exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method postRequest() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
57
        }
58
59
        $script = DebugBar::renderDebugBar();
60
61
        // If the bar is not renderable, return early
62
        if (!$script) {
63
            return;
64
        }
65
66
        // Inject init script into the HTML response
67
        $body = $response->getBody();
68
        if (strpos($body, '</body>') !== false) {
69
            $body = str_replace('</body>', $script . '</body>', $body);
70
            $response->setBody($body);
71
        }
72
73
        // Ajax support
74
        if (Director::is_ajax() && !headers_sent()) {
75
            if (DebugBar::isAdminUrl() && !DebugBar::config()->enabled_in_admin) {
76
                return;
77
            }
78
            // Skip anything that is not a GET request
79
            if (!$request->isGET()) {
80
                return;
81
            }
82
            // Always enable in admin because everything is mostly loaded through ajax
83
            if (DebugBar::config()->ajax || DebugBar::isAdminUrl()) {
84
                $headers = $debugbar->getDataAsHeaders();
85
86
                // Prevent throwing js errors in case header size is too large
87
                if (is_array($headers)) {
88
                    $debugbar->sendDataInHeaders();
89
                }
90
            }
91
        }
92
    }
93
}
94