Passed
Push — master ( b017fa...6506ed )
by Julien
05:31 queued 23s
created

Cache   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 32
c 3
b 1
f 0
dl 0
loc 79
ccs 30
cts 33
cp 0.9091
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addFlushCacheBehavior() 0 29 6
A initializeCache() 0 10 1
A isInstanceOf() 0 12 3
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
use Zemit\Mvc\Model\AbstractTrait\AbstractModelsCache;
20
21
/**
22
 * Flush Cache on changes
23
 *
24
 * @todo set cache keys
25
 * @todo improve to delete only necessary keys
26
 * @todo improve whiteList system
27
 * @todo precache system
28
 */
29
trait Cache
30
{
31
    use AbstractModelsCache;
32
    
33
    /**
34
     * Set true to avoid flushing cache for the current instance
35
     */
36
    public bool $preventFlushCache = false;
37
    
38
    /**
39
     * Whitelisted classes to not force global cache flush on change
40
     */
41
    public array $flushModelsCacheBlackList = [];
42
    
43
    /**
44
     * Initializing Cache
45
     */
46 4
    public function initializeCache(): void
47
    {
48 4
        $config = $this->getDI()->get('config');
49 4
        assert($config instanceof ConfigInterface);
50
        
51 4
        $this->flushModelsCacheBlackList [] = $config->getModelClass(Session::class);
52 4
        $this->flushModelsCacheBlackList [] = $config->getModelClass(Audit::class);
53 4
        $this->flushModelsCacheBlackList [] = $config->getModelClass(AuditDetail::class);
54
        
55 4
        $this->addFlushCacheBehavior($this->flushModelsCacheBlackList);
56
    }
57
    
58
    /**
59
     * Adding Cache Behavior
60
     */
61 4
    public function addFlushCacheBehavior(?array $flushModelsCacheBlackList = null): void
62
    {
63 4
        $flushModelsCacheBlackList ??= $this->flushModelsCacheBlackList;
64
        
65
        // flush cache prevented by current instance
66 4
        if ($this->preventFlushCache) {
67
            return;
68
        }
69
        
70
        // flush cache prevented if current instance class is blacklisted
71 4
        if ($this->isInstanceOf($flushModelsCacheBlackList)) {
72
            return;
73
        }
74
        
75 4
        $modelsCache = $this->getModelsCache();
76 4
        $flushAction = function (ModelInterface $model) use ($modelsCache) {
77
            // Do not flush cache if nothing has changed
78 4
            return ($model->hasSnapshotData() && !($model->hasUpdated() || $model->hasChanged()))
0 ignored issues
show
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\Setting or Zemit\Models\Category or Zemit\Models\Audit or Zemit\Models\UserGroup or Zemit\Models\User or Zemit\Models\Field or Zemit\Models\Page or Zemit\Models\Log or Zemit\Models\File or Zemit\Models\Role or Zemit\Models\GroupRole or Zemit\Models\Template or Zemit\Models\AuditDetail or Zemit\Models\UserType or Zemit\Models\PhalconMigrations or Zemit\Models\Post or Zemit\Models\PostCategory or Zemit\Models\Session or Zemit\Models\TranslateField or Zemit\Models\Table or Zemit\Models\GroupType or Zemit\Models\Translate or Zemit\Models\WorkspaceLang or Zemit\Models\Email or Zemit\Models\Workspace or Zemit\Models\Data or Zemit\Models\Group or Zemit\Models\Lang or Zemit\Models\EmailFile or Zemit\Models\TranslateTable or Zemit\Models\UserRole or Zemit\Models\Flag or Zemit\Models\Menu or Zemit\Models\Type or Zemit\Models\Meta. ( Ignorable by Annotation )

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

78
            return ($model->/** @scrutinizer ignore-call */ hasSnapshotData() && !($model->hasUpdated() || $model->hasChanged()))
Loading history...
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\Setting or Zemit\Models\Category or Zemit\Models\Audit or Zemit\Models\UserGroup or Zemit\Models\User or Zemit\Models\Field or Zemit\Models\Page or Zemit\Models\Log or Zemit\Models\File or Zemit\Models\Role or Zemit\Models\GroupRole or Zemit\Models\Template or Zemit\Models\AuditDetail or Zemit\Models\UserType or Zemit\Models\PhalconMigrations or Zemit\Models\Post or Zemit\Models\PostCategory or Zemit\Models\Session or Zemit\Models\TranslateField or Zemit\Models\Table or Zemit\Models\GroupType or Zemit\Models\Translate or Zemit\Models\WorkspaceLang or Zemit\Models\Email or Zemit\Models\Workspace or Zemit\Models\Data or Zemit\Models\Group or Zemit\Models\Lang or Zemit\Models\EmailFile or Zemit\Models\TranslateTable or Zemit\Models\UserRole or Zemit\Models\Flag or Zemit\Models\Menu or Zemit\Models\Type or Zemit\Models\Meta. ( Ignorable by Annotation )

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

78
            return ($model->hasSnapshotData() && !($model->/** @scrutinizer ignore-call */ 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\Setting or Zemit\Models\Category or Zemit\Models\Audit or Zemit\Models\UserGroup or Zemit\Models\User or Zemit\Models\Field or Zemit\Models\Page or Zemit\Models\Log or Zemit\Models\File or Zemit\Models\Role or Zemit\Models\GroupRole or Zemit\Models\Template or Zemit\Models\AuditDetail or Zemit\Models\UserType or Zemit\Models\PhalconMigrations or Zemit\Models\Post or Zemit\Models\PostCategory or Zemit\Models\Session or Zemit\Models\TranslateField or Zemit\Models\Table or Zemit\Models\GroupType or Zemit\Models\Translate or Zemit\Models\WorkspaceLang or Zemit\Models\Email or Zemit\Models\Workspace or Zemit\Models\Data or Zemit\Models\Group or Zemit\Models\Lang or Zemit\Models\EmailFile or Zemit\Models\TranslateTable or Zemit\Models\UserRole or Zemit\Models\Flag or Zemit\Models\Menu or Zemit\Models\Type or Zemit\Models\Meta. ( Ignorable by Annotation )

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

78
            return ($model->hasSnapshotData() && !($model->hasUpdated() || $model->/** @scrutinizer ignore-call */ hasChanged()))
Loading history...
79 4
                && $modelsCache->clear();
80 4
        };
81
        
82 4
        $actions = ['flush' => $flushAction];
83 4
        $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

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