Test Failed
Push — master ( a835f3...4c7cec )
by Julien
04:50
created

Cache::isThisInstanceOf()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 10
ccs 0
cts 5
cp 0
crap 12
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
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
    public function initializeCache(): void
47
    {
48
        $config = $this->getDI()->get('config');
49
        assert($config instanceof ConfigInterface);
50
        
51
        $this->flushModelsCacheBlackList [] = $config->getModelClass(Session::class);
52
        $this->flushModelsCacheBlackList [] = $config->getModelClass(Audit::class);
53
        $this->flushModelsCacheBlackList [] = $config->getModelClass(AuditDetail::class);
54
        
55
        $this->addFlushCacheBehavior($this->flushModelsCacheBlackList);
56
    }
57
    
58
    /**
59
     * Adding Cache Behavior
60
     */
61
    public function addFlushCacheBehavior(?array $flushModelsCacheBlackList = null): void
62
    {
63
        $flushModelsCacheBlackList ??= $this->flushModelsCacheBlackList;
64
        
65
        // flush cache prevented by current instance
66
        if ($this->preventFlushCache) {
67
            return;
68
        }
69
        
70
        // flush cache prevented if current instance class is blacklisted
71
        if ($this->isInstanceOf($flushModelsCacheBlackList)) {
72
            return;
73
        }
74
        
75
        $modelsCache = $this->getModelsCache();
76
        $flushAction = function (ModelInterface $model) use ($modelsCache) {
77
            // Do not flush cache if nothing has changed
78
            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\Post or Zemit\Models\PostCategory or Zemit\Models\Session or Zemit\Models\TranslateField or Zemit\Models\GroupType or Zemit\Models\Translate or Zemit\Models\Email or Zemit\Models\Data or Zemit\Models\Group or Zemit\Models\Lang or Zemit\Models\EmailFile or Zemit\Models\TranslateTable or Zemit\Models\SiteLang or Zemit\Models\UserRole or Zemit\Models\Flag or Zemit\Models\Menu or Zemit\Models\Site or Zemit\Models\Type or Zemit\Models\Channel 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\Post or Zemit\Models\PostCategory or Zemit\Models\Session or Zemit\Models\TranslateField or Zemit\Models\GroupType or Zemit\Models\Translate or Zemit\Models\Email or Zemit\Models\Data or Zemit\Models\Group or Zemit\Models\Lang or Zemit\Models\EmailFile or Zemit\Models\TranslateTable or Zemit\Models\SiteLang or Zemit\Models\UserRole or Zemit\Models\Flag or Zemit\Models\Menu or Zemit\Models\Site or Zemit\Models\Type or Zemit\Models\Channel 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\Post or Zemit\Models\PostCategory or Zemit\Models\Session or Zemit\Models\TranslateField or Zemit\Models\GroupType or Zemit\Models\Translate or Zemit\Models\Email or Zemit\Models\Data or Zemit\Models\Group or Zemit\Models\Lang or Zemit\Models\EmailFile or Zemit\Models\TranslateTable or Zemit\Models\SiteLang or Zemit\Models\UserRole or Zemit\Models\Flag or Zemit\Models\Menu or Zemit\Models\Site or Zemit\Models\Type or Zemit\Models\Channel 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
                && $modelsCache->clear();
80
        };
81
        
82
        $actions = ['flush' => $flushAction];
83
        $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
            'afterSave' => $actions,
85
            'afterCreate' => $actions,
86
            'afterUpdate' => $actions,
87
            'afterDelete' => $actions,
88
            'afterRestore' => $actions,
89
            'afterReorder' => $actions,
90
        ]));
91
    }
92
    
93
    /**
94
     * Check whether the current instance is any of the classes
95
     */
96
    public function isInstanceOf(array $classes = [], ?ModelInterface $that = null): bool
97
    {
98
        $that ??= $this;
99
        
100
        // Prevent adding behavior to whiteListed models
101
        foreach ($classes as $class) {
102
            if ($that instanceof $class) {
103
                return true;
104
            }
105
        }
106
        
107
        return false;
108
    }
109
}
110