Config   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 191
rs 10
c 0
b 0
f 0
wmc 21

19 Methods

Rating   Name   Duplication   Size   Complexity  
A getPredisConfigArray() 0 8 2
A getHost() 0 3 1
A getPassword() 0 3 1
A getTimeout() 0 3 1
A setOptPrefix() 0 3 1
A setHost() 0 3 1
A getOptPrefix() 0 3 1
A setScheme() 0 6 2
A getPredisClient() 0 3 1
A isPersistent() 0 3 1
A setPort() 0 3 1
A getPort() 0 3 1
A getScheme() 0 3 1
A setPersistent() 0 3 1
A setDatabase() 0 3 1
A setPassword() 0 3 1
A getDatabase() 0 3 1
A setPredisClient() 0 3 1
A setTimeout() 0 3 1
1
<?php
2
3
/**
4
 *
5
 * This file is part of Phpfastcache.
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
10
 *
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 * @author Contributors  https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phpfastcache\Drivers\Predis;
18
19
use Phpfastcache\Config\ConfigurationOption;
20
use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException;
21
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
22
use Predis\Client;
23
24
class Config extends ConfigurationOption
25
{
26
    protected string $host = '127.0.0.1';
27
    protected int $port = 6379;
28
    protected string $password = '';
29
    protected int $database = 0;
30
    protected ?Client $predisClient = null;
31
    protected string $optPrefix = '';
32
    protected int $timeout = 5;
33
    protected bool $persistent = false;
34
    protected string $scheme = 'unix';
35
/**
36
     * @return array<string, mixed>
37
     */
38
    public function getPredisConfigArray(): array
39
    {
40
        return [
41
            'host' => $this->getHost(),
42
            'port' => $this->getPort(),
43
            'password' => $this->getPassword() ?: null,
44
            'database' => $this->getDatabase(),
45
            'timeout' => $this->getTimeout(),
46
        ];
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function getHost(): string
53
    {
54
        return $this->host;
55
    }
56
57
    /**
58
     * @param string $host
59
     * @return Config
60
     * @throws PhpfastcacheLogicException
61
     */
62
    public function setHost(string $host): static
63
    {
64
        return $this->setProperty('host', $host);
65
    }
66
67
    /**
68
     * @return int
69
     */
70
    public function getPort(): int
71
    {
72
        return $this->port;
73
    }
74
75
    /**
76
     * @param int $port
77
     * @return Config
78
     * @throws PhpfastcacheLogicException
79
     */
80
    public function setPort(int $port): static
81
    {
82
        return $this->setProperty('port', $port);
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getPassword(): string
89
    {
90
        return $this->password;
91
    }
92
93
    /**
94
     * @param string $password
95
     * @return self
96
     * @throws PhpfastcacheLogicException
97
     */
98
    public function setPassword(string $password): static
99
    {
100
        return $this->setProperty('password', $password);
101
    }
102
103
    /**
104
     * @return int
105
     */
106
    public function getDatabase(): int
107
    {
108
        return $this->database;
109
    }
110
111
    /**
112
     * @param int $database
113
     * @return Config
114
     * @throws PhpfastcacheLogicException
115
     */
116
    public function setDatabase(int $database): static
117
    {
118
        return $this->setProperty('database', $database);
119
    }
120
121
    /**
122
     * @return int
123
     */
124
    public function getTimeout(): int
125
    {
126
        return $this->timeout;
127
    }
128
129
    /**
130
     * @param int $timeout
131
     * @return self
132
     * @throws PhpfastcacheLogicException
133
     */
134
    public function setTimeout(int $timeout): static
135
    {
136
        return $this->setProperty('timeout', $timeout);
137
    }
138
139
    /**
140
     * @return Client|null
141
     */
142
    public function getPredisClient(): ?Client
143
    {
144
        return $this->predisClient;
145
    }
146
147
    /**
148
     * @param Client|null $predisClient
149
     * @return Config
150
     * @throws PhpfastcacheLogicException
151
     */
152
    public function setPredisClient(?Client $predisClient = null): Config
153
    {
154
        return $this->setProperty('predisClient', $predisClient);
155
    }
156
157
    /**
158
     * @return string
159
     * @since 7.0.2
160
     */
161
    public function getOptPrefix(): string
162
    {
163
        return $this->optPrefix;
164
    }
165
166
    /**
167
     * @param string $optPrefix
168
     * @return Config
169
     * @throws PhpfastcacheLogicException
170
     * @since 7.0.2
171
     */
172
    public function setOptPrefix(string $optPrefix): Config
173
    {
174
        return $this->setProperty('optPrefix', trim($optPrefix));
175
    }
176
177
    /**
178
     * @return bool
179
     */
180
    public function isPersistent(): bool
181
    {
182
        return $this->persistent;
183
    }
184
185
    /**
186
     * @param bool $persistent
187
     * @return Config
188
     * @throws PhpfastcacheLogicException
189
     */
190
    public function setPersistent(bool $persistent): Config
191
    {
192
        return $this->setProperty('persistent', $persistent);
193
    }
194
195
    /**
196
     * @return string
197
     */
198
    public function getScheme(): string
199
    {
200
        return $this->scheme;
201
    }
202
203
    /**
204
     * @param string $scheme
205
     * @return Config
206
     * @throws PhpfastcacheInvalidConfigurationException
207
     * @throws PhpfastcacheLogicException
208
     */
209
    public function setScheme(string $scheme): Config
210
    {
211
        if (!in_array($scheme, ['unix', 'tls'], true)) {
212
            throw new PhpfastcacheInvalidConfigurationException('Invalid scheme: ' . $scheme);
213
        }
214
        return $this->setProperty('scheme', $scheme);
215
    }
216
}
217