Passed
Push — master ( 202659...08aad6 )
by Nicolaas
03:46
created

ControllerExtension::onBeforeInit()   C

Complexity

Conditions 14
Paths 42

Size

Total Lines 57
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 14
eloc 33
nc 42
nop 0
dl 0
loc 57
rs 6.2666
c 3
b 0
f 0

How to fix   Long Method    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 Sunnysideup\SimpleTemplateCaching\Extensions;
4
5
use PageController;
0 ignored issues
show
Bug introduced by
The type PageController 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...
6
use PhpParser\Node\Scalar\MagicConst\Dir;
7
use SilverStripe\CMS\Controllers\ContentController;
8
use SilverStripe\Control\Director;
9
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
10
use SilverStripe\Core\Extension;
11
use SilverStripe\Security\Security;
12
use SilverStripe\SiteConfig\SiteConfig;
13
use SilverStripe\Versioned\Versioned;
14
15
/**
16
 * Class \ControllerExtension.
17
 *
18
 * @property PageController|ControllerExtension $controller
19
 */
20
class ControllerExtension extends Extension
21
{
22
    public function onBeforeInit()
23
    {
24
        //make sure that caching is always https
25
        $controller = $this->getOwner();
26
        if (Director::isLive()) {
27
            $controller->response->addHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');
28
        }
29
        /** PageController|ControllerExtension $controller */
30
        if ($controller instanceof PageController) {
31
            $dataRecord = $controller->data();
32
            if (empty($dataRecord)) {
33
                return $this->returnNoCache();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->returnNoCache() targeting Sunnysideup\SimpleTempla...ension::returnNoCache() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
34
            }
35
            if ($dataRecord->PageCanBeCached() !== true) {
36
                return $this->returnNoCache();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->returnNoCache() targeting Sunnysideup\SimpleTempla...ension::returnNoCache() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
37
            }
38
            if (Security::getCurrentUser()) {
39
                return $this->returnNoCache();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->returnNoCache() targeting Sunnysideup\SimpleTempla...ension::returnNoCache() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
40
            }
41
            if (Versioned::get_reading_mode() !== 'Stage.Live') {
42
                return $this->returnNoCache();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->returnNoCache() targeting Sunnysideup\SimpleTempla...ension::returnNoCache() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
43
            }
44
45
46
            // exclude special situations...
47
            $request = $controller->getRequest();
48
            if ($controller->hasMethod('cacheControlExcludedActions')) {
49
                $excludeActions = $controller->cacheControlExcludedActions();
50
                $action = strtolower($request->param('Action'));
51
                if ($action) {
52
                    if (in_array($action, $excludeActions)) {
53
                        return $this->returnNoCache();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->returnNoCache() targeting Sunnysideup\SimpleTempla...ension::returnNoCache() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
54
                    }
55
                }
56
            }
57
            if ($request->isAjax()) {
58
                return $this->returnNoCache();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->returnNoCache() targeting Sunnysideup\SimpleTempla...ension::returnNoCache() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
59
            }
60
            if ($request->getVar('flush')) {
61
                return $this->returnNoCache();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->returnNoCache() targeting Sunnysideup\SimpleTempla...ension::returnNoCache() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
62
            }
63
            if ($request->requestVars()) {
64
                return $this->returnNoCache();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->returnNoCache() targeting Sunnysideup\SimpleTempla...ension::returnNoCache() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
65
            }
66
67
68
            $cacheTime = $dataRecord->CacheDurationInSeconds();
69
            if ($cacheTime > 0) {
70
                return HTTPCacheControlMiddleware::singleton()
71
                    ->enableCache()
72
                    ->setMaxAge($cacheTime)
73
                    ->publicCache(true)
74
                ;
75
            }
76
        }
77
78
        return null;
79
    }
80
81
    protected function returnNoCache()
82
    {
83
        HTTPCacheControlMiddleware::singleton()
84
            ->disableCache()
85
        ;
86
        return null;
87
    }
88
}
89