Passed
Push — master ( 0f35d9...62011e )
by Tim
03:47
created

ConfigurableCacheAdapter::addReference()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * TechDivision\Import\Cache\ConfigurableCacheAdapter
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 2021 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-cache
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Cache;
22
23
use TechDivision\Import\Cache\Utils\CacheTypes;
24
use TechDivision\Import\Cache\Configuration\ConfigurationInterface;
25
26
/**
27
 * Configurable cache adapter implementation.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2021 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-cache
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\Cache\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 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
     * Lowers the value for the attribute with the passed key by one.
195
     *
196
     * @param mixed $key         The key of the attribute to lower the value for
197
     * @param mixed $counterName The name of the counter to lower
198
     *
199
     * @return integer The counter's new value
200
     */
201
    public function lowerCounter($key, $counterName)
202
    {
203
        return $this->cacheAdapter->lowerCounter($key, $counterName);
204
    }
205
206
    /**
207
     * This method merges the passed attributes with an array that
208
     * has already been added under the passed key.
209
     *
210
     * If no value will be found under the passed key, the attributes
211
     * will simply be registered.
212
     *
213
     * @param mixed $key        The key of the attributes that has to be merged with the passed ones
214
     * @param array $attributes The attributes that has to be merged with the exising ones
215
     *
216
     * @return void
217
     * @throws \Exception Is thrown, if the already registered value is no array
218
     * @link http://php.net/array_replace_recursive
219
     */
220
    public function mergeAttributesRecursive($key, array $attributes)
221
    {
222
        $this->cacheAdapter->mergeAttributesRecursive($key, $attributes);
223
    }
224
225
    /**
226
     * Query whether or not a cache value for the passed cache key is available.
227
     *
228
     * @param string $key The cache key to query for
229
     *
230
     * @return boolean TRUE if the a value is available, else FALSE
231
     */
232
    public function isCached($key)
233
    {
234
235
        // query whether or not the item has been cached, and if yes if the cache is valid
236
        if ($this->enabled) {
237
            return $this->cacheAdapter->isCached($key);
238
        }
239
240
        // return FALSE in all other cases
241
        return false;
242
    }
243
244
    /**
245
     * Add the passed item to the cache.
246
     *
247
     * @param string  $key        The cache key to use
248
     * @param mixed   $value      The value that has to be cached
249
     * @param array   $references An array with references to add
250
     * @param array   $tags       An array with tags to add
251
     * @param boolean $override   Flag that allows to override an exising cache entry
252
     * @param integer $time       The TTL in seconds for the passed item
253
     *
254
     * @return void
255
     */
256
    public function toCache($key, $value, array $references = array(), array $tags = array(), $override = true, $time = null)
257
    {
258
259
        // query whether or not the cache is enabled
260
        if ($this->enabled) {
261
            $this->cacheAdapter->toCache($key, $value, $references, array_merge($this->tags, $tags), $override, $time ? $time : $this->time);
0 ignored issues
show
Bug introduced by
$this->tags of type string is incompatible with the type array expected by parameter $array1 of array_merge(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

261
            $this->cacheAdapter->toCache($key, $value, $references, array_merge(/** @scrutinizer ignore-type */ $this->tags, $tags), $override, $time ? $time : $this->time);
Loading history...
262
        }
263
    }
264
}
265