Completed
Push — 2.0 ( b22aa0...07e083 )
by Marco
11:24 queued 07:13
created

AbstractEnhancedProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php namespace Comodojo\Cache\Providers;
2
3
use \Comodojo\Cache\Traits\StatefulTrait;
4
use \Comodojo\Cache\Traits\NamespaceTrait;
5
use \Comodojo\Cache\Interfaces\EnhancedCacheItemPoolInterface;
6
use \Comodojo\Cache\Item;
7
use \Comodojo\Cache\Components\KeyValidator;
8
use \Comodojo\Foundation\Utils\UniqueId;
9
use \Comodojo\Foundation\Utils\ClassProperties;
10
use \Psr\Log\LoggerInterface;
11
use \Psr\Cache\CacheItemInterface;
12
use \DateTime;
13
use \Comodojo\Exception\CacheException;
14
use \Comodojo\Exception\InvalidCacheArgumentException;
15
use \Exception;
16
17
/**
18
 * @package     Comodojo Cache
19
 * @author      Marco Giovinazzi <[email protected]>
20
 * @license     MIT
21
 *
22
 * LICENSE:
23
 *
24
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30
 * THE SOFTWARE.
31
 */
32
33
abstract class AbstractEnhancedProvider
34
    extends AbstractProvider
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "AbstractProvider" and comma; 1 found
Loading history...
35
    implements EnhancedCacheItemPoolInterface {
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
36
37
    use StatefulTrait;
38
    use NamespaceTrait;
39
40
    protected $driver;
41
42
    protected $default_properties = [];
43
44
    protected $properties;
45
46
    private $queue = [];
47
48 211 View Code Duplication
    public function __construct(array $properties = [], LoggerInterface $logger = null) {
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...
49
50 211
        parent::__construct($logger);
51
52 211
        $this->properties = ClassProperties::create($this->default_properties)->merge($properties);
53
54 211
        $this->setId(UniqueId::generate(64));
55
56 211
    }
57
58
    abstract public function getStats();
59
60 82
    public function getProperties() {
61
62 82
        return $this->properties;
63
64
    }
65
66 176
    public function getItem($key) {
67
68 176
        if ( KeyValidator::validateKey($key) === false ) {
69
            throw new InvalidCacheArgumentException('Invalid key provided');
70
        }
71
72
        try {
73
74 176
            $data = $this->driver->get($key, $this->getNamespace());
75
76
        } catch (Exception $e) {
77
78
            $this->setState(self::CACHE_ERROR, $e->getMessage());
79
            $data = null;
80
81
        }
82
83 176
        if ( $data === null ) return new Item($key);
84
85 93
        $item = new Item($key, true);
86
87 93
        return $item->set(unserialize($data));
88
89
    }
90
91 42 View Code Duplication
    public function hasItem($key) {
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...
92
93 42
        if ( KeyValidator::validateKey($key) === false ) {
94
            throw new InvalidCacheArgumentException('Invalid key provided');
95
        }
96
97
        try {
98
99 42
            $data = $this->driver->has($key, $this->getNamespace());
0 ignored issues
show
Documentation introduced by
$key is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
100
101
        } catch (Exception $e) {
102
103
            $this->setState(self::CACHE_ERROR, $e->getMessage());
104
            $data = false;
105
106
        }
107
108 42
        return $data;
109
110
    }
111
112 57
    public function clear() {
113
114
        try {
115
116 57
            $data = $this->driver->clear();
117
118
        } catch (Exception $e) {
119
120
            $this->setState(self::CACHE_ERROR, $e->getMessage());
121
            $data = false;
122
123
        }
124
125 57
        return $data;
126
127
    }
128
129 8 View Code Duplication
    public function clearNamespace() {
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...
130
131
        try {
132
133 8
            $data = $this->driver->clear($this->getNamespace());
134
135
        } catch (Exception $e) {
136
137
            $this->setState(self::CACHE_ERROR, $e->getMessage());
138
            $data = false;
139
140
        }
141
142 8
        return $data;
143
144
    }
145
146 10 View Code Duplication
    public function deleteItem($key) {
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...
147
148 10
        if ( KeyValidator::validateKey($key) === false ) {
149
            throw new InvalidCacheArgumentException('Invalid key provided');
150
        }
151
152
        try {
153
154 10
            $data = $this->driver->delete($key, $this->getNamespace());
155
156
        } catch (Exception $e) {
157
158
            $this->setState(self::CACHE_ERROR, $e->getMessage());
159
            $data = false;
160
161
        }
162
163 10
        return $data;
164
165
    }
166
167 169
    public function save(CacheItemInterface $item) {
168
169 169
        $ttl = $item->getTtl();
170
171 169
        if ( $ttl < 0 ) return false;
172
173
        try {
174
175 120
            $data = $this->driver->set($item->getKey(), $this->getNamespace(), serialize($item->getRaw()), $ttl);
176
177
        } catch (Exception $e) {
178
179
            $this->setState(self::CACHE_ERROR, $e->getMessage());
180
            $data = false;
181
182
        }
183
184 120
        return $data;
185
186
    }
187
188 211 View Code Duplication
    public function test() {
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...
189
190 211
        if ( $this->driver->test() ) {
191
192 211
            $this->setState(self::CACHE_SUCCESS);
193
194 211
            return true;
195
196
        }
197
198
        $error = $this->driver->getName()." driver unavailable, disabling provider ".$this->getId()." administratively";
199
200
        $this->logger->error($error);
201
202
        $this->setState(self::CACHE_ERROR, $error);
203
204
        return false;
205
206
    }
207
208 12
    public function getItems(array $keys = []) {
209
210 12
        if ( empty($keys) ) return [];
211
212 6
        $result = [];
213
214
        try {
215
216 6
            $data = $this->driver->getMultiple($keys, $this->getNamespace());
217
218
        } catch (Exception $e) {
219
220
            $this->setState(self::CACHE_ERROR, $e->getMessage());
221
            $data = array_combine($keys, array_fill(0, count($keys), null));
222
223
        }
224
225 6
        foreach ( $data as $key => $value ) {
226
227 6
            if ( $value == null ) {
228
                $result[$key] = new Item($key);
229
            } else {
230 6
                $result[$key] = new Item($key, true);
231 6
                $result[$key]->set(unserialize($value));
232
            }
233
234
        }
235
236 6
        return $result;
237
238
    }
239
240 12 View Code Duplication
    public function deleteItems(array $keys) {
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...
241
242
        try {
243
244 12
            $data = $this->driver->deleteMultiple($keys, $this->getNamespace());
245
246
        } catch (Exception $e) {
247
248
            $this->setState(self::CACHE_ERROR, $e->getMessage());
249
            $data = false;
250
251
        }
252
253 12
        return $data;
254
255
    }
256
257 12 View Code Duplication
    public function saveDeferred(CacheItemInterface $item) {
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...
258
259 12
        $this->checkQueueNamespace(true);
260
261 12
        $namespace = $this->getNamespace();
262
263 12
        $this->queue[$namespace][$item->getKey()] = $item;
264
265 12
        return true;
266
267
    }
268
269 12 View Code Duplication
    public function commit() {
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...
270
271 12
        $result = [];
272
273 12
        $active_namespace = $this->getNamespace();
274
275 12
        foreach ( $this->queue as $namespace => $queue ) {
276
277 12
            $this->setNamespace($namespace);
278
279 12
            foreach ( $queue as $key => $item ) {
280
281 12
                $result[] = $this->save($item);
282
283
            }
284
285
        }
286
287 12
        $this->queue = [];
288
289 12
        $this->setNamespace($active_namespace);
290
291 12
        return in_array(false, $result) ? false : true;
292
293
    }
294
295 12 View Code Duplication
    private function checkQueueNamespace($create = false) {
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...
296
297 12
        $namespace = $this->getNamespace();
298
299 12
        if ( array_key_exists($namespace, $this->queue) ) {
300 6
            return true;
301 12
        } else if ( $create ) {
302 12
            $this->queue[$namespace] = [];
303 12
            return true;
304
        } else {
305
            return false;
306
        }
307
308
    }
309
310
}
311