Passed
Branch master (c44613)
by Moln
05:00
created

Config::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 18
dl 0
loc 38
ccs 19
cts 19
cp 1
crap 1
rs 9.6666
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
declare(strict_types=1);
3
4
namespace MySQLReplication\Config;
5
6
use JsonSerializable;
7
8
class Config implements JsonSerializable
9
{
10
    private $user;
11
    private $host;
12
    private $port;
13
    private $password;
14
    private $charset;
15
    private $gtid;
16
    private $slaveId;
17
    private $binLogFileName;
18
    private $binLogPosition;
19
    private $eventsOnly;
20
    private $eventsIgnore;
21
    private $tablesOnly;
22
    private $databasesOnly;
23
    private $mariaDbGtid;
24
    private $tableCacheSize;
25
    private $custom;
26
    private $heartbeatPeriod;
27
    private $retry;
28
29 79
    public function __construct(
30
        string $user,
31
        string $host,
32
        int $port,
33
        string $password,
34
        string $charset,
35
        string $gtid,
36
        string $mariaGtid,
37
        int $slaveId,
38
        string $binLogFileName,
39
        int $binLogPosition,
40
        array $eventsOnly,
41
        array $eventsIgnore,
42
        array $tablesOnly,
43
        array $databasesOnly,
44
        int $tableCacheSize,
45
        array $custom,
46
        float $heartbeatPeriod,
47
        int $retry = 0
48
    ) {
49 79
        $this->user = $user;
50 79
        $this->host = $host;
51 79
        $this->port = $port;
52 79
        $this->password = $password;
53 79
        $this->charset = $charset;
54 79
        $this->gtid = $gtid;
55 79
        $this->slaveId = $slaveId;
56 79
        $this->binLogFileName = $binLogFileName;
57 79
        $this->binLogPosition = $binLogPosition;
58 79
        $this->eventsOnly = $eventsOnly;
59 79
        $this->eventsIgnore = $eventsIgnore;
60 79
        $this->tablesOnly = $tablesOnly;
61 79
        $this->databasesOnly = $databasesOnly;
62 79
        $this->mariaDbGtid = $mariaGtid;
63 79
        $this->tableCacheSize = $tableCacheSize;
64 79
        $this->custom = $custom;
65 79
        $this->heartbeatPeriod = $heartbeatPeriod;
66 79
        $this->retry = $retry;
67
    }
68
69
    /**
70
     * @throws ConfigException
71
     */
72 75
    public function validate(): void
73
    {
74 75
        if (!empty($this->host)) {
75 75
            $ip = gethostbyname($this->host);
76 75
            if (false === filter_var($ip, FILTER_VALIDATE_IP)) {
77 1
                throw new ConfigException(ConfigException::IP_ERROR_MESSAGE, ConfigException::IP_ERROR_CODE);
78
            }
79
        }
80 74
        if (!empty($this->port) && false === filter_var(
81 74
                $this->port, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]]
82
            )) {
83 1
            throw new ConfigException(ConfigException::PORT_ERROR_MESSAGE, ConfigException::PORT_ERROR_CODE);
84
        }
85 73
        if (!empty($this->gtid)) {
86 1
            foreach (explode(',', $this->gtid) as $gtid) {
87 1
                if (!(bool)preg_match(
88
                    '/^([0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12})((?::[0-9-]+)+)$/', $gtid, $matches
89
                )) {
90 1
                    throw new ConfigException(ConfigException::GTID_ERROR_MESSAGE, ConfigException::GTID_ERROR_CODE);
91
                }
92
            }
93
        }
94 72
        if (!empty($this->slaveId) && false === filter_var(
95 72
                $this->slaveId, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]]
96
            )) {
97 1
            throw new ConfigException(ConfigException::SLAVE_ID_ERROR_MESSAGE, ConfigException::SLAVE_ID_ERROR_CODE);
98
        }
