Passed
Pull Request — master (#8)
by Moln
05:05
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 75
    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 75
        $this->user = $user;
50 75
        $this->host = $host;
51 75
        $this->port = $port;
52 75
        $this->password = $password;
53 75
        $this->charset = $charset;
54 75
        $this->gtid = $gtid;
55 75
        $this->slaveId = $slaveId;
56 75
        $this->binLogFileName = $binLogFileName;
57 75
        $this->binLogPosition = $binLogPosition;
58 75
        $this->eventsOnly = $eventsOnly;
59 75
        $this->eventsIgnore = $eventsIgnore;
60 75
        $this->tablesOnly = $tablesOnly;
61 75
        $this->databasesOnly = $databasesOnly;
62 75
        $this->mariaDbGtid = $mariaGtid;
63 75
        $this->tableCacheSize = $tableCacheSize;
64 75
        $this->custom = $custom;
65 75
        $this->heartbeatPeriod = $heartbeatPeriod;
66 75
        $this->retry = $retry;
67
    }
68
69
    /**
70
     * @throws ConfigException
71
     */
72 71
    public function validate(): void
73
    {
74 71
        if (! empty($this->host)) {
75 71
            $ip = gethostbyname($this->host);
76 71
            if (false === filter_var($ip, FILTER_VALIDATE_IP)) {
77 1
                throw new ConfigException(ConfigException::IP_ERROR_MESSAGE, ConfigException::IP_ERROR_CODE);
78
            }
79
        }
80 70
        if (! empty($this->port) && false === filter_var(
81 70
            $this->port,
82 70
            FILTER_VALIDATE_INT,
83 70
            ['options' => ['min_range' => 0]]
84 70
        )) {
85 1
            throw new ConfigException(ConfigException::PORT_ERROR_MESSAGE, ConfigException::PORT_ERROR_CODE);
86
        }
87 69
        if (! empty($this->gtid)) {
88 1
            foreach (explode(',', $this->gtid) as $gtid) {
89 1
                if (! (bool)preg_match(
90 1
                    '/^([0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12})((?::[0-9-]+)+)$/',
91 1
                    $gtid,
92 1
                    $matches
93 1
                )) {
94 1
                    throw new ConfigException(ConfigException::GTID_ERROR_MESSAGE, ConfigException::GTID_ERROR_CODE);
95
                }
96
            }
97
        }
98 68
        if (! empty($this->slaveId) && false === filter_var(
99 68
            $this->slaveId,
100 68
            FILTER_VALIDATE_INT,
101 68
            ['options' => ['min_range' => 0]]
102 68
        )) {
103 1
            throw new ConfigException(ConfigException::SLAVE_ID_ERROR_MESSAGE, ConfigException::SLAVE_ID_ERROR_CODE);
104
        }
105 67
        if (false === filter_var($this->binLogPosition, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) {
106 1
            throw new ConfigException(
107 1
                ConfigException::BIN_LOG_FILE_POSITION_ERROR_MESSAGE,
108 1
                ConfigException::BIN_LOG_FILE_POSITION_ERROR_CODE
109 1
            );
110
        }
111 66
        if (false === filter_var($this->tableCacheSize, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) {
112 1
            throw new ConfigException(
113 1
                ConfigException::TABLE_CACHE_SIZE_ERROR_MESSAGE,
114 1
                ConfigException::TABLE_CACHE_SIZE_ERROR_CODE
115 1
            );
116
        }
117 65
        if (0.0 !== $this->heartbeatPeriod && false === (
118 65
                $this->heartbeatPeriod >= 0.001 && $this->heartbeatPeriod <= 4294967.0
119
            )) {
120 2
            throw new ConfigException(
121 2
                ConfigException::HEARTBEAT_PERIOD_ERROR_MESSAGE,
122 2
                ConfigException::HEARTBEAT_PERIOD_ERROR_CODE
123 2
            );
124
        }
125
    }
126
127
    public function getCustom(): array
128
    {
129
        return $this->custom;
130
    }
131
132 58
    public function getUser(): string
133
    {
134 58
        return $this->user;
135
    }
136
137 58
    public function getHost(): string
138
    {
139 58
        return $this->host;
140
    }
141
142 58
    public function getPort(): int
143
    {
144 58
        return $this->port;
145
    }
146
147 58
    public function getPassword(): string
148
    {
149 58
        return $this->password;
150
    }
151
152 58
    public function getCharset(): string
153
    {
154 58
        return $this->charset;
155
    }
156
157 58
    public function getGtid(): string
158
    {
159 58
        return $this->gtid;
160
    }
161
162 58
    public function getMariaDbGtid(): string
163
    {
164 58
        return $this->mariaDbGtid;
165
    }
166
167 58
    public function getSlaveId(): int
168
    {
169 58
        return $this->slaveId;
170
    }
171
172 58
    public function getBinLogFileName(): string
173
    {
174 58
        return $this->binLogFileName;
175
    }
176
177 58
    public function getBinLogPosition(): int
178
    {
179 58
        return $this->binLogPosition;
180
    }
181
182 58
    public function getTableCacheSize(): int
183
    {
184 58
        return $this->tableCacheSize;
185
    }
186
187 56
    public function checkDataBasesOnly(string $database): bool
188
    {
189 56
        return [] !== $this->getDatabasesOnly() && ! self::matchNames($database, $this->getDatabasesOnly());
190
    }
191
192 56
    public function getDatabasesOnly(): array
193
    {
194 56
        return $this->databasesOnly;
195
    }
196
197 55
    public function checkTablesOnly(string $table): bool
198
    {
199 55
        return [] !== $this->getTablesOnly() && ! self::matchNames($table, $this->getTablesOnly());
200
    }
201
202 3
    private static function matchNames(string $subject, array $names): bool
203
    {
204 3
        foreach ($names as $name) {
205 3
            if (preg_match("/$name/", $subject)) {
206 3
                return true;
207
            }
208
        }
209
210 3
        return false;
211
    }
212
213 55
    public function getTablesOnly(): array
214
    {
215 55
        return $this->tablesOnly;
216
    }
217
218 59
    public function checkEvent(int $type): bool
219
    {
220 59
        if ([] !== $this->getEventsOnly() && ! in_array($type, $this->getEventsOnly(), true)) {
221 5
            return false;
222
        }
223
224 59
        if (in_array($type, $this->getEventsIgnore(), true)) {
225 1
            return false;
226
        }
227
228 59
        return true;
229
    }
230
231 59
    public function getEventsOnly(): array
232
    {
233 59
        return $this->eventsOnly;
234
    }
235
236 59
    public function getEventsIgnore(): array
237
    {
238 59
        return $this->eventsIgnore;
239
    }
240
241 63
    public function getHeartbeatPeriod(): float
242
    {
243 63
        return $this->heartbeatPeriod;
244
    }
245
246
    public function getRetry(): int
247
    {
248
        return $this->retry;
249
    }
250
251
    #[\ReturnTypeWillChange]
252
    public function jsonSerialize()
253
    {
254
        return get_object_vars($this);
255
    }
256
}
257