Completed
Pull Request — master (#32)
by Alexander
01:45
created

Dependency::isEvaluated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 0
dl 0
loc 3
c 0
b 0
f 0
cc 1
rs 10
ccs 2
cts 2
cp 1
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Yiisoft\Cache\Dependency;
4
5
use Yiisoft\Cache\CacheInterface;
6
7
/**
8
 * Dependency is the base class for cache dependency classes.
9
 *
10
 * Child classes should override its {@see Dependency::generateDependencyData()} for generating
11
 * the actual dependency data.
12
 */
13
abstract class Dependency
14
{
15
    /**
16
     * @var mixed the dependency data that is saved in cache and later is compared with the
17
     * latest dependency data
18
     */
19
    protected $data;
20
21
    /**
22
     * @var bool whether this dependency is reusable or not. True value means that dependent
23
     * data for this cache dependency will be generated only once per request. This allows you
24
     * to use the same cache dependency for multiple separate cache calls while generating the same
25
     * page without an overhead of re-evaluating dependency data each time. Defaults to false.
26
     */
27
    protected $isReusable = false;
28
29
    /**
30
     * @var array static storage of cached data for reusable dependencies.
31
     */
32
    private static $reusableData = [];
33
34
    /**
35
     * Changes dependency behavior so dependent data for this cache dependency will be generated only once per request.
36
     * This allows you to use the same cache dependency for multiple separate cache calls while generating the same
37
     * page without an overhead of re-evaluating dependency data each time.
38
     */
39
    public function markAsReusable(): void
40
    {
41
        $this->isReusable = true;
42
    }
43
44
    /**
45
     * Evaluates the dependency by generating and saving the data related with dependency.
46
     * This method is invoked by cache before writing data into it.
47
     * @param CacheInterface $cache the cache component that is currently evaluating this dependency
48
     */
49 6
    public function evaluateDependency(CacheInterface $cache): void
50
    {
51 6
        if ($this->isReusable) {
52
            $hash = $this->generateReusableHash();
53
            if (!\array_key_exists($hash, self::$reusableData)) {
54
                self::$reusableData[$hash] = $this->generateDependencyData($cache);
55
            }
56
            $this->data = self::$reusableData[$hash];
57
        } else {
58 6
            $this->data = $this->generateDependencyData($cache);
59
        }
60
    }
61
62
    /**
63
     * Checks whether the dependency is changed.
64
     * @param CacheInterface $cache the cache component that is currently evaluating this dependency
65
     * @return bool whether the dependency has changed.
66
     */
67 6
    public function isChanged(CacheInterface $cache): bool
68
    {
69 6
        if ($this->isReusable) {
70
            $hash = $this->generateReusableHash();
71
            if (!\array_key_exists($hash, self::$reusableData)) {
72
                self::$reusableData[$hash] = $this->generateDependencyData($cache);
73
            }
74
            $data = self::$reusableData[$hash];
75
        } else {
76 6
            $data = $this->generateDependencyData($cache);
77
        }
78
79 6
        return $data !== $this->data;
80
    }
81
82
    /**
83
     * Resets all cached data for reusable dependencies.
84
     */
85 1
    public static function resetReusableData(): void
86
    {
87 1
        self::$reusableData = [];
88
    }
89
90
    /**
91
     * Checks whether the dependency has already been evaluated.
92
     * @return bool
93
     */
94 4
    public function isEvaluated(): bool
95
    {
96 4
        return $this->data !== null;
97
    }
98
99
    /**
100
     * Generates a unique hash that can be used for retrieving reusable dependency data.
101
     * @return string a unique hash value for this cache dependency.
102
     * @see isReusable
103
     */
104 1
    protected function generateReusableHash(): string
105
    {
106 1
        $data = $this->data;
107 1
        $this->data = null;  // https://github.com/yiisoft/yii2/issues/3052
108 1
        $key = sha1(serialize($this));
109 1
        $this->data = $data;
110 1
        return $key;
111
    }
112
113
    /**
114
     * Generates the data needed to determine if dependency is changed.
115
     * Derived classes should override this method to generate the actual dependency data.
116
     * @param CacheInterface $cache the cache component that is currently evaluating this dependency
117
     * @return mixed the data needed to determine if dependency has been changed.
118
     */
119
    abstract protected function generateDependencyData(CacheInterface $cache);
120
}
121