Passed
Branch master (a6a24c)
by kacper
04:55
created

Config::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 17
dl 0
loc 36
ccs 18
cts 18
cp 1
crap 1
rs 8.8571
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
3
namespace MySQLReplication\Config;
4
5
/**
6
 * Class Config
7
 * @package MySQLReplication\Config
8
 */
9
class Config
10
{
11
    /**
12
     * @var string
13
     */
14
    private static $user;
15
    /**
16
     * @var string
17
     */
18
    private static $host;
19
    /**
20
     * @var int
21
     */
22
    private static $port;
23
    /**
24
     * @var string
25
     */
26
    private static $password;
27
    /**
28
     * @var string
29
     */
30
    private static $charset;
31
    /**
32
     * @var string
33
     */
34
    private static $gtid;
35
    /**
36
     * @var int
37
     */
38
    private static $slaveId;
39
    /**
40
     * @var string
41
     */
42
    private static $binLogFileName;
43
    /**
44
     * @var int
45
     */
46
    private static $binLogPosition;
47
    /**
48
     * @var array
49
     */
50
    private static $eventsOnly;
51
    /**
52
     * @var array
53
     */
54
    private static $eventsIgnore;
55
    /**
56
     * @var array
57
     */
58
    private static $tablesOnly;
59
    /**
60
     * @var array
61
     */
62
    private static $databasesOnly;
63
    /**
64
     * @var string
65
     */
66
    private static $mariaDbGtid;
67
    /**
68
     * @var int
69
     */
70
    private static $tableCacheSize;
71
    /**
72
     * @var array
73
     */
74
    private static $custom;
75
    /**
76
     * @var int
77
     */
78
    private static $heartbeatPeriod;
79
80
    /**
81
     * Config constructor.
82
     * @param string $user
83
     * @param string $host
84
     * @param int $port
85
     * @param string $password
86
     * @param string $charset
87
     * @param string $gtid
88
     * @param string $mariaGtid
89
     * @param int $slaveId
90
     * @param string $binLogFileName
91
     * @param $binLogPosition
92
     * @param array $eventsOnly
93
     * @param array $eventsIgnore
94
     * @param array $tablesOnly
95
     * @param array $databasesOnly
96
     * @param int $tableCacheSize
97
     * @param array $custom
98
     * @param int $heartbeatPeriod
99
     */
100 54
    public function __construct(
101
        $user,
102
        $host,
103
        $port,
104
        $password,
105
        $charset,
106
        $gtid,
107
        $mariaGtid,
108
        $slaveId,
109
        $binLogFileName,
110
        $binLogPosition,
111
        array $eventsOnly,
112
        array $eventsIgnore,
113
        array $tablesOnly,
114
        array $databasesOnly,
115
        $tableCacheSize,
116
        array $custom,
117
        $heartbeatPeriod
118
    ) {
119 54
        self::$user = $user;
120 54
        self::$host = $host;
121 54
        self::$port = $port;
122 54
        self::$password = $password;
123 54
        self::$charset = $charset;
124 54
        self::$gtid = $gtid;
125 54
        self::$slaveId = $slaveId;
126 54
        self::$binLogFileName = $binLogFileName;
127 54
        self::$binLogPosition = $binLogPosition;
128 54
        self::$eventsOnly = $eventsOnly;
129 54
        self::$eventsIgnore = $eventsIgnore;
130 54
        self::$tablesOnly = $tablesOnly;
131 54
        self::$databasesOnly = $databasesOnly;
132 54
        self::$mariaDbGtid = $mariaGtid;
133 54
        self::$tableCacheSize = $tableCacheSize;
134 54
        self::$custom = $custom;
135 54
        self::$heartbeatPeriod = $heartbeatPeriod;
136 54
    }
137
138
    /**
139
     * @throws ConfigException
140
     */
141 54
    public static function validate()
142
    {
143 54
        if (!empty(self::$user) && !is_string(self::$user)) {
144
            throw new ConfigException(ConfigException::USER_ERROR_MESSAGE, ConfigException::USER_ERROR_CODE);
145
        }
146 54
        if (!empty(self::$host)) {
147 54
            $ip = gethostbyname(self::$host);
148 54
            if (false === filter_var($ip, FILTER_VALIDATE_IP)) {
149
                throw new ConfigException(ConfigException::IP_ERROR_MESSAGE, ConfigException::IP_ERROR_CODE);
150
            }
151 54
        }
152 54
        if (!empty(self::$port) && false === filter_var(
153 54
                self::$port, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]]
154 54
            )
155 54
        ) {
156
            throw new ConfigException(ConfigException::PORT_ERROR_MESSAGE, ConfigException::PORT_ERROR_CODE);
157
        }
158 54
        if (!empty(self::$password) && !is_string(self::$password) && !is_numeric(self::$password)) {
159
            throw new ConfigException(ConfigException::PASSWORD_ERROR_MESSAGE, ConfigException::PASSWORD_ERROR_CODE);
160
        }
161 54
        if (!empty(self::$charset) && !is_string(self::$charset)) {
162
            throw new ConfigException(ConfigException::CHARSET_ERROR_MESSAGE, ConfigException::CHARSET_ERROR_CODE);
163
        }
164 54
        if (!empty(self::$gtid) && !is_string(self::$gtid)) {
165
            foreach (explode(',', self::$gtid) as $gtid) {
166
                if (!(bool)preg_match(
167
                    '/^([0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12})((?::[0-9-]+)+)$/', $gtid, $matches
168
                )
169
                ) {
170
                    throw new ConfigException(ConfigException::GTID_ERROR_MESSAGE, ConfigException::GTID_ERROR_CODE);
171
                }
172
            }
173
        }
