Passed
Pull Request — master (#8)
by Moln
05:05
created

MySQLRepository   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 12
Bugs 1 Features 0
Metric Value
eloc 29
c 12
b 1
f 0
dl 0
loc 95
ccs 32
cts 36
cp 0.8889
rs 10
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __destruct() 0 3 1
A __construct() 0 3 1
A ping() 0 7 2
A isCheckSum() 0 5 2
A getConnection() 0 8 2
A getVersion() 0 11 4
A getFields() 0 21 1
A getMasterStatus() 0 11 2
1
<?php
2
declare(strict_types=1);
3
4
namespace MySQLReplication\Repository;
5
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\Exception;
8
use MySQLReplication\BinLog\BinLogException;
9
use MySQLReplication\Exception\MySQLReplicationException;
10
11
class MySQLRepository implements RepositoryInterface, PingableConnection
12
{
13
    private $connection;
14
15 64
    public function __construct(Connection $connection)
16
    {
17 64
        $this->connection = $connection;
18
    }
19
20 59
    public function __destruct()
21
    {
22 59
        $this->connection->close();
23
    }
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 55
       ';
44
45 55
        return FieldDTOCollection::makeFromArray($this->getConnection()->fetchAllAssociative($sql, [$database, $table]));
46
    }
47
48 63
    private function getConnection(): Connection
49
    {
50 63
        if (false === $this->ping($this->connection)) {
51 1
            $this->connection->close();
52 1
            $this->connection->connect();
53
        }
54
55 63
        return $this->connection;
56
    }
57
58
    /**
59
     * @throws Exception
60
     */
61 60
    public function isCheckSum(): bool
62
    {
63 60
        $res = $this->getConnection()->fetchAssociative('SHOW GLOBAL VARIABLES LIKE "BINLOG_CHECKSUM"');
64
65 60
        return isset($res['Value']) && $res['Value'] !== 'NONE';
66
    }
67
68 59
    public function getVersion(): string
69
    {
70 59
        $r = '';
71 59
        $versions = $this->getConnection()->fetchAllAssociative('SHOW VARIABLES LIKE "version%"');
72 59
        if (is_array($versions) && 0 !== count($versions)) {
73 59
            foreach ($versions as $version) {
74 59
                $r .= $version['Value'];
75
            }
76
        }
77
78 59
        return $r;
79
    }
80
81
    /**
82
     * @inheritDoc
83
     * @throws Exception
84
     * @throws BinLogException
85
     */
86 59
    public function getMasterStatus(): MasterStatusDTO
87
    {
88 59
        $data = $this->getConnection()->fetchAssociative('SHOW MASTER STATUS');
89 59
        if (empty($data)) {
90
            throw new BinLogException(
91
                MySQLReplicationException::BINLOG_NOT_ENABLED,
92
                MySQLReplicationException::BINLOG_NOT_ENABLED_CODE
93
            );
94
        }
95
96 59
        return MasterStatusDTO::makeFromArray($data);
97
    }
98
99 63
    public function ping(Connection $connection): bool
100
    {
101
        try {
102 63
            $connection->executeQuery($connection->getDatabasePlatform()->getDummySelectSQL());
103 62
            return true;
104 1
        } catch (Exception $e) {
105 1
            return false;
106
        }
107
    }
108
}
109