DataObjectExtension::onAfterDelete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Sunnysideup\SimpleTemplateCaching\Extensions;
4
5
use SilverStripe\Core\Config\Config;
6
use SilverStripe\Core\Extension;
7
use SilverStripe\Versioned\Versioned;
8
use Sunnysideup\SimpleTemplateCaching\Model\ObjectsUpdated;
9
10
/**
11
 * Class \Sunnysideup\SimpleTemplateCaching\Extensions\DataObjectExtension.
12
 *
13
 * @property DataObject|DataObjectExtension $owner
14
 */
15
class DataObjectExtension extends Extension
16
{
17
    public function onAfterWrite()
18
    {
19
        $owner = $this->getOwner();
20
21
        // NB.
22
        // if the dataobject has the versioned extension then the cache should be invalidated onAfterPublish
23
        // hasStages function is part of the Versioned class so safe to check here
24
        if (! $owner->hasExtension(Versioned::class)) {
25
            $this->doUpdateCache();
26
        } elseif (! $owner->hasStages()) {
27
            $this->doUpdateCache();
28
        }
29
    }
30
31
    public function onAfterDelete()
32
    {
33
        $this->doUpdateCache();
34
    }
35
36
    //* this function needs further consideration as it is called many times on the front end */
37
    // public function updateManyManyComponents()
38
    // {
39
    //     $owner = $this->owner;
40
    //     $className = $owner->ClassName;
41
42
    //     if(!$owner->hasExtension(Versioned::class)){
43
    //         $this->doUpdateCache($className);
44
    //     }
45
    //     //if the dataobject has the versioned extension then the cache should be invalidated onAfterPublish
46
    //     else if (!$owner->hasStages()){
47
    //         $this->doUpdateCache($className);
48
    //     }
49
    // }
50
51
    public function onBeforeRollback()
52
    {
53
        $this->doUpdateCache();
54
    }
55
56
    public function onAfterPublish()
57
    {
58
        $this->doUpdateCache();
59
    }
60
61
    public function onAfterArchive()
62
    {
63
        $this->doUpdateCache();
64
    }
65
66
    public function onAfterUnpublish()
67
    {
68
        $this->doUpdateCache();
69
    }
70
71
    public function onAfterVersionedPublish()
72
    {
73
        $this->doUpdateCache();
74
    }
75
76
    public function onAfterWriteToStage($toStage)
0 ignored issues
show
Unused Code introduced by
The parameter $toStage is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

76
    public function onAfterWriteToStage(/** @scrutinizer ignore-unused */ $toStage)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
77
    {
78
        $this->doUpdateCache();
79
    }
80
81
    private function doUpdateCache()
82
    {
83
        $className = (string) $this->getOwner()->ClassName;
84
        if ($className && $this->canUpdateCache($className)) {
85
            SimpleTemplateCachingSiteConfigExtension::update_cache_key($className);
86
        }
87
    }
88
89
    private function canUpdateCache($className): bool
90
    {
91
        // we want to always avoid this to avoid a loop.
92
        if (ObjectsUpdated::class === $className) {
93
            return false;
94
        }
95
        $excludedClasses = (array) Config::inst()->get(DataObjectExtension::class, 'excluded_classes_for_caching');
96
97
        return ! in_array($className, $excludedClasses, true);
98
    }
99
}
100