174 54
        if (!empty(self::$slaveId) && false === filter_var(self::$slaveId, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])
175 54
        ) {
176
            throw new ConfigException(ConfigException::SLAVE_ID_ERROR_MESSAGE, ConfigException::SLAVE_ID_ERROR_CODE);
177
        }
178 54
        if (!empty(self::$binLogFileName) && !is_string(self::$binLogFileName)) {
179
            throw new ConfigException(
180
                ConfigException::BIN_LOG_FILE_NAME_ERROR_MESSAGE, ConfigException::BIN_LOG_FILE_NAME_ERROR_CODE
181
            );
182
        }
183 54
        if (false === filter_var(self::$binLogPosition, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) {
184
            throw new ConfigException(
185
                ConfigException::BIN_LOG_FILE_POSITION_ERROR_MESSAGE, ConfigException::BIN_LOG_FILE_POSITION_ERROR_CODE
186
            );
187
        }
188
189 54
        if (!empty(self::$mariaDbGtid) && !is_string(self::$mariaDbGtid)) {
190
            throw new ConfigException(
191
                ConfigException::MARIADBGTID_ERROR_MESSAGE, ConfigException::MARIADBGTID_ERROR_CODE
192
            );
193
        }
194 54
        if (false === filter_var(self::$tableCacheSize, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) {
195
            throw new ConfigException(
196
                ConfigException::TABLE_CACHE_SIZE_ERROR_MESSAGE, ConfigException::TABLE_CACHE_SIZE_ERROR_CODE
197
            );
198
        }
199 54
        if (0 !== self::$heartbeatPeriod && false === filter_var(
200
                self::$heartbeatPeriod, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => 4294967]]
201
            )
202 54
        ) {
203
            throw new ConfigException(
204
                ConfigException::HEARTBEAT_PERIOD_ERROR_MESSAGE, ConfigException::HEARTBEAT_PERIOD_ERROR_CODE
205
            );
206
        }
207 54
    }
208
209
    /**
210
     * @return array
211
     */
212
    public static function getCustom()
213
    {
214
        return self::$custom;
215
    }
216
217
    /**
218
     * @return string
219
     */
220 54
    public static function getUser()
221
    {
222 54
        return self::$user;
223
    }
224
225
    /**
226
     * @return string
227
     */
228 54
    public static function getHost()
229
    {
230 54
        return self::$host;
231
    }
232
233
    /**
234
     * @return int
235
     */
236 54
    public static function getPort()
237
    {
238 54
        return self::$port;
239
    }
240
241
    /**
242
     * @return string
243
     */
244 54
    public static function getPassword()
245
    {
246 54
        return self::$password;
247
    }
248
249
    /**
250
     * @return string
251
     */
252 54
    public static function getCharset()
253
    {
254 54
        return self::$charset;
255
    }
256
257
    /**
258
     * @return string
259
     */
260 54
    public static function getGtid()
261
    {
262 54
        return self::$gtid;
263
    }
264
265
    /**
266
     * @return string
267
     */
268 54
    public static function getMariaDbGtid()
269
    {
270 54
        return self::$mariaDbGtid;
271
    }
272
273
    /**
274
     * @return int
275
     */
276 54
    public static function getSlaveId()
277
    {
278 54
        return self::$slaveId;
279
    }
280
281
    /**
282
     * @return string
283
     */
284 54
    public static function getBinLogFileName()
285
    {
286 54
        return self::$binLogFileName;
287
    }
288
289
    /**
290
     * @return int
291
     */
292 54
    public static function getBinLogPosition()
293
    {
294 54
        return self::$binLogPosition;
295
    }
296
297
    /**
298
     * @return array
299
     */
300 54
    public static function getEventsOnly()
301
    {
302 54
        return self::$eventsOnly;
303
    }
304
305
    /**
306
     * @return array
307
     */
308 54
    public static function getEventsIgnore()
309
    {
310 54
        return self::$eventsIgnore;
311
    }
312
313
    /**
314
     * @return array
315
     */
316 52
    public static function getTablesOnly()
317
    {
318 52
        return self::$tablesOnly;
319
    }
320
321
    /**
322
     * @return array
323
     */
324 52
    public static function getDatabasesOnly()
325
    {
326 52
        return self::$databasesOnly;
327
    }
328
329
    /**
330
     * @return int
331
     */
332 52
    public static function getTableCacheSize()
333
    {
334 52
        return self::$tableCacheSize;
335
    }
336
337
    /**
338
     * @param string $database
339
     * @return bool
340
     */
341 52
    public static function checkDataBasesOnly($database)
342
    {
343 52
        return [] !== Config::getDatabasesOnly() && !in_array($database, Config::getDatabasesOnly(), true);
344
    }
345
346
    /**
347
     * @param string $table
348
     * @return bool
349
     */
350 52
    public static function checkTablesOnly($table)
351
    {
352 52
        return [] !== Config::getTablesOnly() && !in_array($table, Config::getTablesOnly(), true);
353
    }
354
355
    /**
356
     * @param int $type
357
     * @return bool
358
     */
359 54
    public static function checkEvent($type)
360
    {
361 54
        if ([] !== Config::getEventsOnly() && !in_array($type, Config::getEventsOnly(), true)) {
362 3
            return false;
363
        }
364
365 54
        if (in_array($type, Config::getEventsIgnore(), true)) {
366
            return false;
367
        }
368
369 54
        return true;
370
    }
371
372
    /**
373
     * @return int
374
     */
375 54
    public static function getHeartbeatPeriod()
376
    {
377 54
        return self::$heartbeatPeriod;
378
    }
379
}