RedisDriver   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 121
rs 10
c 0
b 0
f 0
wmc 17

5 Methods

Rating   Name   Duplication   Size   Complexity  
C setConfig() 0 36 7
A instance() 0 3 1
A check() 0 7 2
A __construct() 0 7 2
B connect() 0 23 5
1
<?php
2
/**
3
 * This file is part of the EventStoreManager 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 SimpleEventStoreManager\Infrastructure\Drivers;
12
13
use SimpleEventStoreManager\Infrastructure\Drivers\Contracts\DriverInterface;
14
use SimpleEventStoreManager\Infrastructure\Drivers\Exceptions\MalformedDriverConfigException;
15
use SimpleEventStoreManager\Infrastructure\Drivers\Exceptions\ManageAggregateIndexException;
0 ignored issues
show
Bug introduced by
The type SimpleEventStoreManager\...AggregateIndexException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use SimpleEventStoreManager\Infrastructure\Drivers\Exceptions\NotInstalledDriverCheckException;
17
use Predis\Client as Redis;
18
19
class RedisDriver implements DriverInterface
20
{
21
    /**
22
     * @var
23
     */
24
    private $config;
25
26
    /**
27
     * @var Redis
28
     */
29
    private $instance;
30
31
    /**
32
     * RedisDriver constructor.
33
     *
34
     * @codeCoverageIgnore
35
     *
36
     * @param array $config
37
     *
38
     * @throws NotInstalledDriverCheckException
39
     */
40
    public function __construct(array $config = [])
41
    {
42
        $this->setConfig($config);
43
        if (!$this->check()) {
44
            throw new NotInstalledDriverCheckException('PRedis Client is not loaded.');
45
        }
46
        $this->connect();
47
    }
48
49
    /**
50
     * @param $config
51
     *
52
     * @throws MalformedDriverConfigException
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 MalformedDriverConfigException('Redis Driver: malformed config parameters');
80
                    }
81
                }
82
            }
83
84
            if (!is_array($server) && !in_array($param, $allowedConfigKeys)) {
85
                throw new MalformedDriverConfigException('Redis Driver: malformed config parameters');
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 bool
108
     */
109
    public function connect()
110
    {
111
        $servers = $this->config ?: [];
112
113
        if (count($servers) === 1) {
114
            $servers = $servers[0];
115
        }
116
117
        if (count($servers) === 0) {
118
            $servers = [
119
                'host' => '127.0.0.1',
120
                'port' => 6379,
121
                'password' => null,
122
                'database' => null,
123
            ];
124
        }
125
126
        $database = (isset($this->config['database'])) ?: 0;
127
128
        $this->instance = new Redis($servers);
129
        $this->instance->select($database);
130
131
        return true;
132
    }
133
134
    /**
135
     * @return mixed
136
     */
137
    public function instance()
138
    {
139
        return $this->instance;
140
    }
141
}
142