Server   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 114
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getConfig() 0 4 1
A getDriver() 0 4 1
A open() 0 18 4
A set() 0 4 1
A evalScript() 0 4 1
A available() 0 10 2
A getInstance() 0 5 1
1
<?php
2
/**
3
 * This file is part of mmdtl/autolock.
4
 *
5
 * (c) liulu <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the "LICENSE.md"
8
 * file that was distributed with this source code.
9
 */
10
namespace AutoLock;
11
12
use autolock\Drivers\Driver;
13
use AutoLock\Exception\ServerConnectException;
14
use AutoLock\Exception\ServersOperateException;
15
16
/**
17
 * Class Server is object to connect to redis server , create and release lock.
18
 *
19
 * @package AutoLock
20
 * @author Liu Lu <[email protected]>
21
 * @since 0.1
22
 */
23
class Server
24
{
25
    /**
26
     * @var Config
27
     */
28
    private $config;
29
30
    /**
31
     * @var Driver;
32
     */
33
    private $instance;
34
35
    /**
36
     * @var Driver
37
     */
38
    protected $driver;
39
40
    /**
41
     * Server constructor.
42
     * @param Config $config
43
     * @param Driver $driver
44
     */
45 24
    public function __construct(Config $config, Driver $driver)
46
    {
47 24
        $this->config = $config;
48 24
        $this->driver = $driver;
49 24
    }
50
51
    /**
52
     * @return Config
53
     */
54 2
    public function getConfig()
55
    {
56 2
        return $this->config;
57
    }
58
59
    /**
60
     * @return Driver
61
     */
62 1
    public function getDriver()
63
    {
64 1
        return $this->driver;
65
    }
66
67
    /**
68
     * @throws ServerConnectException
69
     * @throws ServersOperateException
70
     */
71 12
    protected function open()
72
    {
73 12
        if (empty($this->instance)) {
74 12
            $config = $this->config;
75 12
            $driver = $this->driver;
76 12
            $status = $driver->connect($config->getHost(), $config->getPort(), $config->getTimeout());
77 12
            if ($status !== true) {
78 1
                throw new ServerConnectException('server connect fail.');
79
            }
80 11
            $optionName = $driver->getPrefixOptionName();
81 11
            $prefix = $config->getPrefix();
82 11
            $status = $driver->setOption($optionName, $prefix);
83 11
            if ($status !== true) {
84 1
                throw new ServersOperateException("server set option $optionName:$prefix fail.");
85
            }
86 10
            $this->instance = $driver;
87
        }
88 10
    }
89
90
    /**
91
     * @param $key
92
     * @param $value
93
     * @param array $Options
94
     * @return Bool
95
     */
96 3
    public function set($key, $value, $Options = array())
97
    {
98 3
        return $this->getInstance()->set($key, $value, $Options);
99
    }
100
101
    /**
102
     * @param $script
103
     * @param array $args
104
     * @param int $numKeys
105
     * @return mixed
106
     */
107 3
    public function evalScript($script, $args = array(), $numKeys = 0)
108
    {
109 3
        return $this->getInstance()->evalScript($script, $args, $numKeys);
110
    }
111
112
    /**
113
     * @return bool
114
     */
115 3
    public function available()
116
    {
117 3
        $status = false;
118 3
        $successPingString = Driver::PONG_STRING;
119 3
        $responseString = $this->getInstance()->ping();
120 2
        if ($responseString === $successPingString) {
121 1
            $status = true;
122
        }
123 2
        return $status;
124
    }
125
126
    /**
127
     * @return Driver
128
     * @throws ServerConnectException
129
     * @throws ServersOperateException
130
     */
131 12
    public function getInstance()
132
    {
133 12
        $this->open();
134 10
        return $this->instance;
135
    }
136
}
137