Passed
Push — master ( f0915f...7925ec )
by Alexander
01:20
created

Dependency   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 96.3%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 27
c 1
b 0
f 0
dl 0
loc 108
ccs 26
cts 27
cp 0.963
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A resetReusableData() 0 3 1
A generateReusableHash() 0 7 1
A evaluateDependency() 0 10 3
A markAsReusable() 0 3 1
A isChanged() 0 13 3
A iterableToArray() 0 3 2
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 2
    public function markAsReusable(): void
40
    {
41 2
        $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 9
    public function evaluateDependency(CacheInterface $cache): void
50
    {
51 9
        if ($this->isReusable) {
52 2
            $hash = $this->generateReusableHash();
53 2
            if (!\array_key_exists($hash, self::$reusableData)) {
54 2
                self::$reusableData[$hash] = $this->generateDependencyData($cache);
55
            }
56 2
            $this->data = self::$reusableData[$hash];
57
        } else {
58 7
            $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 7
    public function isChanged(CacheInterface $cache): bool
68
    {
69 7
        if ($this->isReusable) {
70 1
            $hash = $this->generateReusableHash();
71 1
            if (!\array_key_exists($hash, self::$reusableData)) {
72
                self::$reusableData[$hash] = $this->generateDependencyData($cache);
73
            }
74 1
            $data = self::$reusableData[$hash];
75
        } else {
76 6
            $data = $this->generateDependencyData($cache);
77
        }
78
79 7
        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
     * Generates a unique hash that can be used for retrieving reusable dependency data.
92
     * @return string a unique hash value for this cache dependency.
93
     * @see isReusable
94
     */
95 3
    protected function generateReusableHash(): string
96
    {
97 3
        $data = $this->data;
98 3
        $this->data = null;  // https://github.com/yiisoft/yii2/issues/3052
99 3
        $key = sha1(serialize($this));
100 3
        $this->data = $data;
101 3
        return $key;
102
    }
103
104
    /**
105
     * Converts iterable to array
106
     * @param iterable $iterable
107
     * @return array
108
     */
109 5
    protected function iterableToArray(iterable $iterable): array
110
    {
111 5
        return $iterable instanceof \Traversable ? iterator_to_array($iterable) : (array)$iterable;
112
    }
113
114
    /**
115
     * Generates the data needed to determine if dependency is changed.
116
     * Derived classes should override this method to generate the actual dependency data.
117
     * @param CacheInterface $cache the cache component that is currently evaluating this dependency
118
     * @return mixed the data needed to determine if dependency has been changed.
119
     */
120
    abstract protected function generateDependencyData(CacheInterface $cache);
121
}
122