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

Config::setServers()   D

Complexity

Conditions 21
Paths 9

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
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\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
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
70
     * @SuppressWarnings(PHPMD.NPathComplexity)
71
     */
72
    public function setServers(array $servers): static
73
    {
74
        $this->enforceLockedProperty(__FUNCTION__);
75
        foreach ($servers as $server) {
76
            if (\array_key_exists('saslUser', $server) || array_key_exists('saslPassword', $server)) {
77
                throw new PhpfastcacheInvalidConfigurationException('Unlike Memcached, Memcache does not support SASL authentication');
78
            }
79
80
            if ($diff = array_diff(array_keys($server), ['host', 'port', 'path'])) {
81
                throw new PhpfastcacheInvalidConfigurationException('Unknown keys for memcached server: ' . implode(', ', $diff));
82
            }
83
84
            if (!empty($server['host']) && !empty($server['path'])) {
85
                throw new PhpfastcacheInvalidConfigurationException('Host and path cannot be simultaneous defined.');
86
            }
87
88
            if ((isset($server['host']) && !is_string($server['host'])) || (empty($server['path']) && empty($server['host']))) {
89
                throw new PhpfastcacheInvalidConfigurationException('Host must be a valid string in "$server" configuration array if path is not defined');
90
            }
91
92
            if ((isset($server['path']) && !is_string($server['path'])) || (empty($server['host']) && empty($server['path']))) {
93
                throw new PhpfastcacheInvalidConfigurationException('Path must be a valid string in "$server" configuration array if host is not defined');
94
            }
95
96
            if (!empty($server['host']) && (empty($server['port']) || !is_int($server['port'])|| $server['port'] < 1)) {
97
                throw new PhpfastcacheInvalidConfigurationException('Port must be a valid integer in "$server" configuration array');
98
            }
99
100
            if (!empty($server['port']) && !empty($server['path'])) {
101
                throw new PhpfastcacheInvalidConfigurationException('Port should not be defined along with path');
102
            }
103
        }
104
        $this->servers = $servers;
105
        return $this;
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    public function getHost(): string
112
    {
113
        return $this->host;
114
    }
115
116
    /**
117
     * @param string $host
118
     * @return self
119
     * @throws PhpfastcacheLogicException
120
     */
121
    public function setHost(string $host): static
122
    {
123
        $this->enforceLockedProperty(__FUNCTION__);
124
        $this->host = $host;
125
126
        return $this;
127
    }
128
129
    /**
130
     * @return int
131
     */
132
    public function getPort(): int
133
    {
134
        return $this->port;
135
    }
136
137
    /**
138
     * @param int $port
139
     * @return self
140
     * @throws PhpfastcacheLogicException
141
     */
142
    public function setPort(int $port): static
143
    {
144
        $this->enforceLockedProperty(__FUNCTION__);
145
        $this->port = $port;
146
        return $this;
147
    }
148
}
149