SlaveStatusFieldCheck::getIdentifier()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Leankoala\HealthFoundation\Check\Database\Mysql\Slave;
4
5
use Leankoala\HealthFoundation\Check\Check;
6
use Leankoala\HealthFoundation\Check\Result;
7
8
class SlaveStatusFieldCheck implements Check
9
{
10
    const IDENTFIER = 'base:database:mysql:slave:slaveStatusField';
11
12
    private $userName;
13
    private $password;
14
    private $host;
15
16
    private $field;
17
    private $value;
18
19
    public function init($field, $value, $userName = 'root', $password = '', $host = 'localhost')
20
    {
21
        $this->host = $host;
22
        $this->userName = $userName;
23
        $this->password = $password;
24
        $this->field = $field;
25
        $this->value = $value;
26
    }
27
28
    public function run()
29
    {
30
        $mysqli = new \mysqli($this->host, $this->userName, $this->password);
31
32
        if ($mysqli->connect_errno) {
33
            return new Result(Result::STATUS_FAIL, sprintf("Connect failed: %s\n", $mysqli->connect_error));
34
        }
35
36
        $result = $mysqli->query("SHOW SLAVE STATUS");
37
38
        $fields = $result->fetch_assoc();
39
40
        if (is_array($fields)) {
41
            if (array_key_exists($this->field, $fields)) {
42
                if ($fields[$this->field] == $this->value) {
43
                    return new Result(Result::STATUS_PASS, 'Field "' . $this->field . '" has value "' . $this->value . '" which was expected.');
44
                } else {
45
                    return new Result(Result::STATUS_FAIL, 'Field "' . $this->field . '" has value ' . $fields[$this->field] . '. Expected was "' . $this->value . '"');
46
                }
47
            }
48
        }
49
50
        return new Result(Result::STATUS_FAIL, 'Field "' . $this->field . '" was not found in database slave status.');
51
    }
52
53
    public function getIdentifier()
54
    {
55
        return self::IDENTFIER . ':' . $this->field;
56
    }
57
}
58