Passed
Pull Request — master (#53)
by kacper
03:04
created

MySQLRepository   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 90.32%

Importance

Changes 11
Bugs 1 Features 0
Metric Value
eloc 24
c 11
b 1
f 0
dl 0
loc 86
ccs 28
cts 31
cp 0.9032
rs 10
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getFields() 0 21 1
A getMasterStatus() 0 11 2
A __destruct() 0 3 1
A __construct() 0 3 1
A isCheckSum() 0 5 2
A getConnection() 0 8 2
A getVersion() 0 11 4
1
<?php
2
declare(strict_types=1);
3
4
namespace MySQLReplication\Repository;
5
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\DBALException;
8
use MySQLReplication\BinLog\BinLogException;
9
use MySQLReplication\Exception\MySQLReplicationException;
10
11
class MySQLRepository implements RepositoryInterface
12
{
13
    private $connection;
14
15 62
    public function __construct(Connection $connection)
16
    {
17 62
        $this->connection = $connection;
18 62
    }
19
20 57
    public function __destruct()
21
    {
22 57
        $this->connection->close();
23 57
    }
24
25 55
    public function getFields(string $database, string $table): FieldDTOCollection
26
    {
27 55
        $sql = '
28
             SELECT
29
                `COLUMN_NAME`,
30
                `COLLATION_NAME`,
31
                `CHARACTER_SET_NAME`,
32
                `COLUMN_COMMENT`,
33
                `COLUMN_TYPE`,
34
                `COLUMN_KEY`
35
            FROM
36
                `information_schema`.`COLUMNS`
37
            WHERE
38
                    `TABLE_SCHEMA` = ?
39
                AND
40
                    `TABLE_NAME` = ?
41
            ORDER BY 
42
                ORDINAL_POSITION        
43
       ';
44
45 55
        return FieldDTOCollection::makeFromArray($this->getConnection()->fetchAll($sql, [$database, $table]));
46
    }
47
48 61
    private function getConnection(): Connection
49
    {
50 61
        if (false === $this->connection->ping()) {
51 1
            $this->connection->close();
52 1
            $this->connection->connect();
53
        }
54
55 61
        return $this->connection;
56
    }
57
58
    /**
59
     * @throws DBALException
60
     */
61 58
    public function isCheckSum(): bool
62
    {
63 58
        $res = $this->getConnection()->fetchAssoc('SHOW GLOBAL VARIABLES LIKE "BINLOG_CHECKSUM"');
64
65 58
        return isset($res['Value']) && $res['Value'] !== 'NONE';
66
    }
67
68 57
    public function getVersion(): string
69
    {
70 57
        $r = '';
71 57
        $versions = $this->getConnection()->fetchAll('SHOW VARIABLES LIKE "version%"');
72 57
        if (is_array($versions) && 0 !== count($versions)) {
73 57
            foreach ($versions as $version) {
74 57
                $r .= $version['Value'];
75
            }
76
        }
77
78 57
        return $r;
79
    }
80
81
    /**
82
     * @inheritDoc
83
     * @throws DBALException
84
     * @throws BinLogException
85
     */
86 57
    public function getMasterStatus(): MasterStatusDTO
87
    {
88 57
        $data = $this->getConnection()->fetchAssoc('SHOW MASTER STATUS');
89 57
        if (empty($data)) {
90
            throw new BinLogException(
91
                MySQLReplicationException::BINLOG_NOT_ENABLED,
92
                MySQLReplicationException::BINLOG_NOT_ENABLED_CODE
93
            );
94
        }
95
96 57
        return MasterStatusDTO::makeFromArray($data);
97
    }
98
}