Completed
Push — master ( 028b29...cb8c8c )
by Antonio Carlos
07:08
created

Service::instantiateConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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