Passed
Push — master ( 803223...93cfec )
by Nicolaas
09:46
created

PageControllerExtension::CacheKeyHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 1
c 4
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Sunnysideup\SimpleTemplateCaching\Extensions;
4
5
use SilverStripe\Core\Extension;
6
use SilverStripe\Security\Security;
7
8
class PageControllerExtension extends Extension
9
{
10
    /**
11
     * @var null|string
12
     */
13
    protected static $_cache_key_any_data_object_changes;
14
15
    /**
16
     * @var null|bool
17
     */
18
    private static $_can_cache_content;
19
20
    /**
21
     * @var string
22
     */
23
    private static $_can_cache_content_string = '';
24
25
    public function HasCacheKeys(): bool
26
    {
27
        if (null === self::$_can_cache_content) {
28
            self::$_can_cache_content_string = '';
29
            if ($this->owner->hasMethod('canCachePage')) {
30
                self::$_can_cache_content_string = $this->owner->canCachePage() ? '' : 'can-no-cache-' . $this->owner->ID;
31
                if (! $this->canCacheCheck()) {
32
                    return false;
33
                }
34
            }
35
36
            //action
37
            $action = $this->owner->request->param('Action');
38
            if ($action) {
39
                self::$_can_cache_content_string .= $action;
40
                if (! $this->canCacheCheck()) {
41
                    return false;
42
                }
43
            }
44
45
            $id = $this->owner->request->param('ID');
46
            // id
47
            if ($id) {
48
                self::$_can_cache_content_string .= $id;
49
                if (! $this->canCacheCheck()) {
50
                    return false;
51
                }
52
            }
53
54
            //request vars
55
            $requestVars = $this->owner->request->requestVars();
56
            if ($requestVars) {
57
                foreach ($this->owner->request->requestVars() as $item) {
58
                    if (is_string($item)) {
59
                        self::$_can_cache_content_string .= $item;
60
                    } elseif (is_numeric($item)) {
61
                        self::$_can_cache_content_string .= $item;
62
                    }
63
64
                    if (! $this->canCacheCheck()) {
65
                        return false;
66
                    }
67
                }
68
            }
69
70
            //member
71
            $member = Security::getCurrentUser();
72
            if ($member && $member->exists()) {
73
                self::$_can_cache_content_string .= $member->ID;
74
                if (! $this->canCacheCheck()) {
75
                    return false;
76
                }
77
            }
78
79
            // we are ok!
80
            self::$_can_cache_content = true;
81
        }
82
83
        return self::$_can_cache_content;
84
    }
85
86
    public function HasCacheKeyHeader(): bool
87
    {
88
        return $this->HasCacheKeys();
89
    }
90
91
    public function HasCacheKeyMenu(): bool
92
    {
93
        return $this->HasCacheKeys();
94
    }
95
96
    public function HasCacheKeyContent(): bool
97
    {
98
        return $this->HasCacheKeys();
99
    }
100
101
    public function HasCacheKeyFooter(): bool
102
    {
103
        return $this->HasCacheKeys();
104
    }
105
106
    public function CacheKeyHeader(): string
107
    {
108
        return $this->CacheKeyGenerator('H');
109
    }
110
111
    public function CacheKeyMenu(): string
112
    {
113
        return $this->CacheKeyGenerator('M');
114
    }
115
116
    public function CacheKeyFooter(): string
117
    {
118
        return $this->CacheKeyGenerator('F');
119
    }
120
121
    public function CacheKeyContent(): string
122
    {
123
        $cacheKey = $this->CacheKeyGenerator('C');
124
        if ($this->owner->hasMethod('CacheKeyContentCustom')) {
125
            $cacheKey .= '_' . $this->owner->CacheKeyContentCustom();
126
        }
127
128
        return $cacheKey;
129
    }
130
131
    public function CacheKeyGenerator($letter): string
132
    {
133
        if ($this->HasCacheKeys()) {
134
            $string = $letter . '_' .
135
                $this->cacheKeyAnyDataObjectChanges() . '_' .
136
                'ID_' . $this->owner->ID;
137
        } else {
138
            $string = 'NOT_CACHED_' . time() . '_' . rand(0, 999);
139
        }
140
141
        return $string;
142
    }
143
144
    protected function canCacheCheck(): bool
145
    {
146
        if ('' !== trim(self::$_can_cache_content_string)) {
147
            self::$_can_cache_content = false;
148
149
            return false;
150
        }
151
152
        return true;
153
    }
154
155
    protected function getCanCacheContentString(): string
156
    {
157
        return self::$_can_cache_content_string;
158
    }
159
160
    protected function cacheKeyAnyDataObjectChanges(): string
161
    {
162
        if (null === self::$_cache_key_any_data_object_changes) {
163
            self::$_cache_key_any_data_object_changes = SimpleTemplateCachingSiteConfigExtension::site_cache_key();
164
            self::$_cache_key_any_data_object_changes .= $this->getCanCacheContentString();
165
        }
166
167
        return self::$_cache_key_any_data_object_changes;
168
    }
169
}
170