Completed
Push — 6.13 ( 7e7b66...6c34c5 )
by André
53:02 queued 27:51
created

RedisIgbinaryLzf   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 39
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 39
loc 39
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getData() 9 9 2
A storeData() 19 19 3
A isAvailable() 6 6 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * File containing the RedisIgbinaryLzf class.
5
 *
6
 * @copyright Copyright (c) 2009, Robert Hafner. All rights reserved.
7
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
8
 *
9
 * Original source: https://github.com/tedious/Stash/blob/master/src/Stash/Driver/Redis.php
10
 *
11
 * @license For full copyright and license information view LICENSE file distributed with this source code.
12
 */
13
namespace eZ\Bundle\EzPublishCoreBundle\Cache\Driver\Redis;
14
15
use Stash\Driver\Redis as StashRedis;
16
17 View Code Duplication
class RedisIgbinaryLzf extends StashRedis
0 ignored issues
show
Duplication introduced by
This class 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...
18
{
19
    public function getData($key)
20
    {
21
        $data = $this->redis->get($this->makeKeyString($key));
22
        if (false !== $data) {
23
            return igbinary_unserialize(lzf_decompress($data));
24
        }
25
26
        return $data;
27
    }
28
29
    public function storeData($key, $data, $expiration)
30
    {
31
        lzf_optimized_for(0);
32
33
        $store = lzf_compress(igbinary_serialize(array('data' => $data, 'expiration' => $expiration)));
34
        if (null === $expiration) {
35
            return $this->redis->set($this->makeKeyString($key), $store);
36
        } else {
37
            $ttl = $expiration - time();
38
39
            // Prevent us from even passing a negative ttl'd item to redis,
40
            // since it will just round up to zero and cache forever.
41
            if ($ttl < 1) {
42
                return true;
43
            }
44
45
            return $this->redis->setex($this->makeKeyString($key), $ttl, $store);
46
        }
47
    }
48
49
    public static function isAvailable()
50
    {
51
        return class_exists('Redis', false)
52
            && extension_loaded('igbinary')
53
            && extension_loaded('lzf');
54
    }
55
}
56