Completed
Push — ezp26001-permissions_lookup_re... ( 2e76c0...5b8460 )
by
unknown
25:19
created

CacheServiceDecorator::getItem()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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