LangCache::getDependency()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @link https://github.com/LAV45/yii2-translated-behavior
4
 * @copyright Copyright (c) 2015 LAV45!
5
 * @author Alexey Loban <[email protected]>
6
 * @license http://opensource.org/licenses/BSD-3-Clause
7
 */
8
9
namespace lav45\translate\models;
10
11
use yii\di\Instance;
12
use yii\caching\CacheInterface;
13
use yii\caching\TagDependency;
14
15
/**
16
 * Class LangCache
17
 * @package lav45\translate\models
18
 */
19
class LangCache extends Lang
20
{
21
    /**
22
     * @var string
23
     */
24
    public static $cacheKey = 'LangCache::cacheKey';
25
    /**
26
     * @var string|array|CacheInterface
27
     */
28
    public $cache = 'cache';
29
30
    /**
31
     * @return \yii\caching\CacheInterface
32
     */
33
    public function getCache()
34
    {
35
        return Instance::ensure($this->cache, 'yii\caching\CacheInterface');
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function afterDelete()
42
    {
43
        parent::afterDelete();
44
        $this->invalidateCache();
45
    }
46
47
    /**
48
     * @inheritdoc
49
     * @param bool $insert
50
     * @param array $changedAttributes
51
     */
52
    public function afterSave($insert, $changedAttributes)
53
    {
54
        parent::afterSave($insert, $changedAttributes);
55
        if (!empty($changedAttributes)) {
56
            $this->invalidateCache();
57
        }
58
    }
59
60
    /**
61
     * Invalidate cache for all cached lists
62
     */
63
    public function invalidateCache()
64
    {
65
        TagDependency::invalidate($this->getCache(), static::$cacheKey);
66
    }
67
68
    /**
69
     * @return TagDependency
70
     */
71
    public static function getDependency()
72
    {
73
        return new TagDependency(['tags' => static::$cacheKey]);
74
    }
75
76
    /**
77
     * @param bool $active default false
78
     * @return array
79
     */
80
    public static function getList($active = false)
81
    {
82
        return static::getDb()->cache(function() use ($active) {
83
            return parent::getList($active);
84
        }, null, static::getDependency());
85
    }
86
87
    /**
88
     * @param bool $active default true
89
     * @return array
90
     */
91
    public static function getLocaleList($active = true)
92
    {
93
        return static::getDb()->cache(function() use ($active) {
94
            return parent::getLocaleList($active);
95
        }, null, static::getDependency());
96
    }
97
}