PHPRedis   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 69
ccs 20
cts 22
cp 0.9091
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A connect() 0 4 1
A set() 0 4 1
A evalScript() 0 4 1
A ping() 0 9 2
A setOption() 0 4 1
A getPrefixOptionName() 0 4 1
A getInstance() 0 4 1
1
<?php
2
namespace AutoLock\Drivers;
3
4
/**
5
 * This file is part of mmdtl/autolock.
6
 *
7
 * (c) liulu <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the "LICENSE.md"
10
 * file that was distributed with this source code.
11
 */
12
use \Redis;
13
use \RedisException;
14
15
class PHPRedis implements Driver
16
{
17
    /**
18
     * @var Redis
19
     */
20
    private $redis;
21
22 31
    public function __construct(Redis $redis = null)
23
    {
24 31
        if (is_null($redis)) {
25 25
            $this->redis = new Redis();
26
        } else {
27 7
            $this->redis = $redis;
28
        }
29 31
    }
30
31 1
    public function connect($host, $port, $timeout)
32
    {
33 1
        return $this->redis->connect($host, $port, $timeout);
34
    }
35
36 1
    public function set($key, $value, $options = array())
37
    {
38 1
        return $this->redis->set($key, $value, $options);
39
    }
40
41
    /**
42
     * phpunit can't mock function whose name is keyword,so this
43
     * function have no unit test.You should modify carefully.
44
     * @param $script
45
     * @param array $args
46
     * @param int $numKeys
47
     * @return mixed
48
     */
49
    public function evalScript($script, $args = array(), $numKeys = 0)
50
    {
51
        return $this->redis->eval($script, $args, $numKeys);
52
    }
53
54
    /**
55
     * This method will never throw exception, only return false when can't connect with server
56
     * this function will return false or +PONG
57
     * @return mixed
58
     */
59 2
    public function ping()
60
    {
61
        try {
62 2
            $response = $this->redis->ping();
63 1
        } catch (RedisException $e) {
64 1
            $response = false;
65
        }
66 2
        return $response;
67
    }
68
69 1
    public function setOption($key, $value)
70
    {
71 1
        return $this->redis->setOption($key, $value);
72
    }
73
74 25
    public function getPrefixOptionName()
75
    {
76 25
        return Redis::OPT_PREFIX;
77
    }
78
79 1
    public function getInstance()
80
    {
81 1
        return $this->redis;
82
    }
83
}
84