Adapter::getErrorCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Component\Cache\Redis;
6
7
use Nymfonya\Component\Config;
8
use Redis;
9
10
class Adapter
11
{
12
    const _REDIS = 'redis';
13
    const _HOST = 'host';
14
    const _PORT = 'port';
15
16
    /**
17
     * redis server hostname
18
     *
19
     * @var string
20
     */
21
    protected $host;
22
23
    /**
24
     * redis server port
25
     *
26
     * @var integer
27
     */
28
    protected $port;
29
30
    /**
31
     * redis instance
32
     *
33
     * @var \Redis
34
     */
35
    protected $instance;
36
37
    /**
38
     * error
39
     *
40
     * @var Boolean
41
     */
42
    protected $error;
43
44
    /**
45
     * error code
46
     *
47
     * @var int
48
     */
49
    protected $errorCode;
50
51
    /**
52
     * error message
53
     *
54
     * @var string
55
     */
56
    protected $errorMessage;
57
58
    /**
59
     * instanciate
60
     *
61
     * @param Config $config
62
     */
63 11
    public function __construct(Config $config)
64
    {
65 11
        $config = $config->getSettings(self::_REDIS);
66 11
        $this->applyConfig($config[self::_HOST], $config[self::_PORT]);
67 11
        $this->error = false;
68 11
        $this->errorCode = 0;
69 11
        $this->errorMessage = '';
70
    }
71
72
    /**
73
     * return redis client instance
74
     *
75
     * @return Redis
76
     */
77 3
    public function getClient(): Redis
78
    {
79 3
        if (is_null($this->instance)) {
80
            try {
81 3
                $this->instance = new Redis();
82 3
                $con = @$this->instance->connect($this->host, $this->port);
83 1
                if (false === $con) {
84 1
                    throw new \RedisException('Connection refused', 2);
85
                }
86 2
            } catch (\RedisException $e) {
87 2
                $this->error = true;
88 2
                $this->errorMessage = $e->getMessage();
89 2
                $this->errorCode = 1;
90
            }
91
        }
92 3
        return $this->instance;
93
    }
94
95
    /**
96
     * return true if error
97
     *
98
     * @return boolean
99
     */
100 4
    public function isError(): bool
101
    {
102 4
        return $this->error === true;
103
    }
104
105
    /**
106
     * return code error
107
     *
108
     * @return int
109
     */
110 3
    public function getErrorCode(): int
111
    {
112 3
        return $this->errorCode;
113
    }
114
115
    /**
116
     * return error message
117
     *
118
     * @return string
119
     */
120 3
    public function getErrorMessage(): string
121
    {
122 3
        return $this->errorMessage;
123
    }
124
125
    /**
126
     * set redis server config
127
     *
128
     * @param string $host
129
     * @param integer $port
130
     * @return Adapter
131
     */
132 1
    protected function applyConfig(string $host, int $port): Adapter
133
    {
134 1
        $this->host = $host;
135 1
        $this->port = $port;
136 1
        return $this;
137
    }
138
}
139