Completed
Push — master ( f2ed00...07b62e )
by Antonio Carlos
05:36
created

Cache::instantiateManager()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PragmaRX\Countries\Package\Services;
4
5
use Closure;
6
use Psr\SimpleCache\CacheInterface;
7
use PragmaRX\Countries\Package\Services\Cache\Managers\Nette as NetteManager;
8
9
class Cache implements CacheInterface
10
{
11
    /**
12
     * Cache.
13
     *
14
     * @var object
15
     */
16
    protected $manager;
17
18
    /**
19
     * Config.
20
     *
21
     * @var Config
22
     */
23
    protected $config;
24
25
    /**
26
     * Cache directory.
27
     *
28
     * @var string
29
     */
30
    protected $dir;
31
32
    /**
33
     * Cache constructor.
34
     * @param object $config
35
     * @param object $manager
36
     * @param null $path
37
     */
38 31
    public function __construct($config = null, $manager = null, $path = null)
39
    {
40 31
        $this->config = $this->instantiateConfig($config);
41
42 31
        $this->manager = $this->instantiateManager($this->config, $manager, $path);
43 31
    }
44
45
    /**
46
     * Instantiate the config.
47
     *
48
     * @param $config
49
     * @return Config|mixed
50
     */
51 31
    public function instantiateConfig($config)
52
    {
53 31
        return is_null($config) ? new Config() : $config;
54
    }
55
56
    /**
57
     * Instantiate the cache manager.
58
     *
59
     * @param $config
60
     * @param $manager
61
     * @param $path
62
     * @return NetteManager|mixed
63
     */
64 31
    public function instantiateManager($config, $manager, $path)
65
    {
66 31
        return is_null($manager)
67 31
            ? new NetteManager($config, $path)
68 31
            : $manager;
69
    }
70
71
    /**
72
     * Check if cache is enabled.
73
     *
74
     * @return bool
75
     */
76 31
    protected function enabled()
77
    {
78 31
        return $this->config->get('countries.cache.enabled');
79
    }
80
81
    /**
82
     * Fetches a value from the cache.
83
     *
84
     * @param string $key
85
     * @param null $default
86
     * @return mixed|null
87
     */
88 31
    public function get($key, $default = null)
89
    {
90 31
        if ($this->enabled()) {
91
            return $this->manager->get($key, $default);
92
        }
93
94 31
        return null;
95
    }
96
97
    /**
98
     * Create a cache key.
99
     *
100
     * @return string
101
     * @throws Exception
102
     */
103 31
    public function makeKey()
104
    {
105 31
        $arguments = func_get_args();
106
107 31
        if (empty($arguments)) {
108
            throw new Exception('Empty key');
109
        }
110
111 31
        return base64_encode(serialize($arguments));
112
    }
113
114
    /**
115
     * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
116
     *
117
     * @param string $key
118
     * @param mixed $value
119
     * @param null $ttl
120
     * @return bool
121
     */
122 31
    public function set($key, $value, $ttl = null)
123
    {
124 31
        if ($this->enabled()) {
125
            return $this->manager->set($key, $value, $ttl);
126
        }
127
128 31
        return $value;
129
    }
130
131
    /**
132
     * Delete an item from the cache by its unique key.
133
     *
134
     * @param string $key
135
     * @return bool
136
     */
137
    public function delete($key)
138
    {
139
        $this->manager->delete($key);
140
    }
141
142
    /**
143
     * Wipe clean the entire cache's keys.
144
     */
145 31
    public function clear()
146
    {
147 31
        $this->manager->clear();
148 31
    }
149
150
    /**
151
     * Obtains multiple cache items by their unique keys.
152
     *
153
     * @param $keys
154
     * @param null $default
155
     * @return array
156
     */
157
    public function getMultiple($keys, $default = null)
158
    {
159
        return $this->manager->getMultiple($keys, $default);
160
    }
161
162
    /**
163
     * Persists a set of key => value pairs in the cache, with an optional TTL.
164
     *
165
     * @param $values
166
     * @param null $ttl
167
     * @return bool
168
     */
169
    public function setMultiple($values, $ttl = null)
170
    {
171
        return $this->manager->setMultiple($keys, $ttl);
0 ignored issues
show
Bug introduced by
The variable $keys does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
172
    }
173
174
    /**
175
     * Deletes multiple cache items in a single operation.
176
     *
177
     * @param $keys
178
     * @return bool|void
179
     */
180
    public function deleteMultiple($keys)
181
    {
182
        $this->manager->deleteMultiple($keys);
183
    }
184
185
    /**
186
     * Determines whether an item is present in the cache.
187
     *
188
     * @param string $key
189
     * @return bool
190
     */
191
    public function has($key)
192
    {
193
        return $this->manager->has($key);
194
    }
195
196
    /**
197
     * Get an item from the cache, or store the default value.
198
     *
199
     * @param  string $key
200
     * @param  \DateTimeInterface|\DateInterval|float|int $minutes
201
     * @param Closure $callback
202
     * @return mixed
203
     */
204 View Code Duplication
    public function remember($key, $minutes, Closure $callback)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
205
    {
206
        if (! is_null($value = $this->manager->get($key))) {
207
            return $value;
208
        }
209
210
        $this->manager->set($key, $value = $callback(), $minutes);
211
212
        return $value;
213
    }
214
}
215