Completed
Push — master ( b5c840...3d5461 )
by Marcus
04:46
created

ConfigurableCacheAdapter   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 0
loc 217
ccs 0
cts 63
cp 0
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 3
A cacheKey() 0 4 1
A notCached() 0 4 1
A addReference() 0 4 1
A fromCache() 0 4 1
A flushCache() 0 4 1
A invalidateTags() 0 4 1
A removeCache() 0 4 1
A raiseCounter() 0 4 1
A mergeAttributesRecursive() 0 4 1
A isCached() 0 11 2
A toCache() 0 8 3
1
<?php
2
3
/**
4
 * TechDivision\Import\Cache\GenericCacheAdapter
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2019 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Cache;
22
23
use TechDivision\Import\Configuration\ConfigurationInterface;
24
use TechDivision\Import\Utils\CacheTypes;
25
26
/**
27
 * Configurable cache adapter implementation.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2019 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import
33
 * @link      http://www.techdivision.com
34
 */
35
class ConfigurableCacheAdapter implements CacheAdapterInterface
36
{
37
38
    /**
39
     * The cache adatper instance.
40
     *
41
     * @var \TechDivision\Import\Cache\CacheAdapterInterface
42
     */
43
    protected $cacheAdapter;
44
45
    /**
46
     * The TTL used to cache items.
47
     *
48
     * @var integer
49
     */
50
    protected $time = null;
51
52
    /**
53
     * The flag if the cache is anabled or not.
54
     *
55
     * @var boolean
56
     */
57
    protected $enabled = true;
58
59
    /**
60
     * The array with the default tags.
61
     *
62
     * @var string
63
     */
64
    protected $tags = array();
65
66
    /**
67
     * Initialize the cache handler with the passed cache and configuration instances.
68
     * .
69
     * @param \TechDivision\Import\Cache\CacheAdapterInterface          $cacheAdapter  The cache instance
70
     * @param \TechDivision\Import\Configuration\ConfigurationInterface $configuration The configuration instance
71
     * @param string                                                    $type          The cache type to use
72
     */
73
    public function __construct(
74
        CacheAdapterInterface $cacheAdapter,
75
        ConfigurationInterface $configuration,
76
        $type = CacheTypes::TYPE_STATIC
77
    ) {
78
79
        // set the cache adapter to use
80
        $this->cacheAdapter = $cacheAdapter;
81
82
        // append the serial to the array with the default tags
83
        $this->tags = array_merge(array($configuration->getSerial()));
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge(array($configuration->getSerial())) of type array<integer,string,{"0":"string"}> is incompatible with the declared type string of property $tags.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
84
85
        // load the configuration for the passed cache type
86
        if ($cacheConfiguration = $configuration->getCacheByType($type)) {
87
            // initialize the ttl and the enabled flag
88
            $this->time = $cacheConfiguration->getTime();
89
            $this->enabled = $cacheConfiguration->isEnabled();
90
        } else {
91
            $this->enabled = ($configuration->isCacheEnabled() || $type === CacheTypes::TYPE_STATIC);
92
        }
93
    }
94
95
    /**
96
     * Creates a unique cache key from the passed data.
97
     *
98
     * @param mixed   $data      The date to create the cache key from
99
     * @param boolean $usePrefix Flag to signal using the prefix or not
100
     *
101
     * @return string The generated cache key
102
     */
103
    public function cacheKey($data, $usePrefix = true)
104
    {
105
        return $this->cacheAdapter->cacheKey($data, $usePrefix);
106
    }
107
108
    /**
109
     * Inversion of the isCached() method.
110
     *
111
     * @param string $key The cache key to query for
112
     *
113
     * @return boolean TRUE if the value is not available, else FALSE
114
     */
115
    public function notCached($key)
116
    {
117
        return $this->cacheAdapter->notCached($key);
118
    }
119
120
    /**
121
     * Add's a cache reference from one key to another.
122
     *
123
     * @param string $from The key to reference from
124
     * @param string $to   The key to reference to
125
     *
126
     * @return void
127
     */
128
    public function addReference($from, $to)
129
    {
130
        $this->cacheAdapter->addReference($from, $to);
131
    }
132
133
    /**
134
     * Returns a new cache item for the passed key
135
     *
136
     * @param string $key The cache key to return the item for
137
     *
138
     * @return mixed The value for the passed key
139
     */
140
    public function fromCache($key)
141
    {
142
        return $this->cacheAdapter->fromCache($key);
143
    }
144
145
    /**
146
     * Flush the cache and remove the references.
147
     *
148
     * @return void
149
     */
150
    public function flushCache()
151
    {
152
        $this->cacheAdapter->flushCache();
153
    }
154
155
    /**
156
     * Invalidate the cache entries for the passed tags.
157
     *
158
     * @param array $tags The tags to invalidate the cache for
159
     *
160
     * @return void
161
     */
162
    public function invalidateTags(array $tags)
163
    {
164
        $this->cacheAdapter->invalidateTags($tags);
165
    }
166
167
    /**
168
     * Remove the item with the passed key and all its references from the cache.
169
     *
170
     * @param string $key               The key of the cache item to Remove
171
     * @param bool   $cleanUpReferences TRUE if the references has to be cleaned-up, else FALSE (default)
172
     *
173
     * @return void
174
     */
175
    public function removeCache($key, $cleanUpReferences = false)
176
    {
177
        $this->cacheAdapter->removeCache($key, $cleanUpReferences);
178
    }
179
180
    /**
181
     * Raises the value for the attribute with the passed key by one.
182
     *
183
     * @param mixed $key         The key of the attribute to raise the value for
184
     * @param mixed $counterName The name of the counter to raise
185
     *
186
     * @return integer The counter's new value
187
     */
188
    public function raiseCounter($key, $counterName)
189
    {
190
        return $this->cacheAdapter->raiseCounter($key, $counterName);
191
    }
192
193
    /**
194
     * This method merges the passed attributes with an array that
195
     * has already been added under the passed key.
196
     *
197
     * If no value will be found under the passed key, the attributes
198
     * will simply be registered.
199
     *
200
     * @param mixed $key        The key of the attributes that has to be merged with the passed ones
201
     * @param array $attributes The attributes that has to be merged with the exising ones
202
     *
203
     * @return void
204
     * @throws \Exception Is thrown, if the already registered value is no array
205
     * @link http://php.net/array_replace_recursive
206
     */
207
    public function mergeAttributesRecursive($key, array $attributes)
208
    {
209
        $this->cacheAdapter->mergeAttributesRecursive($key, $attributes);
210
    }
211
212
    /**
213
     * Query whether or not a cache value for the passed cache key is available.
214
     *
215
     * @param string $key The cache key to query for
216
     *
217
     * @return boolean TRUE if the a value is available, else FALSE
218
     */
219
    public function isCached($key)
220
    {
221
222
        // query whether or not the item has been cached, and if yes if the cache is valid
223
        if ($this->enabled) {
224
            return $this->cacheAdapter->isCached($key);
225
        }
226
227
        // return FALSE in all other cases
228
        return false;
229
    }
230
231
    /**
232
     * Add the passed item to the cache.
233
     *
234
     * @param string  $key        The cache key to use
235
     * @param mixed   $value      The value that has to be cached
236
     * @param array   $references An array with references to add
237
     * @param array   $tags       An array with tags to add
238
     * @param boolean $override   Flag that allows to override an exising cache entry
239
     * @param integer $time       The TTL in seconds for the passed item
240
     *
241
     * @return void
242
     */
243
    public function toCache($key, $value, array $references = array(), array $tags = array(), $override = true, $time = null)
244
    {
245
246
        // query whether or not the cache is enabled
247
        if ($this->enabled) {
248
            $this->cacheAdapter->toCache($key, $value, $references, array_merge($this->tags, $tags), $override, $time ? $time : $this->time);
249
        }
250
    }
251
}
252