Completed
Push — master ( b3fbb6...d0db0e )
by Lars
02:47
created

CacheChain::removeAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
namespace voku\cache;
4
5
/**
6
 * CacheChain: global-cache-chain class
7
 *
8
 * @package   voku\cache
9
 */
10
class CacheChain implements iCache
11
{
12
13
  /**
14
   * @var array iCache
15
   */
16
  private $caches = array();
17
18
  /**
19
   * __construct
20
   *
21
   * @param array $caches
22
   */
23
  public function __construct(array $caches = array())
24
  {
25
    array_map(
26
        array(
27
            $this,
28
            'addCache'
29
        ), $caches
30
    );
31
  }
32
33
  /**
34
   * get caches
35
   *
36
   * @return array
37
   */
38
  public function getCaches()
39
  {
40
    return $this->caches;
41
  }
42
43
  /**
44
   * add cache
45
   *
46
   * @param iCache  $cache
47
   * @param boolean $prepend
48
   *
49
   * @throws \InvalidArgumentException
50
   */
51
  public function addCache(iCache $cache, $prepend = true)
52
  {
53
    if ($this === $cache) {
54
      throw new \InvalidArgumentException('loop-error, put into other cache');
55
    }
56
57
    if ($prepend) {
58
      array_unshift($this->caches, $cache);
59
    } else {
60
      $this->caches[] = $cache;
61
    }
62
  }
63
64
  /**
65
   * @inheritdoc
66
   */
67
  public function getItem($key)
68
  {
69
    /* @var $cache iCache */
70
    foreach ($this->caches as $cache) {
71
      if ($cache->existsItem($key)) {
72
        return $cache->getItem($key);
73
      }
74
    }
75
76
    return null;
77
  }
78
79
  /**
80
   * @inheritdoc
81
   */
82
  public function setItem($key, $value, $ttl = null)
83
  {
84
    /* @var $cache iCache */
85
    foreach ($this->caches as $cache) {
86
      $cache->setItem($key, $value, $ttl);
87
    }
88
  }
89
90
  /**
91
   * @inheritdoc
92
   */
93
  public function setItemToDate($key, $value, \DateTime $date)
94
  {
95
    /* @var $cache iCache */
96
    foreach ($this->caches as $cache) {
97
      $cache->setItemToDate($key, $value, $date);
98
    }
99
  }
100
101
  /**
102
   * @inheritdoc
103
   */
104
  public function removeItem($key)
105
  {
106
    /* @var $cache iCache */
107
    foreach ($this->caches as $cache) {
108
      $cache->removeItem($key);
109
    }
110
  }
111
112
  /**
113
   * @inheritdoc
114
   */
115
  public function existsItem($key)
116
  {
117
    /* @var $cache iCache */
118
    foreach ($this->caches as $cache) {
119
      if ($cache->existsItem($key)) {
120
        return true;
121
      }
122
    }
123
124
    return false;
125
  }
126
127
  /**
128
   * @inheritdoc
129
   */
130
  public function removeAll()
131
  {
132
    /* @var $cache iCache */
133
    foreach ($this->caches as $cache) {
134
      $cache->removeAll();
135
    }
136
  }
137
}
138