Completed
Push — 11.x ( 480f94...52ec97 )
by Tim
15s
created

GenericCacheAdapter::raiseCounter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 10
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
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 Psr\Cache\CacheItemPoolInterface;
24
use TechDivision\Import\Utils\CacheKeys;
25
use TechDivision\Import\Utils\CacheKeyUtilInterface;
26
27
/**
28
 * Generic cache adapter that wrappes any PSR-6 compatible cache implementation and can be
29
 * used in a distributed environment.
30
 *
31
 * If you're searching for a maximum performance consider using the LocalCacheAdapter
32
 * implementation.
33
 *
34
 * ATTENTION: Please be aware, that this cache adapter is NOT multiprocess or -threadsafe!
35
 *
36
 * @author    Tim Wagner <[email protected]>
37
 * @copyright 2019 TechDivision GmbH <[email protected]>
38
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
39
 * @link      https://github.com/techdivision/import
40
 * @link      http://www.techdivision.com
41
 * @see       \TechDivision\Import\Cache\LocalCacheAdapter
42
 */
43
class GenericCacheAdapter implements CacheAdapterInterface
44
{
45
46
    /**
47
     * Trait that provides custom cache adapter functionality.
48
     *
49
     * @var TechDivision\Import\Cache\CacheAdapterTrait
50
     */
51
    use CacheAdapterTrait;
52
53
    /**
54
     * The cache for the query results.
55
     *
56
     * @var \Psr\Cache\CacheItemPoolInterface
57
     */
58
    protected $cache;
59
60
    /**
61
     * The cache key utility instance.
62
     *
63
     * @var \TechDivision\Import\Utils\CacheKeyUtilInterface
64
     */
65
    protected $cacheKeyUtil;
66
67
    /**
68
     * Initialize the cache handler with the passed cache and configuration instances.
69
     * .
70
     * @param \Psr\Cache\CacheItemPoolInterface                $cache        The cache instance
71
     * @param \TechDivision\Import\Utils\CacheKeyUtilInterface $cacheKeyUtil The cache key utility instance
72
     */
73
    public function __construct(
74
        CacheItemPoolInterface $cache,
75
        CacheKeyUtilInterface $cacheKeyUtil
76
    ) {
77
78
        // set the cache and the cache key utility instance
79
        $this->cache = $cache;
80
        $this->cacheKeyUtil = $cacheKeyUtil;
81
    }
82
83
    /**
84
     * Resolve's the cache key.
85
     *
86
     * @param string $from The cache key to resolve
87
     *
88
     * @return string The resolved reference
89
     */
90
    protected function resolveReference($from)
91
    {
92
93
        // try to load the load the references
94
        if ($this->cache->hasItem(CacheKeys::REFERENCES)) {
95
            // load the array with references from the cache
96
            $references = $this->cache->getItem(CacheKeys::REFERENCES)->get();
97
98
            // query whether a reference is available
99
            if (isset($references[$from])) {
100
                return $references[$from];
101
            }
102
        }
103
104
        // return the passed reference
105
        return $from;
106
    }
107
108
    /**
109
     * Creates a unique cache key from the passed data.
110
     *
111
     * @param mixed $data The date to create the cache key from
112
     *
113
     * @return string The generated cache key
114
     */
115
    public function cacheKey($data)
116
    {
117
        return $this->cacheKeyUtil->cacheKey($data);
118
    }
119
120
    /**
121
     * Query whether or not a cache value for the passed cache key is available.
122
     *
123
     * @param string $key The cache key to query for
124
     *
125
     * @return boolean TRUE if the a value is available, else FALSE
126
     */
127
    public function isCached($key)
128
    {
129
130
        // query whether or not the item has been cached, and if yes if the cache is valid
131
        if ($this->cache->hasItem($resolvedKey = $this->resolveReference($this->cacheKey($key)))) {
132
            return $this->cache->getItem($resolvedKey)->isHit();
133
        }
134
135
        // return FALSE in all other cases
136
        return false;
137
    }
138
139
    /**
140
     * Inversion of the isCached() method.
141
     *
142
     * @param string $key The cache key to query for
143
     *
144
     * @return boolean TRUE if the value is not available, else FALSE
145
     */
146
    public function notCached($key)
147
    {
148
        return !$this->isCached($key);
149
    }
150
151
    /**
152
     * Add's a cache reference from one key to another.
153
     *
154
     * @param string $from The key to reference from
155
     * @param string $to   The key to reference to
156
     *
157
     * @return void
158
     */
159
    public function addReference($from, $to)
160
    {
161
162
        // initialize the array with references
163
        $references = array();
164
165
        // try to load the references from the cache
166
        if ($this->isCached(CacheKeys::REFERENCES)) {
167
            $references = $this->fromCache(CacheKeys::REFERENCES);
168
        }
169
170
        // add the reference to the array
171
        $references[$this->cacheKey($from)] = $this->cacheKey($to);
172
173
        // add the references back to the cache
174
        $this->toCache(CacheKeys::REFERENCES, $references);
175
    }
176
177
    /**
178
     * Add the passed item to the cache.
179
     *
180
     * @param string  $key        The cache key to use
181
     * @param mixed   $value      The value that has to be cached
182
     * @param array   $references An array with references to add
183
     * @param array   $tags       An array with tags to add
184
     * @param boolean $override   Flag that allows to override an exising cache entry
185
     * @param integer $time       The TTL in seconds for the passed item
186
     *
187
     * @return void
188
     */
189
    public function toCache($key, $value, array $references = array(), array $tags = array(), $override = false, $time = null)
190
    {
191
192
        // create the unique cache key
193
        $uniqueKey = $this->cacheKey($key);
194
195
        // query whether or not the key has already been used
196
        if ($this->isCached($uniqueKey) && $override === false) {
197
            throw new \Exception(
198
                sprintf(
199
                    'Try to override data with key "%s"',
200
                    $uniqueKey
201
                )
202
            );
203
        }
204
205
        // prepend the tags with the cache key
206
        array_walk($tags, function (&$tag) {
207
            $tag = $this->cacheKey($tag);
208
        });
209
210
        // initialize the cache item
211
        $cacheItem = $this->cache->getItem($uniqueKey);
212
        $cacheItem->set($value)->expiresAfter($time)->setTags($tags);
213
214
        // set the attribute in the registry
215
        $this->cache->save($cacheItem);
216
217
        // also register the references if given
218
        foreach ($references as $from => $to) {
219
            $this->addReference($from, $to);
220
        }
221
    }
222
223
    /**
224
     * Returns a new cache item for the passed key
225
     *
226
     * @param string $key The cache key to return the item for
227
     *
228
     * @return mixed The value for the passed key
229
     */
230
    public function fromCache($key)
231
    {
232
        return $this->cache->getItem($this->resolveReference($this->cacheKey($key)))->get();
233
    }
234
235
    /**
236
     * Flush the cache and remove the references.
237
     *
238
     * @return void
239
     */
240
    public function flushCache()
241
    {
242
        $this->cache->clear();
243
    }
244
245
    /**
246
     * Invalidate the cache entries for the passed tags.
247
     *
248
     * @param array $tags The tags to invalidate the cache for
249
     *
250
     * @return void
251
     */
252
    public function invalidateTags(array $tags)
253
    {
254
255
        // prepend the tags with the cache key
256
        array_walk($tags, function (&$tag) {
257
            $tag = $this->cacheKey($tag);
258
        });
259
260
        // query whether or not references are available
261
        if ($this->isCached(CacheKeys::REFERENCES)) {
262
            // load the array with references from the cache
263
            $references = $this->fromCache(CacheKeys::REFERENCES);
264
265
            // remove all the references of items that has one of the passed tags
266
            foreach ($tags as $tag) {
267
                foreach ($references as $from => $to) {
268
                    // load the cache item for the referenced key
269
                    $cacheItem = $this->cache->getItem($to);
270
                    // query whether or not the cache item has the tag, if yes remove the reference
271
                    if (in_array($tag, $cacheItem->getPreviousTags())) {
272
                        unset($references[$from]);
273
                    }
274
                }
275
            }
276
277
            // query whether or not the references exists
278
            if (sizeof($references) > 0) {
279
                // set the array with references to the cache
280
                $this->toCache(CacheKeys::REFERENCES, $references);
281
            } else {
282
                $this->removeCache(CacheKeys::REFERENCES);
283
            }
284
        }
285
286
        // finally, invalidate the items with the passed tags
287
        $this->cache->invalidateTags($tags);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Cache\CacheItemPoolInterface as the method invalidateTags() does only exist in the following implementations of said interface: Cache\Adapter\Common\AbstractCachePool, Cache\Adapter\PHPArray\ArrayCachePool.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
288
    }
289
290
    /**
291
     * Remove the item with the passed key and all its references from the cache.
292
     *
293
     * @param string $key The key of the cache item to Remove
294
     *
295
     * @return void
296
     */
297
    public function removeCache($key)
298
    {
299
300
        // delete the item with the passed key
301
        $this->cache->deleteItem($this->resolveReference($uniqueKey = $this->cacheKey($key)));
302
303
        // query whether or not references are available
304
        if ($this->isCached(CacheKeys::REFERENCES)) {
305
            // load the array with references from the cache
306
            $references = $this->fromCache(CacheKeys::REFERENCES);
307
            // query whether or not the references exists
308
            if (isset($references[$uniqueKey])) {
309
                // remove the reference
310
                unset($references[$uniqueKey]);
311
                // set the array with references to the cache
312
                $this->toCache(CacheKeys::REFERENCES, $references);
313
            }
314
        }
315
    }
316
}
317