Completed
Push — master ( bcd068...4006b8 )
by Georges
24s queued 13s
created

Config::setServers()   D

Complexity

Conditions 23
Paths 9

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 23
eloc 18
nc 9
nop 1
dl 0
loc 34
rs 4.1666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
118
     * @SuppressWarnings(PHPMD.NPathComplexity)
119
     */
120
    public function setServers(array $servers): static
121
    {
122
        $this->enforceLockedProperty(__FUNCTION__);
123
        foreach ($servers as $server) {
124
            if ($diff = array_diff(array_keys($server), ['host', 'port', 'saslUser', 'saslPassword', 'path'])) {
125
                throw new PhpfastcacheInvalidConfigurationException('Unknown keys for memcached server: ' . implode(', ', $diff));
126
            }
127
128
            if (!empty($server['host']) && !empty($server['path'])) {
129
                throw new PhpfastcacheInvalidConfigurationException('Host and path cannot be simultaneous defined.');
130
            }
131
132
            if ((isset($server['host']) && !is_string($server['host'])) || (empty($server['path']) && empty($server['host']))) {
133
                throw new PhpfastcacheInvalidConfigurationException('Host must be a valid string in "$server" configuration array if path is not defined');
134
            }
135
136
            if ((isset($server['path']) && !is_string($server['path'])) || (empty($server['host']) && empty($server['path']))) {
137
                throw new PhpfastcacheInvalidConfigurationException('Path must be a valid string in "$server" configuration array if host is not defined');
138
            }
139
140
            if (!empty($server['host']) && (empty($server['port']) || !is_int($server['port'])|| $server['port'] < 1)) {
141
                throw new PhpfastcacheInvalidConfigurationException('Port must be a valid integer in "$server" configuration array');
142
            }
143
144
            if (!empty($server['port']) && !empty($server['path'])) {
145
                throw new PhpfastcacheInvalidConfigurationException('Port should not be defined along with path');
146
            }
147
148
            if (!empty($server['saslUser']) && !empty($server['saslPassword']) && (!is_string($server['saslUser']) || !is_string($server['saslPassword']))) {
149
                throw new PhpfastcacheInvalidConfigurationException('If provided, saslUser and saslPassword must be a string');
150
            }
151
        }
152
        $this->servers = $servers;
153
        return $this;
154
    }
155
156
    /**
157
     * @return string
158
     */
159
    public function getHost(): string
160
    {
161
        return $this->host;
162
    }
163
164
    /**
165
     * @param string $host
166
     * @return self
167
     * @throws PhpfastcacheLogicException
168
     */
169
    public function setHost(string $host): static
170
    {
171
        $this->enforceLockedProperty(__FUNCTION__);
172
        $this->host = $host;
173
        return $this;
174
    }
175
176
    /**
177
     * @return int
178
     */
179
    public function getPort(): int
180
    {
181
        return $this->port;
182
    }
183
184
    /**
185
     * @param int $port
186
     * @return Config
187
     * @throws PhpfastcacheLogicException
188
     */
189
    public function setPort(int $port): static
190
    {
191
        $this->enforceLockedProperty(__FUNCTION__);
192
        $this->port = $port;
193
        return $this;
194
    }
195
196
    /**
197
     * @return string
198
     * @since 8.0.2
199
     */
200
    public function getOptPrefix(): string
201
    {
202
        return $this->optPrefix;
203
    }
204
205
    /**
206
     * @param string $optPrefix
207
     * @return Config
208
     * @throws PhpfastcacheLogicException
209
     * @since 8.0.2
210
     */
211
    public function setOptPrefix(string $optPrefix): Config
212
    {
213
        $this->enforceLockedProperty(__FUNCTION__);
214
        $this->optPrefix = trim($optPrefix);
215
        return $this;
216
    }
217
}
218