Completed
Push — master ( 28797f...652832 )
by Alexander
01:44
created

ChainedDependency   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 14
dl 0
loc 62
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isChanged() 0 13 6
A evaluateDependency() 0 4 2
A generateDependencyData() 0 3 1
1
<?php
2
namespace Yiisoft\Cache\Dependency;
3
4
use Yiisoft\Cache\CacheInterface;
5
6
/**
7
 * ChainedDependency represents a dependency which is composed of a list of other dependencies.
8
 *
9
 * When {@see dependOnAll} is true, if any of the dependencies has changed, this dependency is
10
 * considered changed; When {@see dependOnAll} is false, if one of the dependencies has NOT changed,
11
 * this dependency is considered NOT changed.
12
 *
13
 * For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
14
 */
15
final class ChainedDependency extends Dependency
16
{
17
    /**
18
     * @var Dependency[]
19
     */
20
    private $dependencies;
21
22
    /**
23
     * @var bool
24
     */
25
    private $dependOnAll;
26
27
    /**
28
     * ChainedDependency constructor.
29
     * @param Dependency[] $dependencies list of dependencies that this dependency is composed of.
30
     * Each array element must be a dependency object.
31
     * @param bool $dependOnAll whether this dependency is depending on every dependency in {@see dependencies}.
32
     * Defaults to true, meaning if any of the dependencies has changed, this dependency is considered changed.
33
     * When it is set false, it means if one of the dependencies has NOT changed, this dependency
34
     * is considered NOT changed.
35
     */
36
    public function __construct(array $dependencies = [], bool $dependOnAll = true)
37
    {
38
        $this->dependencies = $dependencies;
39
        $this->dependOnAll = $dependOnAll;
40
    }
41
42
    /**
43
     * Evaluates the dependency by generating and saving the data related with dependency.
44
     * @param CacheInterface $cache the cache component that is currently evaluating this dependency
45
     */
46
    public function evaluateDependency(CacheInterface $cache): void
47
    {
48
        foreach ($this->dependencies as $dependency) {
49
            $dependency->evaluateDependency($cache);
50
        }
51
    }
52
53
    /**
54
     * Generates the data needed to determine if dependency has been changed.
55
     * This method does nothing in this class.
56
     * @param CacheInterface $cache the cache component that is currently evaluating this dependency
57
     * @return mixed the data needed to determine if dependency has been changed.
58
     */
59
    protected function generateDependencyData(CacheInterface $cache)
60
    {
61
        return null;
62
    }
63
64
    public function isChanged(CacheInterface $cache): bool
65
    {
66
        foreach ($this->dependencies as $dependency) {
67
            if ($this->dependOnAll && $dependency->isChanged($cache)) {
68
                return true;
69
            }
70
71
            if (!$this->dependOnAll && !$dependency->isChanged($cache)) {
72
                return false;
73
            }
74
        }
75
76
        return !$this->dependOnAll;
77
    }
78
}
79