MysqlRunningCheck::run()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Leankoala\HealthFoundation\Check\Database\Mysql;
4
5
use Leankoala\HealthFoundation\Check\Check;
6
use Leankoala\HealthFoundation\Check\Result;
7
8
/**
9
 * Class MysqlRunningCheck
10
 *
11
 * Checks if a mysql database is running on a defined host with user and password.
12
 *
13
 * @package Leankoala\HealthFoundation\Check\Database\Mysql
14
 */
15
class MysqlRunningCheck implements Check
16
{
17
    const IDENTIFIER = 'base:database:mysql:running';
18
19
    private $username;
20
    private $password;
21
    private $host;
22
23
    /**
24
     * @param string $username the mysql user
25
     * @param string $password the mysql password for the given user
26
     * @param string $host the mysql host
27
     */
28
    public function init($username, $password, $host = 'localhost')
29
    {
30
        $this->username = $username;
31
        $this->password = $password;
32
        $this->host = $host;
33
    }
34
35
    public function run()
36
    {
37
        $mysqli = @(new \mysqli($this->host, $this->username, $this->password));
38
39
        if ($mysqli->connect_errno) {
40
            $message = 'Database is not running; ' . $mysqli->connect_error;
41
            return new Result(Result::STATUS_FAIL, $message);
42
        } else {
43
            return new Result(Result::STATUS_PASS, 'Mysql server is up and running.');
44
        }
45
    }
46
47
    public function getIdentifier()
48
    {
49
        return self::IDENTIFIER;
50
    }
51
}
52