Passed
Push — master ( 5e6d7b...38474e )
by Nicolaas
04:20 queued 17s
created

ControllerExtension::onBeforeInit()   D

Complexity

Conditions 18
Paths 58

Size

Total Lines 67
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 18
eloc 40
c 2
b 0
f 0
nc 58
nop 0
dl 0
loc 67
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 PhpParser\Node\Scalar\MagicConst\Dir;
6
use SilverStripe\CMS\Controllers\ContentController;
7
use SilverStripe\Control\Director;
8
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
9
use SilverStripe\Core\Extension;
10
use SilverStripe\Security\Security;
11
use SilverStripe\SiteConfig\SiteConfig;
12
use SilverStripe\Versioned\Versioned;
13
14
/**
15
 * Class \ControllerExtension.
16
 *
17
 * @property PageController|ControllerExtension $owner
18
 */
19
class ControllerExtension extends Extension
20
{
21
    public function onBeforeInit()
22
    {
23
        //make sure that caching is always https
24
        $owner = $this->getOwner();
25
        if (Director::isLive()) {
26
            $owner->response->addHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');
27
        }
28
        if (Security::getCurrentUser()) {
29
            return null;
30
        }
31
        if (Versioned::get_reading_mode() !== 'Stage.Live') {
32
            return null;
33
        }
34
35
        $sc = SiteConfig::current_site_config();
36
        if (! $sc->HasCaching) {
37
            return null;
38
        }
39
        /** PageController|ControllerExtension $owner */
40
        if ($owner instanceof ContentController) {
41
            $dataRecord = $owner->data();
42
            if (empty($dataRecord)) {
43
                return null;
44
            }
45
            if ($dataRecord->NeverCachePublicly) {
46
                HTTPCacheControlMiddleware::singleton()
47
                    ->disableCache()
48
                ;
49
                return null;
50
            }
51
52
            if ($owner->hasMethod('updateCacheControl')) {
53
                $extend = $owner->extend('updateCacheControl');
54
                if ($extend) {
55
                    return null;
56
                }
57
            }
58
59
            $request = $owner->getRequest();
60
            if ($owner->hasMethod('cacheControlExcludedActions')) {
61
                $excludeActions = $owner->cacheControlExcludedActions();
62
                if ($request->param('Action') && in_array($request->param('Action'), $excludeActions)) {
63
                    return null;
64
                }
65
            }
66
            if ($request->isAjax()) {
67
                return null;
68
            }
69
            if ($request->getVar('flush')) {
70
                return null;
71
            }
72
            if ($request->requestVars()) {
73
                return null;
74
            }
75
76
77
            $cacheTime = (int) ($dataRecord->PublicCacheDurationInSeconds ?: $sc->PublicCacheDurationInSeconds);
78
            if ($cacheTime > 0) {
79
                return HTTPCacheControlMiddleware::singleton()
80
                    ->enableCache()
81
                    ->setMaxAge($cacheTime)
82
                    ->publicCache(true)
83
                ;
84
            }
85
        }
86
87
        return null;
88
    }
89
}
90