Completed
Push — migrate-files-no-interaction ( 025687...608925 )
by
unknown
46:43 queued 18:48
created

CacheServiceDecorator::getItem()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 5
nop 0
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the CacheServiceDecorator class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Cache;
10
11
use Stash\Interfaces\PoolInterface;
12
13
/**
14
 * Class CacheServiceDecorator.
15
 *
16
 * Wraps the Cache Service for Spi cache to apply key prefix for the cache
17
 */
18
class CacheServiceDecorator
19
{
20
    const SPI_CACHE_KEY_PREFIX = 'ez_spi';
21
22
    /**
23
     * @var \Stash\Interfaces\PoolInterface
24
     */
25
    protected $cachePool;
26
27
    /**
28
     * Constructs the cache service decorator.
29
     *
30
     * @param \Stash\Interfaces\PoolInterface $cachePool
31
     */
32
    public function __construct(PoolInterface $cachePool)
33
    {
34
        $this->cachePool = $cachePool;
35
    }
36
37
    /**
38
     * Prepend key with prefix and support array format Stash supported before.
39
     *
40
     * {@see \Psr\Cache\CacheItemPoolInterface}
41
     *
42
     * @internal param array|string $key , $key, $key...
43
     *
44
     * @return \Stash\Interfaces\ItemInterface
45
     */
46
    public function getItem()
47
    {
48
        $args = func_get_args();
49
50
        if (empty($args)) {
51
            return $this->cachePool->getItem(self::SPI_CACHE_KEY_PREFIX);
52
        }
53
54
        //  Upstream seems to no longer support array, so we flatten it
55
        if (!isset($args[1]) && is_array($args[0])) {
56
            $key = implode('/', array_map([$this, 'washKey'], $args[0]));
57
        } else {
58
            $key = '' . implode('/', array_map([$this, 'washKey'], $args));
59
        }
60
61
        $key = $key === '' ? self::SPI_CACHE_KEY_PREFIX : self::SPI_CACHE_KEY_PREFIX . '/' . $key;
62
63
        return $this->cachePool->getItem($key);
64
    }
65
66
    /**
67
     * Prepend keys with prefix.
68
     *
69
     * {@see \Psr\Cache\CacheItemPoolInterface}
70
     *
71
     * @param array $keys
72
     * @return \Stash\Interfaces\ItemInterface[]
73
     */
74
    public function getItems(array $keys = array())
75
    {
76
        $prefix = self::SPI_CACHE_KEY_PREFIX;
77
        $keys = array_map(
78
            function ($key) use ($prefix) {
79
                $key = $this->washKey($key);
80
81
                return $key === '' ? $prefix : $prefix . '/' . $key;
82
            },
83
            $keys
84
        );
85
86
        return $this->cachePool->getItems($keys);
87
    }
88
89
    /**
90
     * Remove slashes from start and end of keys, and for content replace it with _ to avoid issues for Stash.
91
     *
92
     * @param string $key
93
     * @return string
94
     */
95
    private function washKey($key)
96
    {
97
        return str_replace('/', '_', trim($key, '/'));
98
    }
99
100
    /**
101
     * Clears the cache for the key, or if none is specified clears the entire cache. The key can be either
102
     * a series of string arguments, or an array.
103
     *
104
     * @internal param array|null|string $key , $key, $key...
105
     */
106
    public function clear()
107
    {
108
        $item = call_user_func_array(array($this, 'getItem'), func_get_args());
109
110
        return $item->clear();
111
    }
112
}
113