99 71
        if (false === filter_var($this->binLogPosition, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) {
100 1
            throw new ConfigException(
101
                ConfigException::BIN_LOG_FILE_POSITION_ERROR_MESSAGE, ConfigException::BIN_LOG_FILE_POSITION_ERROR_CODE
102
            );
103
        }
104 70
        if (false === filter_var($this->tableCacheSize, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) {
105 1
            throw new ConfigException(
106
                ConfigException::TABLE_CACHE_SIZE_ERROR_MESSAGE, ConfigException::TABLE_CACHE_SIZE_ERROR_CODE
107
            );
108
        }
109 69
        if (0.0 !== $this->heartbeatPeriod && false === (
110 69
                $this->heartbeatPeriod >= 0.001 && $this->heartbeatPeriod <= 4294967.0
111
            )) {
112 2
            throw new ConfigException(
113
                ConfigException::HEARTBEAT_PERIOD_ERROR_MESSAGE, ConfigException::HEARTBEAT_PERIOD_ERROR_CODE
114
            );
115
        }
116
    }
117
118
    public function getCustom(): array
119
    {
120
        return $this->custom;
121
    }
122
123 62
    public function getUser(): string
124
    {
125 62
        return $this->user;
126
    }
127
128 62
    public function getHost(): string
129
    {
130 62
        return $this->host;
131
    }
132
133 62
    public function getPort(): int
134
    {
135 62
        return $this->port;
136
    }
137
138 62
    public function getPassword(): string
139
    {
140 62
        return $this->password;
141
    }
142
143 62
    public function getCharset(): string
144
    {
145 62
        return $this->charset;
146
    }
147
148 62
    public function getGtid(): string
149
    {
150 62
        return $this->gtid;
151
    }
152
153 62
    public function getMariaDbGtid(): string
154
    {
155 62
        return $this->mariaDbGtid;
156
    }
157
158 62
    public function getSlaveId(): int
159
    {
160 62
        return $this->slaveId;
161
    }
162
163 62
    public function getBinLogFileName(): string
164
    {
165 62
        return $this->binLogFileName;
166
    }
167
168 62
    public function getBinLogPosition(): int
169
    {
170 62
        return $this->binLogPosition;
171
    }
172
173 62
    public function getTableCacheSize(): int
174
    {
175 62
        return $this->tableCacheSize;
176
    }
177
178 59
    public function checkDataBasesOnly(string $database): bool
179
    {
180 59
        return [] !== $this->getDatabasesOnly() && !in_array($database, $this->getDatabasesOnly(), true);
181
    }
182
183 59
    public function getDatabasesOnly(): array
184
    {
185 59
        return $this->databasesOnly;
186
    }
187
188 59
    public function checkTablesOnly(string $table): bool
189
    {
190 59
        return [] !== $this->getTablesOnly() && !in_array($table, $this->getTablesOnly(), true);
191
    }
192
193 59
    public function getTablesOnly(): array
194
    {
195 59
        return $this->tablesOnly;
196
    }
197
198 63
    public function checkEvent(int $type): bool
199
    {
200 63
        if ([] !== $this->getEventsOnly() && !in_array($type, $this->getEventsOnly(), true)) {
201 5
            return false;
202
        }
203
204 63
        if (in_array($type, $this->getEventsIgnore(), true)) {
205 1
            return false;
206
        }
207
208 63
        return true;
209
    }
210
211 63
    public function getEventsOnly(): array
212
    {
213 63
        return $this->eventsOnly;
214
    }
215
216 63
    public function getEventsIgnore(): array
217
    {
218 63
        return $this->eventsIgnore;
219
    }
220
221 67
    public function getHeartbeatPeriod(): float
222
    {
223 67
        return $this->heartbeatPeriod;
224
    }
225
226
    public function getRetry(): int
227
    {
228
        return $this->retry;
229
    }
230
231
    public function jsonSerialize()
232
    {
233
        return get_object_vars($this);
234
    }
235
}