Passed
Push — master ( 626be1...2b0f26 )
by kacper
02:53
created

MySQLRepository::getConnection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.5

Importance

Changes 5
Bugs 0 Features 0
Metric Value
dl 0
loc 9
ccs 3
cts 6
cp 0.5
rs 9.6666
c 5
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2.5
1
<?php
2
3
namespace MySQLReplication\Repository;
4
5
use Doctrine\DBAL\Connection;
6
7
/**
8
 * Class MySQLRepository
9
 * @package MySQLReplication\Repository
10
 */
11
class MySQLRepository implements RepositoryInterface
12
{
13
    /**
14
     * @var Connection
15
     */
16
    private $connection;
17
18
    /**
19
     * MySQLRepository constructor.
20
     * @param Connection $connection
21
     */
22 59
    public function __construct(Connection $connection)
23
    {
24 59
        $this->connection = $connection;
25 59
    }
26
27 55
    public function __destruct()
28
    {
29 55
        $this->connection->close();
30 55
    }
31
32
    /**
33
     * @param string $database
34
     * @param string $table
35
     * @return array
36
     */
37 53
    public function getFields($database, $table)
38
    {
39
        $sql = '
40
             SELECT
41
                `COLUMN_NAME`,
42
                `COLLATION_NAME`,
43
                `CHARACTER_SET_NAME`,
44
                `COLUMN_COMMENT`,
45
                `COLUMN_TYPE`,
46
                `COLUMN_KEY`
47
            FROM
48
                `information_schema`.`COLUMNS`
49
            WHERE
50
                    `TABLE_SCHEMA` = ?
51
                AND
52
                    `TABLE_NAME` = ?
53 53
       ';
54
55 53
        return $this->getConnection()->fetchAll($sql, [$database, $table]);
56
    }
57
58
    /**
59
     * @return Connection
60
     */
61 58
    private function getConnection()
62
    {
63 58
        if (false === $this->connection->ping()) {
64
            $this->connection->close();
65
            $this->connection->connect();
66
        }
67
68 58
        return $this->connection;
69
    }
70
71
    /**
72
     * @return bool
73
     */
74 55
    public function isCheckSum()
75
    {
76 55
        $res = $this->getConnection()->fetchAssoc('SHOW GLOBAL VARIABLES LIKE "BINLOG_CHECKSUM"');
77
78 55
		if (!isset($res['Value'])) {
79 1
			return false;
80
		}
81
82 55
		$check_sum = $res['Value'];
83
84 55
		return (!empty($check_sum) && strtolower($check_sum) !== 'none');
85
    }
86
87
    /**
88
     * @return string
89
     */
90 55
    public function getVersion()
91
    {
92 55
        $r = '';
93 55
        $versions = $this->getConnection()->fetchAll('SHOW VARIABLES LIKE "version%"');
94 55
        if (is_array($versions) && 0 !== count($versions)) {
95 55
            foreach ($versions as $version) {
96 55
                $r .= $version['Value'];
97 55
            }
98 55
        }
99
100 55
        return $r;
101
    }
102
103
    /**
104
     * File
105
     * Position
106
     * Binlog_Do_DB
107
     * Binlog_Ignore_DB
108
     * Executed_Gtid_Set
109
     *
110
     * @return array
111
     */
112 55
    public function getMasterStatus()
113
    {
114 55
        return $this->getConnection()->fetchAssoc('SHOW MASTER STATUS');
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getConnection()->...('SHOW MASTER STATUS'); of type array|boolean adds the type boolean to the return on line 114 which is incompatible with the return type declared by the interface MySQLReplication\Reposit...erface::getMasterStatus of type array.
Loading history...
115
    }
116
}