Test Failed
Push — master ( 2a75d8...e4b8e4 )
by Julien
12:49
created

Cache::initializeCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 10
ccs 0
cts 5
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Mvc\Model;
13
14
use Phalcon\Mvc\ModelInterface;
15
use Zemit\Config\ConfigInterface;
16
use Zemit\Models\Audit;
17
use Zemit\Models\AuditDetail;
18
use Zemit\Models\Session;
19
20
/**
21
 * Flush Cache on changes
22
 *
23
 * @todo set cache keys
24
 * @todo improve to delete only necessary keys
25
 * @todo improve whiteList system
26
 * @todo precache system
27
 */
28
trait Cache
29
{
30
    /**
31
     * Set true to avoid flushing cache for the current instance
32
     */
33
    public bool $preventFlushCache = false;
34
    
35
    /**
36
     * Whitelisted classes to not force global cache flush on change
37
     */
38
    public array $flushModelsCacheBlackList = [];
39
    
40
    /**
41
     * Get modelsCache service from default DI
42
     */
43
    public function getModelsCache(): \Phalcon\Cache
44
    {
45
        return $this->getDI()->get('modelsCache');
0 ignored issues
show
Bug introduced by
It seems like getDI() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

45
        return $this->/** @scrutinizer ignore-call */ getDI()->get('modelsCache');
Loading history...
46
    }
47
    
48
    /**
49
     * Initializing Cache
50
     */
51
    public function initializeCache(): void
52
    {
53
        $config = $this->getDI()->get('config');
54
        assert($config instanceof ConfigInterface);
55
        
56
        $this->flushModelsCacheBlackList [] = $config->getModelClass(Session::class);
57
        $this->flushModelsCacheBlackList [] = $config->getModelClass(Audit::class);
58
        $this->flushModelsCacheBlackList [] = $config->getModelClass(AuditDetail::class);
59
        
60
        $this->addFlushCacheBehavior($this->flushModelsCacheBlackList);
61
    }
62
    
63
    /**
64
     * Adding Cache Behavior
65
     */
66
    public function addFlushCacheBehavior(?array $flushModelsCacheBlackList = null): void
67
    {
68
        $flushModelsCacheBlackList ??= $this->flushModelsCacheBlackList;
69
        
70
        // flush cache prevented by current instance
71
        if ($this->preventFlushCache) {
72
            return;
73
        }
74
        
75
        // flush cache prevented if current instance class is blacklisted
76
        if ($this->isThisInstanceOf($flushModelsCacheBlackList)) {
77
            return;
78
        }
79
        
80
        $modelsCache = $this->getModelsCache();
81
        $flushAction = function (ModelInterface $model) use ($modelsCache) {
82
            // Do not flush cache if nothing has changed
83
            return ($model->hasSnapshotData() && !($model->hasUpdated() || $model->hasChanged()))
0 ignored issues
show
Bug introduced by
The method hasUpdated() does not exist on Phalcon\Mvc\ModelInterface. It seems like you code against a sub-type of Phalcon\Mvc\ModelInterface such as Phalcon\Mvc\Model or Zemit\Models\Session or Zemit\Models\Audit or Zemit\Models\Role. ( Ignorable by Annotation )

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

83
            return ($model->hasSnapshotData() && !($model->/** @scrutinizer ignore-call */ hasUpdated() || $model->hasChanged()))
Loading history...
Bug introduced by
The method hasSnapshotData() does not exist on Phalcon\Mvc\ModelInterface. It seems like you code against a sub-type of Phalcon\Mvc\ModelInterface such as Phalcon\Mvc\Model or Zemit\Models\Session or Zemit\Models\Audit or Zemit\Models\Role. ( Ignorable by Annotation )

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

83
            return ($model->/** @scrutinizer ignore-call */ hasSnapshotData() && !($model->hasUpdated() || $model->hasChanged()))
Loading history...
Bug introduced by
The method hasChanged() does not exist on Phalcon\Mvc\ModelInterface. It seems like you code against a sub-type of Phalcon\Mvc\ModelInterface such as Phalcon\Mvc\Model or Zemit\Models\Session or Zemit\Models\Audit or Zemit\Models\Role. ( Ignorable by Annotation )

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

83
            return ($model->hasSnapshotData() && !($model->hasUpdated() || $model->/** @scrutinizer ignore-call */ hasChanged()))
Loading history...
84
                && $modelsCache->clear();
85
        };
86
        
87
        $actions = ['flush' => $flushAction];
88
        $this->addBehavior(new Behavior\Action([
0 ignored issues
show
Bug introduced by
It seems like addBehavior() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

88
        $this->/** @scrutinizer ignore-call */ 
89
               addBehavior(new Behavior\Action([
Loading history...
89
            'afterSave' => $actions,
90
            'afterCreate' => $actions,
91
            'afterUpdate' => $actions,
92
            'afterDelete' => $actions,
93
            'afterRestore' => $actions,
94
            'afterReorder' => $actions,
95
        ]));
96
    }
97
    
98
    /**
99
     * Check whether the current instance is any of the classes
100
     */
101
    public function isThisInstanceOf(array $classes = []): bool
102
    {
103
        // Prevent adding behavior to whiteListed models
104
        foreach ($classes as $class) {
105
            if ($this instanceof $class) {
106
                return true;
107
            }
108
        }
109
        
110
        return false;
111
    }
112
}
113