Completed
Push — master ( 980e9f...0f4ec9 )
by Georges
23s queued 12s
created

Config::setOptPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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 file.
9
 *
10
 * @author Khoa Bui (khoaofgod)  <[email protected]> https://www.phpfastcache.com
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 *
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
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
     *           'host' => '127.0.0.1',
32
     *           'port' => 11211,
33
     *           'saslUser' => false,
34
     *           'saslPassword' => false,
35
     *         ]
36
     *      ]);
37
     */
38
    protected $servers = [];
39
40
    /**
41
     * @var string
42
     */
43
    protected $host = '127.0.0.1';
44
45
    /**
46
     * @var int
47
     */
48
    protected $port = 11211;
49
50
    /**
51
     * @var string
52
     */
53
    protected $saslUser = '';
54
55
    /**
56
     * @var string
57
     */
58
    protected $saslPassword = '';
59
60
    /**
61
     * @var string
62
     */
63
    protected $optPrefix = '';
64
65
    /**
66
     * @return string
67
     */
68
    public function getSaslUser(): string
69
    {
70
        return $this->saslUser;
71
    }
72
73
    /**
74
     * @param string $saslUser
75
     * @return self
76
     */
77
    public function setSaslUser(string $saslUser): self
78
    {
79
        $this->saslUser = $saslUser;
80
        return $this;
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function getSaslPassword(): string
87
    {
88
        return $this->saslPassword;
89
    }
90
91
    /**
92
     * @param string $saslPassword
93
     * @return self
94
     */
95
    public function setSaslPassword(string $saslPassword): self
96
    {
97
        $this->saslPassword = $saslPassword;
98
        return $this;
99
    }
100
101
    /**
102
     * @return array
103
     */
104
    public function getServers(): array
105
    {
106
        return $this->servers;
107
    }
108
109
    /**
110
     * @param array $servers
111
     * @return self
112
     * @throws PhpfastcacheInvalidConfigurationException
113
     */
114
    public function setServers(array $servers): self
115
    {
116
        foreach ($servers as $server) {
117
            if ($diff = array_diff(['host', 'port', 'saslUser', 'saslPassword'], array_keys($server))) {
118
                throw new PhpfastcacheInvalidConfigurationException('Missing keys for memcached server: ' . implode(', ', $diff));
119
            }
120
            if ($diff = array_diff(array_keys($server), ['host', 'port', 'saslUser', 'saslPassword'])) {
121
                throw new PhpfastcacheInvalidConfigurationException('Unknown keys for memcached server: ' . implode(', ', $diff));
122
            }
123
            if (!is_string($server['host'])) {
124
                throw new PhpfastcacheInvalidConfigurationException('Host must be a valid string in "$server" configuration array');
125
            }
126
            if (!is_int($server['port'])) {
127
                throw new PhpfastcacheInvalidConfigurationException('Port must be a valid integer in "$server" configuration array');
128
            }
129
        }
130
        $this->servers = $servers;
131
        return $this;
132
    }
133
134
    /**
135
     * @return string
136
     */
137
    public function getHost(): string
138
    {
139
        return $this->host;
140
    }
141
142
    /**
143
     * @param string $host
144
     * @return self
145
     */
146
    public function setHost(string $host): self
147
    {
148
        $this->host = $host;
149
        return $this;
150
    }
151
152
    /**
153
     * @return int
154
     */
155
    public function getPort(): int
156
    {
157
        return $this->port;
158
    }
159
160
    /**
161
     * @param int $port
162
     * @return Config
163
     */
164
    public function setPort(int $port): self
165
    {
166
        $this->port = $port;
167
        return $this;
168
    }
169
170
    /**
171
     * @return string
172
     * @since 8.0.2
173
     */
174
    public function getOptPrefix(): string
175
    {
176
        return $this->optPrefix;
177
    }
178
179
    /**
180
     * @param string $optPrefix
181
     * @return Config
182
     * @since 8.0.2
183
     */
184
    public function setOptPrefix(string $optPrefix): Config
185
    {
186
        $this->optPrefix = trim($optPrefix);
187
        return $this;
188
    }
189
}