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

Nette::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PragmaRX\Countries\Package\Services\Cache\Managers;
4
5
use Closure;
6
use Exception;
7
use Psr\SimpleCache\CacheInterface;
8
use Nette\Caching\Cache as NetteCache;
9
use Nette\Caching\Storages\FileStorage;
10
use PragmaRX\Countries\Package\Services\Config;
11
12
class Nette implements CacheInterface
13
{
14
    /**
15
     * Cache.
16
     *
17
     * @var \Nette\Caching\Cache
18
     */
19
    protected $cache;
20
21
    /**
22
     * Config.
23
     *
24
     * @var Config
25
     */
26
    protected $config;
27
28
    /**
29
     * Cache directory.
30
     *
31
     * @var string
32
     */
33
    protected $dir;
34
35
    /**
36
     * Cache constructor.
37
     * @param object $config
38
     * @param null $path
39
     */
40 31
    public function __construct($config = null, $path = null)
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42 31
        $this->config = is_null($config) ? new Config() : $config;
43
44 31
        $this->cache = new NetteCache(
45 31
            $this->getStorage($path = null)
46
        );
47 31
    }
48
49
    /**
50
     * Check if cache is enabled.
51
     *
52
     * @return bool
53
     */
54
    protected function enabled()
55
    {
56
        return $this->config->get('countries.cache.enabled');
57
    }
58
59
    /**
60
     * Get the cache directory.
61
     *
62
     * @return mixed|string|static
63
     */
64 31
    public function getCacheDir()
65
    {
66 31
        if (is_null($this->dir)) {
67 31
            $this->dir = $this->config->cache->directory ?: sys_get_temp_dir().'/__PRAGMARX_COUNTRIES__/cache';
0 ignored issues
show
Documentation introduced by
The property cache does not exist on object<PragmaRX\Countrie...ackage\Services\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
68
69 31
            if (! file_exists($this->dir)) {
70 1
                mkdir($this->dir, 0755, true);
71
            }
72
        }
73
74 31
        return $this->dir;
75
    }
76
77
    /**
78
     * Get the file storage.
79
     *
80
     * @param null $path
81
     * @return FileStorage
82
     */
83 31
    public function getStorage($path = null)
84
    {
85 31
        return new FileStorage(
86 31
            is_null($path)
87 31
                ? $this->getCacheDir()
88 31
                : $path
89
        );
90
    }
91
92
    /**
93
     * Fetches a value from the cache.
94
     *
95
     * @param string $key
96
     * @param null $default
97
     * @return mixed
98
     */
99
    public function get($key, $default = null)
100
    {
101
        if ($this->enabled()) {
102
            return $this->cache->load($key, $default);
103
        }
104
    }
105
106
    /**
107
     * @param $ttl
108
     * @return string
109
     */
110
    protected function makeExpiration($ttl)
111
    {
112
        $expiration = ($ttl ?: $this->config->get('cache.duration')).' minutes';
113
114
        return $expiration;
115
    }
116
117
    /**
118
     * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
119
     *
120
     * @param string $key
121
     * @param mixed $value
122
     * @param null $ttl
123
     * @return bool
124
     */
125
    public function set($key, $value, $ttl = null)
126
    {
127
        if ($this->enabled()) {
128
            return $this->cache->save($key, $value, [NetteCache::EXPIRE => $this->makeExpiration($ttl)]);
129
        }
130
131
        return $value;
132
    }
133
134
    /**
135
     * Delete an item from the cache by its unique key.
136
     *
137
     * @param string $key
138
     * @return bool
139
     */
140
    public function delete($key)
141
    {
142
        $this->cache->remove($key);
143
    }
144
145
    /**
146
     * Wipe clean the entire cache's keys.
147
     */
148 31
    public function clear()
149
    {
150 31
        $this->cache->clean([NetteCache::ALL => true]);
151 31
    }
152
153
    /**
154
     * Obtains multiple cache items by their unique keys.
155
     *
156
     * @param $keys
157
     * @param null $default
158
     * @return array
159
     */
160
    public function getMultiple($keys, $default = null)
161
    {
162
        return coollect($keys)->map(function ($key) {
163
            return $this->get($key);
164
        });
165
    }
166
167
    /**
168
     * Persists a set of key => value pairs in the cache, with an optional TTL.
169
     *
170
     * @param $values
171
     * @param null $ttl
172
     * @return bool
173
     */
174
    public function setMultiple($values, $ttl = null)
175
    {
176
        return coollect($values)->map(function ($value, $key) use ($ttl) {
177
            return $this->set($key, $value, $ttl);
178
        });
179
    }
180
181
    /**
182
     * Deletes multiple cache items in a single operation.
183
     *
184
     * @param $keys
185
     * @return bool|void
186
     */
187
    public function deleteMultiple($keys)
188
    {
189
        coollect($keys)->map(function ($key) {
190
            $this->forget($key);
0 ignored issues
show
Bug introduced by
The method forget() does not seem to exist on object<PragmaRX\Countrie...s\Cache\Managers\Nette>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
191
        });
192
    }
193
194
    /**
195
     * Determines whether an item is present in the cache.
196
     *
197
     * @param string $key
198
     * @return bool
199
     */
200
    public function has($key)
201
    {
202
        return ! is_null($this->get($key));
203
    }
204
205
    /**
206
     * Get an item from the cache, or store the default value.
207
     *
208
     * @param  string $key
209
     * @param  \DateTimeInterface|\DateInterval|float|int $minutes
210
     * @param Closure $callback
211
     * @return mixed
212
     */
213 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...
214
    {
215
        $value = $this->get($key);
216
217
        if (! is_null($value)) {
218
            return $value;
219
        }
220
221
        $this->set($key, $value = $callback(), $minutes);
222
223
        return $value;
224
    }
225
}
226