Passed
Push — master ( a6a24c...80ac3a )
by kacper
03:22
created

MySQLRepository::isCheckSum()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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 60
    public function __construct(Connection $connection)
23
    {
24 60
        $this->connection = $connection;
25 60
    }
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 59
    private function getConnection()
62
    {
63 59
        if (false === $this->connection->ping()) {
64 1
            $this->connection->close();
65 1
            $this->connection->connect();
66 1
        }
67
68 59
        return $this->connection;
69
    }
70
71
    /**
72
     * @return bool
73
     */
74 56
    public function isCheckSum()
75
    {
76 56
        $res = $this->getConnection()->fetchAssoc('SHOW GLOBAL VARIABLES LIKE "BINLOG_CHECKSUM"');
77
78 56
        return isset($res['Value']) && $res['Value'] !== 'NONE';
79
    }
80
81
    /**
82
     * @return string
83
     */
84 55
    public function getVersion()
85
    {
86 55
        $r = '';
87 55
        $versions = $this->getConnection()->fetchAll('SHOW VARIABLES LIKE "version%"');
88 55
        if (is_array($versions) && 0 !== count($versions)) {
89 55
            foreach ($versions as $version) {
90 55
                $r .= $version['Value'];
91 55
            }
92 55
        }
93
94 55
        return $r;
95
    }
96
97
    /**
98
     * File
99
     * Position
100
     * Binlog_Do_DB
101
     * Binlog_Ignore_DB
102
     * Executed_Gtid_Set
103
     *
104
     * @return array
105
     */
106 55
    public function getMasterStatus()
107
    {
108 55
        return $this->getConnection()->fetchAssoc('SHOW MASTER STATUS');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getConnect...c('SHOW MASTER STATUS') also could return the type boolean which is incompatible with the documented return type array.
Loading history...
109
    }
110
}