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

Config::setSaslPassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
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 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\Memcache;
17
18
use Phpfastcache\Config\ConfigurationOption;
19
use Phpfastcache\Exceptions\PhpfastcacheDriverException;
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 an 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
     *         ]
36
     *      ]);
37
     */
38
    protected array $servers = [];
39
40
    protected string $host = '127.0.0.1';
41
42
    protected int $port = 11211;
43
44
45
    /**
46
     * @return array
47
     */
48
    public function getServers(): array
49
    {
50
        if (!count($this->servers)) {
51
            return [
52
                [
53
                    'host' => $this->getHost(),
54
                    'path' => $this->getPath(),
55
                    'port' => $this->getPort(),
56
                ],
57
            ];
58
        }
59
60
        return $this->servers;
61
    }
62
63
    /**
64
     * @param array $servers
65
     * @return self
66
     * @throws PhpfastcacheInvalidConfigurationException
67
     * @throws PhpfastcacheLogicException
68
     */
69
    public function setServers(array $servers): static
70
    {
71
        $this->enforceLockedProperty(__FUNCTION__);
72
        foreach ($servers as $server) {
73
            if (\array_key_exists('saslUser', $server) || array_key_exists('saslPassword', $server)) {
74
                throw new PhpfastcacheInvalidConfigurationException('Unlike Memcached, Memcache does not support SASL authentication');
75
            }
76
77
            if ($diff = array_diff(array_keys($server), ['host', 'port', 'path'])) {
78
                throw new PhpfastcacheInvalidConfigurationException('Unknown keys for memcached server: ' . implode(', ', $diff));
79
            }
80
81
            if (!empty($server['host']) && !empty($server['path'])) {
82
                throw new PhpfastcacheInvalidConfigurationException('Host and path cannot be simultaneous defined.');
83
            }
84
85
            if ((isset($server['host']) && !is_string($server['host'])) || (empty($server['path']) && empty($server['host']))) {
86
                throw new PhpfastcacheInvalidConfigurationException('Host must be a valid string in "$server" configuration array if path is not defined');
87
            }
88
89
            if ((isset($server['path']) && !is_string($server['path'])) || (empty($server['host']) && empty($server['path']))) {
90
                throw new PhpfastcacheInvalidConfigurationException('Path must be a valid string in "$server" configuration array if host is not defined');
91
            }
92
93
            if (!empty($server['host']) && (empty($server['port']) || !is_int($server['port'])|| $server['port'] < 1)) {
94
                throw new PhpfastcacheInvalidConfigurationException('Port must be a valid integer in "$server" configuration array');
95
            }
96
97
            if (!empty($server['port']) && !empty($server['path'])) {
98
                throw new PhpfastcacheInvalidConfigurationException('Port should not be defined along with path');
99
            }
100
        }
101
        $this->servers = $servers;
102
        return $this;
103
    }
104
105
    /**
106
     * @return string
107
     */
108
    public function getHost(): string
109
    {
110
        return $this->host;
111
    }
112
113
    /**
114
     * @param string $host
115
     * @return self
116
     * @throws PhpfastcacheLogicException
117
     */
118
    public function setHost(string $host): static
119
    {
120
        $this->enforceLockedProperty(__FUNCTION__);
121
        $this->host = $host;
122
123
        return $this;
124
    }
125
126
    /**
127
     * @return int
128
     */
129
    public function getPort(): int
130
    {
131
        return $this->port;
132
    }
133
134
    /**
135
     * @param int $port
136
     * @return self
137
     * @throws PhpfastcacheLogicException
138
     */
139
    public function setPort(int $port): static
140
    {
141
        $this->enforceLockedProperty(__FUNCTION__);
142
        $this->port = $port;
143
        return $this;
144
    }
145
}
146