Passed
Pull Request — master (#53)
by kacper
04:09
created

Config::__construct()   A

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