Completed
Push — master ( e388d4...73bb33 )
by Pierre
04:01
created

Adapter::getClient()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
nc 4
nop 0
dl 0
loc 13
ccs 9
cts 9
cp 1
crap 3
rs 9.9666
c 1
b 0
f 0
1
<?php
2
3
namespace App\Component\Cache\Redis;
4
5
use Nymfonya\Component\Config;
6
use \Redis;
7
8
class Adapter
9
{
10
    const _REDIS = 'redis';
11
    const _HOST = 'host';
12
    const _PORT = 'port';
13
14
    /**
15
     * redis server hostname
16
     *
17
     * @var string
18
     */
19
    protected $host;
20
21
    /**
22
     * redis server port
23
     *
24
     * @var integer
25
     */
26
    protected $port;
27
28
    /**
29
     * redis instance
30
     *
31
     * @var \Redis
32
     */
33
    protected $instance;
34
35
    /**
36
     * error
37
     *
38
     * @var Boolean
39
     */
40
    protected $error;
41
42
    /**
43
     * error code
44
     *
45
     * @var int
46
     */
47
    protected $errorCode;
48
49
    /**
50
     * error message
51
     *
52
     * @var string
53
     */
54
    protected $errorMessage;
55
56
    /**
57
     * instanciate
58
     *
59
     * @param Config $config
60
     */
61 11
    public function __construct(Config $config)
62
    {
63 11
        $config = $config->getSettings(self::_REDIS);
64 11
        $this->applyConfig($config[self::_HOST], $config[self::_PORT]);
65 11
        $this->error = false;
66 11
        $this->errorCode = 0;
67 11
        $this->errorMessage = '';
68
    }
69
70
    /**
71
     * return redis client instance
72
     *
73
     * @return Redis
74
     */
75 3
    public function getClient(): Redis
76
    {
77 3
        if (is_null($this->instance)) {
78
            try {
79 3
                $this->instance =  new Redis();
80 3
                @$this->instance->connect($this->host, $this->port);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for connect(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

80
                /** @scrutinizer ignore-unhandled */ @$this->instance->connect($this->host, $this->port);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
81 2
            } catch (\RedisException $e) {
82 2
                $this->error = true;
83 2
                $this->errorMessage = $e->getMessage();
84 2
                $this->errorCode = 1;
85
            }
86
        }
87 3
        return $this->instance;
88
    }
89
90
    /**
91
     * return true if error
92
     *
93
     * @return boolean
94
     */
95 3
    public function isError(): bool
96
    {
97 3
        return $this->error === true;
98
    }
99
100
    /**
101
     * return code error
102
     *
103
     * @return int
104
     */
105 3
    public function getErrorCode(): int
106
    {
107 3
        return $this->errorCode;
108
    }
109
110
    /**
111
     * return error message
112
     *
113
     * @return string
114
     */
115 3
    public function getErrorMessage(): string
116
    {
117 3
        return $this->errorMessage;
118
    }
119
120
    /**
121
     * set redis server config
122
     *
123
     * @param string $host
124
     * @param integer $port
125
     * @return Adapter
126
     */
127 1
    protected function applyConfig(string $host, int $port): Adapter
128
    {
129 1
        $this->host = $host;
130 1
        $this->port = $port;
131 1
        return $this;
132
    }
133
}
134