Completed
Push — master ( 420da5...d93dfb )
by Pierre
43:19 queued 10:32
created

Adapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace App\Component\Cache\Redis;
4
5
use App\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 adapter config
16
     *
17
     * @var array
18
     */
19
    protected $config;
20
21
    /**
22
     * redis instance
23
     *
24
     * @var \Redis
25
     */
26
    protected $instance;
27
28
    /**
29
     * error
30
     *
31
     * @var Boolean
32
     */
33
    protected $error;
34
35
    /**
36
     * error code
37
     *
38
     * @var int
39
     */
40
    protected $errorCode;
41
42
    /**
43
     * error message
44
     *
45
     * @var string
46
     */
47
    protected $errorMessage;
48
49
    /**
50
     * instanciate
51
     *
52
     * @param Config $config
53
     */
54
    public function __construct(Config $config)
55
    {
56
        $this->config = $config->getSettings(self::_REDIS);
57
        $this->error = false;
58
        $this->errorCode = 0;
59
        $this->errorMessage = '';
60
    }
61
62
    /**
63
     * return redis client instance
64
     *
65
     * @return Redis
66
     */
67
    public function getClient(): Redis
68
    {
69
        if (is_null($this->instance)) {
70
            try {
71
                $this->instance =  new Redis();
72
                $this->instance->connect(
73
                    $this->config[self::_HOST],
74
                    $this->config[self::_PORT]
75
                );
76
            } catch (\RedisException $e) {
77
                $this->error = true;
78
                $this->errorCode = $e->getCode();
79
                $this->errorMessage = $e->getMessage();
80
            }
81
        }
82
        return $this->instance;
83
    }
84
85
    /**
86
     * return true if error
87
     *
88
     * @return boolean
89
     */
90
    public function isError(): bool
91
    {
92
        return $this->error === true;
93
    }
94
95
    /**
96
     * return code error
97
     *
98
     * @return int
99
     */
100
    public function getErrorCode(): int
101
    {
102
        return $this->errorCode;
103
    }
104
105
    /**
106
     * return error message
107
     *
108
     * @return string
109
     */
110
    public function getErrorMessage(): string
111
    {
112
        return $this->errorMessage;
113
    }
114
}
115