Completed
Push — master ( 98de1c...0ea9e7 )
by Anton
10s
created

Cache   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 22.22%

Importance

Changes 0
Metric Value
dl 0
loc 125
ccs 8
cts 36
cp 0.2222
rs 10
c 0
b 0
f 0
wmc 19
lcom 2
cbo 5

6 Methods

Rating   Name   Duplication   Size   Complexity  
A initInstance() 0 5 1
B getAdapter() 0 16 7
A get() 0 14 4
A set() 0 19 4
A prepare() 0 4 1
A clearTags() 0 7 2
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Proxy;
12
13
use Bluz\Common\Exception\ComponentException;
14
use Cache\Hierarchy\HierarchicalPoolInterface;
15
use Cache\Taggable\TaggablePoolInterface as Instance;
16
17
/**
18
 * Proxy to Cache
19
 *
20
 * Example of usage
21
 *     use Bluz\Proxy\Cache;
22
 *
23
 *     if (!$result = Cache::get('some unique id')) {
24
 *          $result = 2*2;
25
 *          Cache::set('some unique id', $result);
26
 *     }
27
 *
28
 * @package  Bluz\Proxy
29
 * @author   Anton Shevchuk
30
 *
31
 * @method   static Instance|false getInstance()
32
 *
33
 * @method   static bool delete($key)
34
 * @see      CacheItemPoolInterface::deleteItem()
35
 *
36
 * @method   static bool clear()
37
 * @see      CacheItemPoolInterface::clear()
38
 */
39
class Cache
40
{
41
    use ProxyTrait;
42
43
    /**
44
     * No expiry TTL value
45
     */
46
    const TTL_NO_EXPIRY = 0;
47
48
    /**
49
     * @var array
50
     */
51
    protected static $pools = [];
52
53
    /**
54
     * Init cache instance
55
     *
56
     * @return Instance|false
57
     * @throws ComponentException
58
     */
59
    protected static function initInstance()
60
    {
61
        $adapter = Config::getData('cache', 'adapter');
62
        return self::getAdapter($adapter);
63
    }
64
65
    /**
66
     * Get Cache Adapter
67
     *
68
     * @param string $adapter
69
     * @return Instance|false
70
     * @throws ComponentException
71
     */
72
    public static function getAdapter($adapter)
73
    {
74
        $config = Config::getData('cache');
75
76
        if ($config && $adapter && isset($config['enabled']) && $config['enabled']) {
77
            if (!isset($config['pools'][$adapter])) {
78
                throw new ComponentException("Class `Proxy\\Cache` required configuration for `$adapter` adapter");
79
            } else {
80
                if (!isset(static::$pools[$adapter])) {
81
                    static::$pools[$adapter] = $config['pools'][$adapter]();
82
                }
83
                return static::$pools[$adapter];
84
            }
85
        }
86
        return false;
87
    }
88
89
    /**
90
     * Get value of cache item
91
     *
92
     * @param string $key
93
     * @return mixed
94
     */
95 639
    public static function get($key)
96
    {
97 639
        if (!$cache = self::getInstance()) {
98 639
            return false;
99
        }
100
101
        if ($cache->hasItem($key)) {
102
            $item = $cache->getItem($key);
103
            if ($item->isHit()) {
104
                return $item->get();
105
            }
106
        }
107
        return false;
108
    }
109
110
    /**
111
     * Set value of cache item
112
     *
113
     * @param string $key
114
     * @param mixed $data
115
     * @param int $ttl
116
     * @param array $tags
117
     * @return bool
118
     */
119 639
    public static function set($key, $data, $ttl = self::TTL_NO_EXPIRY, $tags = [])
120
    {
121 639
        if (!$cache = self::getInstance()) {
122 639
            return false;
123
        }
124
125
        $item = $cache->getItem($key);
126
        $item->set($data);
127
128
        if (self::TTL_NO_EXPIRY !== $ttl) {
129
            $item->expiresAfter($ttl);
130
        }
131
132
        if (!empty($tags)) {
133
            $item->setTags($tags);
134
        }
135
136
        return $cache->save($item);
137
    }
138
139
    /**
140
     * Prepare key
141
     *
142
     * @return string
143
     */
144 639
    public static function prepare($key)
145
    {
146 639
        return str_replace(['-', '/'], '_', $key);
147
    }
148
149
    /**
150
     * Clear cache items by tags
151
     *
152
     * @see    TaggablePoolInterface::clearTags()
153
     *
154
     * @return bool
155
     */
156
    public static function clearTags(array $tags)
157
    {
158
        if (self::getInstance() instanceof HierarchicalPoolInterface) {
159
            return self::getInstance()->clearTags($tags);
160
        }
161
        return false;
162
    }
163
}
164