MysqlRunningCheck   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 35
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getIdentifier() 0 3 1
A run() 0 9 2
A init() 0 5 1
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