Passed
Push — master ( eae59a...3582c1 )
by Nicolaas
09:19
created

ControllerExtension::onBeforeInit()   D

Complexity

Conditions 18
Paths 34

Size

Total Lines 63
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 18
eloc 39
c 5
b 0
f 0
nc 34
nop 0
dl 0
loc 63
rs 4.8666

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 Page;
0 ignored issues
show
Bug introduced by
The type Page 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 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...
7
use PhpParser\Node\Scalar\MagicConst\Dir;
8
use SilverStripe\CMS\Controllers\ContentController;
9
use SilverStripe\Control\Director;
10
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
11
use SilverStripe\Core\Extension;
12
use SilverStripe\Security\Security;
13
use SilverStripe\SiteConfig\SiteConfig;
14
use SilverStripe\Versioned\Versioned;
15
16
/**
17
 * Class \ControllerExtension.
18
 *
19
 * @property PageController|ControllerExtension $owner
20
 */
21
class ControllerExtension extends Extension
22
{
23
    public function onBeforeInit()
24
    {
25
        //make sure that caching is always https
26
        $controller = $this->getOwner();
27
        /** PageController|ControllerExtension $controller */
28
        if ($controller instanceof PageController) {
29
            $dataRecord = $controller->data();
30
            if (empty($dataRecord) || !$dataRecord instanceof Page) {
31
                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...
32
            }
33
            if ($dataRecord->PageCanBeCachedEntirely() !== true) {
34
                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...
35
            }
36
            if (Security::getCurrentUser()) {
37
                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...
38
            }
39
            if (Versioned::get_reading_mode() !== 'Stage.Live') {
40
                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...
41
            }
42
            if (!Director::isLive()) {
43
                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...
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
            if ($request->isGET() !== true) {
67
                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...
68
            }
69
            if ($controller->hasMethod('canCachePage')) {
70
                $canCachePage = $controller->canCachePage();
71
                if ($canCachePage !== true) {
72
                    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...
73
                }
74
            }
75
            $cacheTime = $dataRecord->PageCanBeCachedEntirelyDuration();
76
            if ($cacheTime > 0) {
77
                return HTTPCacheControlMiddleware::singleton()
78
                    ->enableCache()
79
                    ->setMaxAge($cacheTime)
80
                    ->publicCache(true)
81
                ;
82
            }
83
        }
84
85
        return null;
86
    }
87
88
    protected function returnNoCache()
89
    {
90
        HTTPCacheControlMiddleware::singleton()
91
            ->disableCache()
92
        ;
93
        return null;
94
    }
95
}
96