Passed
Push — 2.2 ( 0a3394...9a5e2a )
by Paweł
16:06
created

Dependency   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 101
ccs 27
cts 27
cp 1
rs 10
c 1
b 0
f 0
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A evaluateDependency() 0 10 3
A resetReusableData() 0 3 1
A generateReusableHash() 0 18 5
A isChanged() 0 13 3
1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7
8
namespace yii\caching;
9
10
/**
11
 * Dependency is the base class for cache dependency classes.
12
 *
13
 * Child classes should override its [[generateDependencyData()]] for generating
14
 * the actual dependency data.
15
 *
16
 * For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
17
 *
18
 * @author Qiang Xue <[email protected]>
19
 * @since 2.0
20
 */
21
abstract class Dependency extends \yii\base\BaseObject
22
{
23
    /**
24
     * @var mixed the dependency data that is saved in cache and later is compared with the
25
     * latest dependency data.
26
     */
27
    public $data;
28
    /**
29
     * @var bool whether this dependency is reusable or not. True value means that dependent
30
     * data for this cache dependency will be generated only once per request. This allows you
31
     * to use the same cache dependency for multiple separate cache calls while generating the same
32
     * page without an overhead of re-evaluating dependency data each time. Defaults to false.
33
     */
34
    public $reusable = false;
35
36
    /**
37
     * @var array static storage of cached data for reusable dependencies.
38
     */
39
    private static $_reusableData = [];
40
41
42
    /**
43
     * Evaluates the dependency by generating and saving the data related with dependency.
44
     * This method is invoked by cache before writing data into it.
45
     * @param CacheInterface $cache the cache component that is currently evaluating this dependency
46
     */
47 9
    public function evaluateDependency($cache)
48
    {
49 9
        if ($this->reusable) {
50 1
            $hash = $this->generateReusableHash();
51 1
            if (!array_key_exists($hash, self::$_reusableData)) {
52 1
                self::$_reusableData[$hash] = $this->generateDependencyData($cache);
53
            }
54 1
            $this->data = self::$_reusableData[$hash];
55
        } else {
56 8
            $this->data = $this->generateDependencyData($cache);
57
        }
58
    }
59
60
    /**
61
     * Checks whether the dependency is changed.
62
     * @param CacheInterface $cache the cache component that is currently evaluating this dependency
63
     * @return bool whether the dependency has changed.
64
     * @since 2.0.11
65
     */
66 7
    public function isChanged($cache)
67
    {
68 7
        if ($this->reusable) {
69 1
            $hash = $this->generateReusableHash();
70 1
            if (!array_key_exists($hash, self::$_reusableData)) {
71 1
                self::$_reusableData[$hash] = $this->generateDependencyData($cache);
72
            }
73 1
            $data = self::$_reusableData[$hash];
74
        } else {
75 6
            $data = $this->generateDependencyData($cache);
76
        }
77
78 7
        return $data !== $this->data;
79
    }
80
81
    /**
82
     * Resets all cached data for reusable dependencies.
83
     */
84 1
    public static function resetReusableData()
85
    {
86 1
        self::$_reusableData = [];
87
    }
88
89
    /**
90
     * Generates a unique hash that can be used for retrieving reusable dependency data.
91
     *
92
     * @return string a unique hash value for this cache dependency.
93
     * @see reusable
94
     */
95 2
    protected function generateReusableHash()
96
    {
97 2
        $clone = clone $this;
98 2
        $clone->data = null; // https://github.com/yiisoft/yii2/issues/3052
99
100
        try {
101 2
            $serialized = serialize($clone);
0 ignored issues
show
Bug introduced by
The function serialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

101
            $serialized = /** @scrutinizer ignore-call */ serialize($clone);
Loading history...
102 1
        } catch (\Exception $e) {
103
            // unserializable properties are nulled
104 1
            foreach ($clone as $name => $value) {
105 1
                if (is_object($value) && $value instanceof \Closure) {
0 ignored issues
show
Bug introduced by
The function is_object was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

105
                if (/** @scrutinizer ignore-call */ is_object($value) && $value instanceof \Closure) {
Loading history...
106 1
                    $clone->{$name} = null;
107
                }
108
            }
109 1
            $serialized = serialize($clone);
110
        }
111
112 2
        return sha1($serialized);
113
    }
114
115
    /**
116
     * Generates the data needed to determine if dependency is changed.
117
     * Derived classes should override this method to generate the actual dependency data.
118
     * @param CacheInterface $cache the cache component that is currently evaluating this dependency
119
     * @return mixed the data needed to determine if dependency has been changed.
120
     */
121
    abstract protected function generateDependencyData($cache);
122
}
123