RedisDriver   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 127
rs 10
c 0
b 0
f 0
wmc 17

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 3 1
A clear() 0 3 1
A connect() 0 20 4
A __construct() 0 8 2
B setConfig() 0 36 7
A check() 0 7 2
1
<?php
2
/**
3
 * This file is part of the InMemoryList package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 */
10
11
namespace InMemoryList\Infrastructure\Drivers;
12
13
use InMemoryList\Infrastructure\Drivers\Contracts\DriverInterface;
14
use InMemoryList\Infrastructure\Drivers\Exceptions\RedisDriverCheckException;
15
use InMemoryList\Infrastructure\Drivers\Exceptions\RedisMalformedConfigException;
16
use Predis\Client as Redis;
17
18
class RedisDriver implements DriverInterface
19
{
20
    /**
21
     * @var
22
     */
23
    private $config;
24
25
    /**
26
     * @var Redis
27
     */
28
    private $instance;
29
30
    /**
31
     * RedisDriver constructor.
32
     *
33
     * @codeCoverageIgnore
34
     *
35
     * @param array $config
36
     *
37
     * @throws RedisDriverCheckException
38
     */
39
    public function __construct(array $config = [])
40
    {
41
        $this->setConfig($config);
42
        if (!$this->check()) {
43
            throw new RedisDriverCheckException('PRedis Client is not loaded.');
44
        }
45
46
        $this->connect();
47
    }
48
49
    /**
50
     * @param $config
51
     *
52
     * @throws RedisMalformedConfigException
53
     */
54
    private function setConfig($config)
55
    {
56
        $allowedConfigKeys = [
57
            'alias',
58
            'async',
59
            'database',
60
            'host',
61
            'iterable_multibulk',
62
            'options',
63
            'password',
64
            'path',
65
            'port',
66
            'persistent',
67
            'profile',
68
            'timeout',
69
            'read_write_timeout',
70
            'scheme',
71
            'throw_errors',
72
            'weight',
73
        ];
74
75
        foreach ($config as $param => $server) {
76
            if (is_array($server)) {
77
                foreach (array_keys($server) as $key) {
78
                    if (!in_array($key, $allowedConfigKeys)) {
79
                        throw new RedisMalformedConfigException();
80
                    }
81
                }
82
            }
83
84
            if (!is_array($server) && !in_array($param, $allowedConfigKeys)) {
85
                throw new RedisMalformedConfigException();
86
            }
87
        }
88
89
        $this->config = $config;
90
    }
91
92
    /**
93
     * @codeCoverageIgnore
94
     *
95
     * @return bool
96
     */
97
    public function check()
98
    {
99
        if (extension_loaded('Redis')) {
100
            trigger_error('The native Redis extension is installed, you should use Redis instead of Predis to increase performances', E_USER_NOTICE);
101
        }
102
103
        return class_exists('Predis\Client');
104
    }
105
106
    /**
107
     * @return mixed
108
     */
109
    public function clear()
110
    {
111
        $this->instance->flushall();
112
    }
113
114
    /**
115
     * @return bool
116
     */
117
    public function connect()
118
    {
119
        $servers = $this->config ?: [];
120
121
        if (count($servers) === 1) {
122
            $servers = $servers[0];
123
        }
124
125
        if (count($servers) === 0) {
126
            $servers = [
127
                'host' => '127.0.0.1',
128
                'port' => 6379,
129
                'password' => null,
130
                'database' => null,
131
            ];
132
        }
133
134
        $this->instance = new Redis($servers);
135
136
        return true;
137
    }
138
139
    /**
140
     * @return mixed
141
     */
142
    public function getInstance()
143
    {
144
        return $this->instance;
145
    }
146
}
147