Passed
Push — master ( 407d85...7d4e8a )
by Nicolaas
04:17 queued 15s
created

PageExtension::PageCanBeCached()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 15
nc 8
nop 0
dl 0
loc 25
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Sunnysideup\SimpleTemplateCaching\Extensions;
4
5
use SilverStripe\Forms\CheckboxField;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\NumericField;
8
use SilverStripe\Core\Extension;
9
use SilverStripe\Forms\LiteralField;
10
use SilverStripe\SiteConfig\SiteConfig;
11
12
/**
13
 * Class \Sunnysideup\SimpleTemplateCaching\Extensions\PageExtension.
14
 *
15
 * @property Page|PageExtension $owner
16
 * @property bool $NeverCachePublicly
17
 * @property int $PublicCacheDurationInSeconds
18
 */
19
class PageExtension extends Extension
20
{
21
    private static $db = [
22
        'NeverCachePublicly' => 'Boolean',
23
        'PublicCacheDurationInSeconds' => 'Int',
24
    ];
25
26
    public function updateSettingsFields(FieldList $fields)
27
    {
28
        $owner = $this->getOwner();
29
        $fields->addFieldsToTab(
30
            'Root.Cache',
31
            [
32
                CheckboxField::create(
33
                    'NeverCachePublicly',
34
                    'Never cache this page.
35
                    This should be checked if this page can show different information for different users or different situations.'
36
                ),
37
            ]
38
        );
39
        if (! (bool) $owner->NeverCachePublicly) {
40
            $fields->addFieldsToTab(
41
                'Root.Cache',
42
                [
43
                    NumericField::create(
44
                        'PublicCacheDurationInSeconds',
45
                        'In seconds, how long can this be cached for?'
46
                    )
47
                        ->setDescription(
48
                            'Use with care!<br />
49
                            Leave empty or zero to use the default value for the site<br />
50
                            This should only be used on pages that should be the same for all users and that should be accessible publicly.<br />
51
                            You can also set this value <a href="/admin/settings#Root_Caching">for the whole site</a>.<br />
52
                            Caching is ' . (SiteConfig::current_site_config()->HasCaching ? '' : 'NOT') . ' allowed on for this site.<br />
53
                            The current value for the whole site is ' . SiteConfig::current_site_config()->PublicCacheDurationInSeconds . ' seconds.<br />
54
                            '
55
                        ),
56
57
                ]
58
            );
59
        }
60
    }
61
62
    /**
63
     * Update Fields
64
     * @return FieldList
65
     */
66
    public function updateCMSFields(FieldList $fields)
67
    {
68
        $owner = $this->getOwner();
69
        $sc = SiteConfig::current_site_config();
0 ignored issues
show
Unused Code introduced by
The assignment to $sc is dead and can be removed.
Loading history...
70
        if ($owner->PageCanBeCachedEntirely()) {
71
            $fields->push(
72
                LiteralField::create(
73
                    'CacheInfo',
74
                    '<p class="message warning">Careful: this page can be cached publicly for up to ' . $owner->PageCanBeCachedEntirelyDuration() . ' seconds.</p>'
75
                )
76
            );
77
        }
78
        return $fields;
79
    }
80
81
    public function PageCanBeCachedEntirely(): bool
82
    {
83
        $owner = $this->getOwner();
84
85
        if ($owner->NeverCachePublicly) {
86
            return false;
87
        }
88
        $sc = SiteConfig::current_site_config();
89
        if (!$sc->HasCaching) {
90
            return false;
91
        }
92
        if ($owner->PageCanBeCachedEntirelyDuration() <= 0) {
93
            return false;
94
        }
95
        if ($owner->hasMethod('updateCacheControl')) {
96
            user_error('Please use canCachePage instead of updateCacheControl', E_USER_ERROR);
97
        }
98
        if ($owner->hasMethod('canCachePage')) {
99
            user_error('Please add the canCachePage method to your controller', E_USER_ERROR);
100
        }
101
102
        return true;
103
    }
104
105
    public function PageCanBeCachedEntirelyDuration(): int
106
    {
107
        return (int) (
108
            $this->getOwner()->PublicCacheDurationInSeconds ?:
109
            SiteConfig::current_site_config()->PublicCacheDurationInSeconds);
110
    }
111
}
112