Config::setSaslUser()   A
last analyzed

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