Completed
Push — master ( 36441b...3990ec )
by Anton
11s
created

Cache::__callStatic()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 11
ccs 0
cts 5
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link https://github.com/bluzphp/framework
7
 */
8
9
/**
10
 * @namespace
11
 */
12
namespace Bluz\Proxy;
13
14
use Bluz\Common\Exception\ComponentException;
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
 * @method   static bool clearTags(array $tags)
40
 * @see      TaggablePoolInterface::clearTags()
41
 */
42
class Cache
43
{
44
    use ProxyTrait;
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 627
    public static function get($key)
96
    {
97 627
        if (!$cache = self::getInstance()) {
98 627
            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 627
    public static function set($key, $data, $ttl = self::TTL_NO_EXPIRY, $tags = [])
120
    {
121 627
        if (!$cache = self::getInstance()) {
122 627
            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 627
    public static function prepare($key)
145
    {
146 627
        return str_replace(['-', '/'], '_', $key);
147
    }
148
}
149