Completed
Push — master ( 944c9c...280a9a )
by Antonio Carlos
03:20 queued 10s
created

Nette::deleteMultiple()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 2
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 Psr\SimpleCache\CacheInterface;
7
use Nette\Caching\Cache as NetteCache;
8
use Nette\Caching\Storages\FileStorage;
9
use PragmaRX\Countries\Package\Services\Config;
10
11
class Nette implements CacheInterface
12
{
13
    /**
14
     * Cache.
15
     *
16
     * @var \Nette\Caching\Cache
17
     */
18
    protected $cache;
19
20
    /**
21
     * Config.
22
     *
23
     * @var Config
24
     */
25
    protected $config;
26
27
    /**
28
     * Cache directory.
29
     *
30
     * @var string
31
     */
32
    protected $dir;
33
34
    /**
35
     * Cache constructor.
36
     * @param object $config
37
     * @param null $path
38
     */
39
    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...
40
    {
41
        $this->config = \is_null($config) ? new Config() : $config;
42
        $this->cache = new NetteCache($this->getStorage());
43
    }
44
45
    /**
46
     * Check if cache is enabled.
47
     *
48
     * @return bool
49
     */
50
    protected function enabled()
51
    {
52
        return $this->config->get('countries.cache.enabled');
53
    }
54
55
    /**
56
     * Get the cache directory.
57
     *
58
     * @return mixed|string|static
59
     */
60
    public function getCacheDir()
61
    {
62
        if (\is_null($this->dir)) {
63
            $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...
64
65
            if (! file_exists($this->dir)) {
66
                mkdir($this->dir, 0755, true);
67
            }
68
        }
69
70
        return $this->dir;
71
    }
72
73
    /**
74
     * Get the file storage.
75
     *
76
     * @param null $path
77
     * @return FileStorage
78
     */
79
    public function getStorage($path = null)
80
    {
81
        return new FileStorage(
82
            \is_null($path)
83
                ? $this->getCacheDir()
84
                : $path
85
        );
86
    }
87
88
    /**
89
     * Fetches a value from the cache.
90
     *
91
     * @param string $key
92
     * @param null $default
93
     * @return mixed
94
     */
95
    public function get($key, $default = null)
96
    {
97
        if ($this->enabled()) {
98
            return $this->cache->load($key, $default);
99
        }
100
    }
101
102
    /**
103
     * @param $ttl
104
     * @return string
105
     */
106
    protected function makeExpiration($ttl)
107
    {
108
        return ($ttl ?: $this->config->get('cache.duration')).' minutes';
109
    }
110
111
    /**
112
     * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
113
     *
114
     * @param string $key
115
     * @param mixed $value
116
     * @param null $ttl
117
     * @return bool
118
     */
119
    public function set($key, $value, $ttl = null)
120
    {
121
        if ($this->enabled()) {
122
            return $this->cache->save($key, $value, [NetteCache::EXPIRE => $this->makeExpiration($ttl)]);
123
        }
124
125
        return $value;
126
    }
127
128
    /**
129
     * Delete an item from the cache by its unique key.
130
     *
131
     * @param string $key
132
     * @return bool
133
     */
134
    public function delete($key)
135
    {
136
        $this->cache->remove($key);
137
    }
138
139
    /**
140
     * Wipe clean the entire cache's keys.
141
     */
142
    public function clear()
143
    {
144
        $this->cache->clean([NetteCache::ALL => true]);
145
    }
146
147
    /**
148
     * Obtains multiple cache items by their unique keys.
149
     *
150
     * @param $keys
151
     * @param null $default
152
     * @return array
153
     */
154
    public function getMultiple($keys, $default = null)
155
    {
156
        return coollect($keys)->map(function ($key) {
157
            return $this->get($key);
158
        });
159
    }
160
161
    /**
162
     * Persists a set of key => value pairs in the cache, with an optional TTL.
163
     *
164
     * @param $values
165
     * @param null $ttl
166
     * @return bool
167
     */
168
    public function setMultiple($values, $ttl = null)
169
    {
170
        return coollect($values)->map(function ($value, $key) use ($ttl) {
171
            return $this->set($key, $value, $ttl);
172
        });
173
    }
174
175
    /**
176
     * Deletes multiple cache items in a single operation.
177
     *
178
     * @param $keys
179
     * @return bool|void
180
     */
181
    public function deleteMultiple($keys)
182
    {
183
        coollect($keys)->map(function ($key) {
184
            $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...
185
        });
186
    }
187
188
    /**
189
     * Determines whether an item is present in the cache.
190
     *
191
     * @param string $key
192
     * @return bool
193
     */
194
    public function has($key)
195
    {
196
        return ! \is_null($this->get($key));
197
    }
198
199
    /**
200
     * Get an item from the cache, or store the default value.
201
     *
202
     * @param  string $key
203
     * @param  \DateTimeInterface|\DateInterval|float|int $minutes
204
     * @param Closure $callback
205
     * @return mixed
206
     */
207 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...
208
    {
209
        $value = $this->get($key);
210
211
        if (! \is_null($value)) {
212
            return $value;
213
        }
214
215
        $this->set($key, $value = $callback(), $minutes);
216
217
        return $value;
218
    }
219
}
220