RedisCache   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 110
Duplicated Lines 5.45 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 68.75%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 6
loc 110
ccs 22
cts 32
cp 0.6875
rs 10
c 3
b 2
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 6 25 5
A __destruct() 0 4 1
A close() 0 4 1
A get() 0 7 2
A set() 0 4 1
A has() 0 4 1
A delete() 0 7 2

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
 * Created by PhpStorm.
4
 * User: Jenner
5
 * Date: 2015/8/20
6
 * Time: 15:14
7
 */
8
9
namespace Jenner\SimpleFork\Cache;
10
11
12
/**
13
 * redis cache
14
 *
15
 * @package Jenner\SimpleFork\Cache
16
 */
17
class RedisCache implements CacheInterface
18
{
19
20
    /**
21
     * @var \Redis
22
     */
23
    protected $redis;
24
25
    protected $prefix;
26
27
    /**
28
     * @param string $host
29
     * @param int $port
30
     * @param int $database
31
     * @param string $prefix
32
     */
33 3
    public function __construct(
34
        $host = '127.0.0.1',
35
        $port = 6379,
36
        $database = 0,
37
        $prefix = 'simple-fork'
38
    )
39
    {
40 3
        $this->redis = new \Redis();
41 3
        $connection_result = $this->redis->connect($host, $port);
42 3
        if (!$connection_result) {
43
            throw new \RuntimeException('can not connect to the redis server');
44
        }
45
46 3 View Code Duplication
        if ($database != 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
47
            $select_result = $this->redis->select($database);
48
            if (!$select_result) {
49
                throw new \RuntimeException('can not select the database');
50
            }
51
        }
52
53 3
        if (empty($prefix)) {
54
            throw new \InvalidArgumentException('prefix can not be empty');
55
        }
56 3
        $this->prefix = $prefix;
57 3
    }
58
59
    /**
60
     * close redis connection
61
     */
62
    public function __destruct()
63
    {
64
        $this->close();
65
    }
66
67
    /**
68
     * close the connection
69
     */
70 3
    public function close()
71
    {
72 3
        $this->redis->close();
73 3
    }
74
75
    /**
76
     * get var
77
     *
78
     * @param $key
79
     * @param null $default
80
     * @return bool|string|null
81
     */
82 3
    public function get($key, $default = null)
83
    {
84 3
        $result = $this->redis->hGet($this->prefix, $key);
85 3
        if ($result !== false) return $result;
86
87 3
        return $default;
88
    }
89
90
    /**
91
     * set var
92
     *
93
     * @param $key
94
     * @param null $value
95
     * @return boolean
96
     */
97 3
    public function set($key, $value)
98
    {
99 3
        return $this->redis->hSet($this->prefix, $key, $value);
100
    }
101
102
    /**
103
     * has var ?
104
     *
105
     * @param $key
106
     * @return bool
107
     */
108 3
    public function has($key)
109
    {
110 3
        return $this->redis->hExists($this->prefix, $key);
111
    }
112
113
    /**
114
     * delete var
115
     *
116
     * @param $key
117
     * @return bool
118
     */
119 3
    public function delete($key)
120
    {
121 3
        if ($this->redis->hDel($this->prefix, $key) > 0) {
122 3
            return true;
123
        }
124
        return false;
125
    }
126
}