Completed
Push — master ( 15c40a...010522 )
by kacper
01:57
created

Config::getIp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace MySQLReplication\Config;
4
5
use MySQLReplication\Config\Exception\ConfigException;
6
7
/**
8
 * Class Config
9
 * @package MySQLReplication\Config
10
 */
11
class Config
12
{
13
    /**
14
     * @var string
15
     */
16
    private $user;
17
    /**
18
     * @var string
19
     */
20
    private $host;
21
    /**
22
     * @var int
23
     */
24
    private $port;
25
    /**
26
     * @var string
27
     */
28
    private $password;
29
    /**
30
     * @var string
31
     */
32
    private $dbName;
33
    /**
34
     * @var string
35
     */
36
    private $charset;
37
    /**
38
     * @var string
39
     */
40
    private $gtid;
41
    /**
42
     * @var int
43
     */
44
    private $slaveId;
45
    /**
46
     * @var string
47
     */
48
    private $binLogFileName;
49
    /**
50
     * @var int
51
     */
52
    private $binLogPosition;
53
    /**
54
     * @var array
55
     */
56
    private $eventsOnly;
57
    /**
58
     * @var array
59
     */
60
    private $eventsIgnore;
61
    /**
62
     * @var array
63
     */
64
    private $tablesOnly;
65
    /**
66
     * @var array
67
     */
68
    private $databasesOnly;
69
    /**
70
     * @var string
71
     */
72
    private $mariaDbGtid;
73
74
    /**
75
     * Config constructor.
76
     * @param string $user
77
     * @param string $host
78
     * @param int $port
79
     * @param string $password
80
     * @param string $dbName
81
     * @param string $charset
82
     * @param string $gtid
83
     * @param string $mariaGtid
84
     * @param int $slaveId
85
     * @param string $binLogFileName
86
     * @param $binLogPosition
87
     * @param array $eventsOnly
88
     * @param array $eventsIgnore
89
     * @param array $tablesOnly
90
     * @param array $databasesOnly
91
     */
92
    public function __construct(
93
        $user,
94
        $host,
95
        $port,
96
        $password,
97
        $dbName,
98
        $charset,
99
        $gtid,
100
        $mariaGtid,
101
        $slaveId,
102
        $binLogFileName,
103
        $binLogPosition,
104
        array $eventsOnly,
105
        array $eventsIgnore,
106
        array $tablesOnly,
107
        array $databasesOnly
108
    ) {
109
        $this->user = $user;
110
        $this->host = $host;
111
        $this->port = $port;
112
        $this->password = $password;
113
        $this->dbName = $dbName;
114
        $this->charset = $charset;
115
        $this->gtid = $gtid;
116
        $this->slaveId = $slaveId;
117
        $this->binLogFileName = $binLogFileName;
118
        $this->binLogPosition = $binLogPosition;
119
        $this->eventsOnly = $eventsOnly;
120
        $this->eventsIgnore = $eventsIgnore;
121
        $this->tablesOnly = $tablesOnly;
122
        $this->databasesOnly = $databasesOnly;
123
        $this->mariaDbGtid = $mariaGtid;
124
    }
125
126
    /**
127
     * @throws ConfigException
128
     */
129
    public function validate()
130
    {
131 View Code Duplication
        if (!empty($this->user) && false === is_string($this->user))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
        {
133
            throw new ConfigException(ConfigException::USER_ERROR_MESSAGE, ConfigException::USER_ERROR_CODE);
134
        }
135
        if (!empty($this->host))
136
        {
137
            $ip = gethostbyname($this->host);
138
            if (false === filter_var($ip, FILTER_VALIDATE_IP))
139
            {
140
                throw new ConfigException(ConfigException::IP_ERROR_MESSAGE, ConfigException::IP_ERROR_CODE);
141
            }
142
        }
143 View Code Duplication
        if (!empty($this->port) && false === filter_var($this->port, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]]))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
        {
145
            throw new ConfigException(ConfigException::PORT_ERROR_MESSAGE, ConfigException::PORT_ERROR_CODE);
146
        }
147
        if (!empty($this->password) && false === is_string($this->password) && false === is_numeric($this->password))
148
        {
149
            throw new ConfigException(ConfigException::PASSWORD_ERROR_MESSAGE, ConfigException::PASSWORD_ERROR_CODE);
150
        }
