Passed
Push — master ( ad3fad...fe986e )
by Nicolaas
10:10
created

ControllerExtension::onBeforeInit()   C

Complexity

Conditions 16
Paths 14

Size

Total Lines 53
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 16
eloc 37
c 2
b 0
f 0
nc 14
nop 0
dl 0
loc 53
rs 5.5666

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
use SilverStripe\CMS\Controllers\ContentController;
4
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
5
use SilverStripe\Core\Extension;
6
use SilverStripe\Security\Security;
7
use SilverStripe\SiteConfig\SiteConfig;
8
use SilverStripe\Versioned\Versioned;
9
10
/**
11
 * Class \ControllerExtension.
12
 *
13
 * @property ControllerExtension $owner
14
 */
15
class ControllerExtension extends Extension
16
{
17
    public function onBeforeInit()
18
    {
19
        if(Security::getCurrentUser()) {
20
            return;
21
        }
22
        if (Versioned::LIVE !== Versioned::get_stage()) {
23
            return;
24
        }
25
        /** PageController|ControllerExtension $owner */
26
        $owner = $this->getOwner();
27
        if ($owner instanceof ContentController) {
28
            $extend = $owner->extend('updateCacheControl');
29
            if($extend) {
30
                return;
31
            }
32
            if($owner->param('Action')) {
33
                return;
34
            }
35
            if($owner->param('ID')) {
36
                return;
37
            }
38
            if($owner->request->isAjax()) {
39
                return;
40
            }
41
            if($owner->request->getVar('flush')) {
42
                return;
43
            }
44
            if($owner->request->requestVars()) {
45
                return;
46
            }
47
            $dataRecord = $owner->dataRecord;
48
            if (empty($dataRecord)) {
49
                return;
50
            }
51
            if($dataRecord->NeverCachePublicly) {
52
                HTTPCacheControlMiddleware::singleton()
53
                    ->disableCache()
54
                ;
55
                return;
56
            }
57
            if($dataRecord->PublicCacheDurationInSeconds === -1 || $dataRecord->PublicCacheDurationInSeconds === 0) {
58
                HTTPCacheControlMiddleware::singleton()
59
                    ->disableCache()
60
                ;
61
                return;
62
            }
63
            $sc = SiteConfig::current_site_config();
64
            if($sc->HasCaching) {
65
                $cacheTime = $dataRecord->PublicCacheDurationInSecond ?: $sc->PublicCacheDurationInSeconds;
66
                HTTPCacheControlMiddleware::singleton()
67
                    ->enableCache()
68
                    ->publicCache(true)
69
                    ->setMaxAge($cacheTime)
70
                ;
71
            }
72
        }
73
    }
74
}
75