Config   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 0
dl 0
loc 102
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C resolveDsn() 0 27 11
A getHost() 0 4 1
A getPort() 0 4 1
A getTimeout() 0 4 1
A getPrefix() 0 4 1
A setPrefix() 0 4 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
class Config
13
{
14
    /**
15
     * @var string | null
16
     */
17
    private $dsn;
18
19
    /**
20
     * @var string
21
     */
22
    private $host = '127.0.0.1';
23
24
    /**
25
     * @var int
26
     */
27
    private $port = 6379;
28
29
    /**
30
     * @var float
31
     */
32
    private $timeout = 5;
33
34
    /**
35
     * @var string
36
     */
37
    private $prefix = 'lock:';
38
39 20
    public function __construct($dsn)
40
    {
41 20
        $this->dsn = $dsn;
42 20
        $this->resolveDsn();
43 20
    }
44
45 20
    protected function resolveDsn()
46
    {
47 20
        if (is_array($this->dsn)) {
48 4
            $dsn = $this->dsn;
49 4
            $this->host = empty($dsn[0]) ? $this->host : (string)$dsn[0];
50 4
            $this->port = empty($dsn[1]) ? $this->port : (int)$dsn[1];
51 4
            $this->timeout = empty($dsn[2]) ? $this->timeout : (float)$dsn[2];
52
        } else {
53 16
            $dsnArray = explode('@', $this->dsn);
54 16
            if (count($dsnArray) >= 1) {
55 16
                if (count($dsnArray) >= 2) {
56 12
                    list($hostAndPort, $timeout) = $dsnArray;
57
                } else {
58 4
                    $hostAndPort = $dsnArray[0];
59
                }
60 16
                $hostAndPortArray = explode(':', $hostAndPort);
61 16
                if (count($hostAndPortArray) >= 2) {
62 13
                    list($host, $port) = $hostAndPortArray;
63
                } else {
64 3
                    $host = $hostAndPortArray[0];
65
                }
66
            }
67 16
            $this->host = empty($host) ? $this->host : (string)$host;
68 16
            $this->port = empty($port) ? $this->port : (int)$port;
69 16
            $this->timeout = empty($timeout) ? $this->timeout : (float)$timeout;
70
        }
71 20
    }
72
73
    /**
74
     * @return string
75
     */
76 19
    public function getHost()
77
    {
78 19
        return $this->host;
79
    }
80
81
    /**
82
     * @return int
83
     */
84 19
    public function getPort()
85
    {
86 19
        return $this->port;
87
    }
88
89
    /**
90
     * @return float
91
     */
92 19
    public function getTimeout()
93
    {
94 19
        return $this->timeout;
95
    }
96
97
    /**
98
     * @return string
99
     */
100 11
    public function getPrefix()
101
    {
102 11
        return $this->prefix;
103
    }
104
105
    /**
106
     * @param string $prefix
107
     */
108 1
    public function setPrefix($prefix)
109
    {
110 1
        $this->prefix = (string)$prefix;
111 1
    }
112
113
}
114