Passed
Push — master ( c82647...c877fa )
by Georges
11:01
created

Config::setPassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Rediscluster;
18
19
use Phpfastcache\Config\ConfigurationOption;
20
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
21
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
22
use RedisCluster as RedisClusterClient;
23
24
class Config extends ConfigurationOption
25
{
26
    /**
27
     * @var array<string>
28
     */
29
    protected array $clusters = [];
30
    protected string $password = '';
31
    protected int $timeout = 5;
32
    protected int $readTimeout = 5;
33
    protected ?RedisClusterClient $redisClusterClient = null;
34
    protected string $optPrefix = '';
35
    protected ?int $slaveFailover = null;
36
37
    /**
38
     * @return array<string>
39
     */
40
    public function getClusters(): array
41
    {
42
        return $this->clusters;
43
    }
44
45
    /**
46
     * @param string $cluster
47
     * @return Config
48
     */
49
    public function addCluster(string $cluster): static
50
    {
51
        $this->enforceLockedProperty(__FUNCTION__);
52
        $this->clusters[] = $cluster;
53
        return $this;
54
    }
55
56
    /**
57
     * @param string $clusters
58
     * @return Config
59
     */
60
    public function setClusters(...$clusters): static
61
    {
62
        return $this->setProperty('clusters', $clusters);
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getPassword(): string
69
    {
70
        return $this->password;
71
    }
72
73
    /**
74
     * @param string $password
75
     *
76
     * @return static
77
     * @throws PhpfastcacheLogicException
78
     */
79
    public function setPassword(string $password): static
80
    {
81
        return $this->setProperty('password', $password);
82
    }
83
84
    /**
85
     * @return int
86
     */
87
    public function getTimeout(): int
88
    {
89
        return $this->timeout;
90
    }
91
92
    /**
93
     * @param int $timeout
94
     * @return static
95
     * @throws PhpfastcacheLogicException
96
     */
97
    public function setTimeout(int $timeout): static
98
    {
99
        return $this->setProperty('timeout', $timeout);
100
    }
101
102
    /**
103
     * @return int
104
     */
105
    public function getReadTimeout(): int
106
    {
107
        return $this->readTimeout;
108
    }
109
110
    /**
111
     * @param int $readTimeout
112
     * @return static
113
     * @throws PhpfastcacheLogicException
114
     */
115
    public function setReadTimeout(int $readTimeout): static
116
    {
117
        return $this->setProperty('readTimeout', $readTimeout);
118
    }
119
120
121
    /**
122
     * @return RedisClusterClient|null
123
     */
124
    public function getRedisClusterClient(): ?RedisClusterClient
125
    {
126
        return $this->redisClusterClient;
127
    }
128
129
    /**
130
     * @param RedisClusterClient|null $redisClusterClient
131
     * @return Config
132
     * @throws PhpfastcacheLogicException
133
     */
134
    public function setRedisClusterClient(?RedisClusterClient $redisClusterClient): static
135
    {
136
        return $this->setProperty('redisClusterClient', $redisClusterClient);
137
    }
138
139
    /**
140
     * @return string
141
     * @since 7.0.2
142
     */
143
    public function getOptPrefix(): string
144
    {
145
        return $this->optPrefix;
146
    }
147
148
    /**
149
     * @param string $optPrefix
150
     * @return Config
151
     * @throws PhpfastcacheLogicException
152
     * @since 7.0.2
153
     */
154
    public function setOptPrefix(string $optPrefix): static
155
    {
156
        return $this->setProperty('optPrefix', trim($optPrefix));
157
    }
158
159
    /**
160
     * @return int|null
161
     */
162
    public function getSlaveFailover(): ?int
163
    {
164
        return $this->slaveFailover;
165
    }
166
167
    /**
168
     * @param int|null $slaveFailover
169
     * @return Config
170
     * @throws PhpfastcacheInvalidArgumentException
171
     */
172
    public function setSlaveFailover(?int $slaveFailover): static
173
    {
174
        if (
175
            $slaveFailover !== null && !in_array($slaveFailover, [
176
            RedisClusterClient::FAILOVER_NONE,
177
            RedisClusterClient::FAILOVER_ERROR,
178
            RedisClusterClient::FAILOVER_DISTRIBUTE,
179
            RedisClusterClient::FAILOVER_DISTRIBUTE_SLAVES,
180
            ])
181
        ) {
182
            throw new PhpfastcacheInvalidArgumentException('Invalid Slave Failover option: ' . $slaveFailover);
183
        }
184
        return $this->setProperty('slaveFailover', $slaveFailover);
185
    }
186
}
187