Passed
Push — v9 ( d487bf...d9ab0c )
by Georges
11:51
created

Config::getSaslUser()   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 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * This file is part of Phpfastcache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
9
 *
10
 * @author Georges.L (Geolim4)  <[email protected]>
11
 * @author Contributors  https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
12
 */
13
14
declare(strict_types=1);
15
16
namespace Phpfastcache\Drivers\Memcached;
17
18
use Phpfastcache\Config\ConfigurationOption;
19
use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException;
20
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
21
22
class Config extends ConfigurationOption
23
{
24
    /**
25
     * @var array
26
     *
27
     * Multiple server can be added this way:
28
     *       $cfg->setServers([
29
     *         [
30
     *           // If you use an UNIX socket set the host and port to null
31
     *           'host' => '127.0.0.1',
32
     *           //'path' => 'path/to/unix/socket',
33
     *           'port' => 11211,
34
     *           'saslUser' => null,
35
     *           'saslPassword' => null,
36
     *         ]
37
     *      ]);
38
     */
39
    protected array $servers = [];
40
41
    protected string $host = '127.0.0.1';
42
43
    protected int $port = 11211;
44
45
    protected string $saslUser = '';
46
47
    protected string $saslPassword = '';
48
49
    protected string $optPrefix = '';
50
51
    /**
52
     * @return string
53
     */
54
    public function getSaslUser(): string
55
    {
56
        return $this->saslUser;
57
    }
58
59
    /**
60
     * @param string $saslUser
61
     * @return self
62
     * @throws PhpfastcacheLogicException
63
     */
64
    public function setSaslUser(string $saslUser): static
65
    {
66
        $this->enforceLockedProperty(__FUNCTION__);
67
        $this->saslUser = $saslUser;
68
        return $this;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getSaslPassword(): string
75
    {
76
        return $this->saslPassword;
77
    }
78
79
    /**
80
     * @param string $saslPassword
81
     * @return self
82
     * @throws PhpfastcacheLogicException
83
     */
84
    public function setSaslPassword(string $saslPassword): static
85
    {
86
        $this->enforceLockedProperty(__FUNCTION__);
87
        $this->saslPassword = $saslPassword;
88
        return $this;
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    public function getServers(): array
95
    {
96
        if (!count($this->servers)) {
97
            return [
98
                [
99
                    'host' => $this->getHost(),
100
                    'path' => $this->getPath(),
101
                    'port' => $this->getPort(),
102
                    'saslUser' => $this->getSaslUser() ?: null,
103
                    'saslPassword' => $this->getSaslPassword() ?: null,
104
                ],
105
            ];
106
        }
107
108
        return $this->servers;
109
    }
110
111
    /**
112
     * @param array $servers
113
     * @return self
114
     * @throws PhpfastcacheInvalidConfigurationException
115
     * @throws PhpfastcacheLogicException
116
     */
117
    public function setServers(array $servers): static
118
    {
119
        $this->enforceLockedProperty(__FUNCTION__);
120
        foreach ($servers as $server) {
121
            if ($diff = array_diff(array_keys($server), ['host', 'port', 'saslUser', 'saslPassword', 'path'])) {
122
                throw new PhpfastcacheInvalidConfigurationException('Unknown keys for memcached server: ' . implode(', ', $diff));
123
            }
124
125
            if (!empty($server['host']) && !empty($server['path'])) {
126
                throw new PhpfastcacheInvalidConfigurationException('Host and path cannot be simultaneous defined.');
127
            }
128
129
            if ((isset($server['host']) && !is_string($server['host'])) || (empty($server['path']) && empty($server['host']))) {
130
                throw new PhpfastcacheInvalidConfigurationException('Host must be a valid string in "$server" configuration array if path is not defined');
131
            }
132
133
            if ((isset($server['path']) && !is_string($server['path'])) || (empty($server['host']) && empty($server['path']))) {
134
                throw new PhpfastcacheInvalidConfigurationException('Path must be a valid string in "$server" configuration array if host is not defined');
135
            }
136
137
            if (!empty($server['host']) && (empty($server['port']) || !is_int($server['port'])|| $server['port'] < 1)) {
138
                throw new PhpfastcacheInvalidConfigurationException('Port must be a valid integer in "$server" configuration array');
139
            }
140
141
            if (!empty($server['port']) && !empty($server['path'])) {
142
                throw new PhpfastcacheInvalidConfigurationException('Port should not be defined along with path');
143
            }
144
145
            if (!empty($server['saslUser']) && !empty($server['saslPassword']) && (!is_string($server['saslUser']) || !is_string($server['saslPassword']))) {
146
                throw new PhpfastcacheInvalidConfigurationException('If provided, saslUser and saslPassword must be a string');
147
            }
148
        }
149
        $this->servers = $servers;
150
        return $this;
151
    }
152
153
    /**
154
     * @return string
155
     */
156
    public function getHost(): string
157
    {
158
        return $this->host;
159
    }
160
161
    /**
162
     * @param string $host
163
     * @return self
164
     * @throws PhpfastcacheLogicException
165
     */
166
    public function setHost(string $host): static
167
    {
168
        $this->enforceLockedProperty(__FUNCTION__);
169
        $this->host = $host;
170
        return $this;
171
    }
172
173
    /**
174
     * @return int
175
     */
176
    public function getPort(): int
177
    {
178
        return $this->port;
179
    }
180
181
    /**
182
     * @param int $port
183
     * @return Config
184
     * @throws PhpfastcacheLogicException
185
     */
186
    public function setPort(int $port): static
187
    {
188
        $this->enforceLockedProperty(__FUNCTION__);
189
        $this->port = $port;
190
        return $this;
191
    }
192
193
    /**
194
     * @return string
195
     * @since 8.0.2
196
     */
197
    public function getOptPrefix(): string
198
    {
199
        return $this->optPrefix;
200
    }
201
202
    /**
203
     * @param string $optPrefix
204
     * @return Config
205
     * @throws PhpfastcacheLogicException
206
     * @since 8.0.2
207
     */
208
    public function setOptPrefix(string $optPrefix): Config
209
    {
210
        $this->enforceLockedProperty(__FUNCTION__);
211
        $this->optPrefix = trim($optPrefix);
212
        return $this;
213
    }
214
}
215