151 View Code Duplication
        if (!empty($this->dbName) && false === is_string($this->dbName))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152
        {
153
            throw new ConfigException(ConfigException::DB_NAME_ERROR_MESSAGE, ConfigException::DB_NAME_ERROR_CODE);
154
        }
155 View Code Duplication
        if (!empty($this->charset) && false === is_string($this->charset))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
        {
157
            throw new ConfigException(ConfigException::CHARSET_ERROR_MESSAGE, ConfigException::CHARSET_ERROR_CODE);
158
        }
159
        if (!empty($this->gtid) && false === is_string($this->gtid))
160
        {
161
            foreach (explode(',', $this->gtid) as $gtid)
162
            {
163
                if (false === (bool)preg_match('/^([0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12})((?::[0-9-]+)+)$/', $gtid, $matches))
164
                {
165
                    throw new ConfigException(ConfigException::GTID_ERROR_MESSAGE, ConfigException::GTID_ERROR_CODE);
166
                }
167
            }
168
        }
169 View Code Duplication
        if (!empty($this->slaveId) && false === filter_var($this->slaveId, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]]))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
        {
171
            throw new ConfigException(ConfigException::SLAVE_ID_ERROR_MESSAGE, ConfigException::SLAVE_ID_ERROR_CODE);
172
        }
173 View Code Duplication
        if (!empty($this->binLogFileName) && false === is_string($this->binLogFileName))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
        {
175
            throw new ConfigException(ConfigException::BIN_LOG_FILE_NAME_ERROR_MESSAGE, ConfigException::BIN_LOG_FILE_NAME_ERROR_CODE);
176
        }
177 View Code Duplication
        if (!empty($this->binLogPosition) && false === filter_var($this->binLogPosition, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]]))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
        {
179
            throw new ConfigException(ConfigException::BIN_LOG_FILE_POSITION_ERROR_MESSAGE, ConfigException::BIN_LOG_FILE_POSITION_ERROR_CODE);
180
        }
181 View Code Duplication
        if (!empty($this->mariaDbGtid) && false === is_string($this->mariaDbGtid))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
        {
183
            throw new ConfigException(ConfigException::MARIADBGTID_ERROR_MESSAGE, ConfigException::MARIADBGTID_ERROR_CODE);
184
        }
185
    }
186
187
    /**
188
     * @return string
189
     */
190
    public function getUser()
191
    {
192
        return $this->user;
193
    }
194
195
    /**
196
     * @return string
197
     */
198
    public function getHost()
199
    {
200
        return $this->host;
201
    }
202
203
    /**
204
     * @return int
205
     */
206
    public function getPort()
207
    {
208
        return $this->port;
209
    }
210
211
    /**
212
     * @return string
213
     */
214
    public function getPassword()
215
    {
216
        return $this->password;
217
    }
218
219
    /**
220
     * @return string
221
     */
222
    public function getDbName()
223
    {
224
        return $this->dbName;
225
    }
226
227
    /**
228
     * @return string
229
     */
230
    public function getCharset()
231
    {
232
        return $this->charset;
233
    }
234
235
    /**
236
     * @return string
237
     */
238
    public function getGtid()
239
    {
240
        return $this->gtid;
241
    }
242
243
    /**
244
     * @return string
245
     */
246
    public function getMariaDbGtid()
247
    {
248
        return $this->mariaDbGtid;
249
    }
250
251
    /**
252
     * @return int
253
     */
254
    public function getSlaveId()
255
    {
256
        return $this->slaveId;
257
    }
258
259
    /**
260
     * @return string
261
     */
262
    public function getBinLogFileName()
263
    {
264
        return $this->binLogFileName;
265
    }
266
267
    /**
268
     * @return int
269
     */
270
    public function getBinLogPosition()
271
    {
272
        return $this->binLogPosition;
273
    }
274
275
    /**
276
     * @return array
277
     */
278
    public function getEventsOnly()
279
    {
280
        return $this->eventsOnly;
281
    }
282
283
    /**
284
     * @return array
285
     */
286
    public function getEventsIgnore()
287
    {
288
        return $this->eventsIgnore;
289
    }
290
291
    /**
292
     * @return array
293
     */
294
    public function getTablesOnly()
295
    {
296
        return $this->tablesOnly;
297
    }
298
299
    /**
300
     * @return array
301
     */
302
    public function getDatabasesOnly()
303
    {
304
        return $this->databasesOnly;
305
    }
306
}