DBBaseTableExistCheck   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 104
c 0
b 0
f 0
ccs 33
cts 33
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
C performCheck() 0 28 8
A getTablesExist() 0 4 1
A setTablesExist() 0 4 1
A getSqlGetAllTable() 0 4 1
A setSqlGetAllTable() 0 4 1
1
<?php
2
3
namespace TonicHealthCheck\Check\DB\BaseTableExist;
4
5
use PDO;
6
use PDOException;
7
use TonicHealthCheck\Check\DB\AbstractDBCheck;
8
use TonicHealthCheck\Check\DB\DBExpectConnectCheckException;
9
use TonicHealthCheck\Check\DB\PDOFactory;
10
11
/**
12
 * Class DBBaseTableExistCheck.
13
 */
14
class DBBaseTableExistCheck extends AbstractDBCheck
15
{
16
    const CHECK = 'db-base-table-exist-check';
17
    const DEFAULT_SQL_GET_ALL_TABLE = 'SHOW TABLES';
18
19
    /**
20
     * @var array
21
     */
22
    protected $tablesExist;
23
24
    /**
25
     * @var string
26
     */
27
    protected $sqlGetAllTable;
28
29
    /**
30
     * @param string     $checkNode
31
     * @param PDOFactory $PDOFactory
32
     * @param array      $tablesExist
33
     * @param string     $sqlGetAllTable
34
     */
35 5
    public function __construct(
36
        $checkNode,
37
        PDOFactory $PDOFactory,
38
        $tablesExist,
39
        $sqlGetAllTable = self::DEFAULT_SQL_GET_ALL_TABLE
40
    ) {
41 5
        parent::__construct($checkNode);
42 5
        $this->setPDOFactory($PDOFactory);
43 5
        $this->setTablesExist($tablesExist);
44 5
        $this->setSqlGetAllTable($sqlGetAllTable);
45 5
    }
46
47
    /**
48
     * Check $tablesExist in the database.
49
     *
50
     * @param null|array $tablesExist
51
     *
52
     * @return void
53
     *
54
     * @throws DBBaseTableExistCheckException
55
     * @throws DBExpectConnectCheckException
56
     */
57 5
    public function performCheck($tablesExist = null)
58
    {
59 5
        if (null === $tablesExist) {
60 5
            $tablesExist = $this->getTablesExist();
61
        }
62
        try {
63 5
            $pdo = $this->getPDOInstance();
64
65 4
            if (null === $pdo || !$this->getPDOInstance() instanceof PDO) {
66 4
                throw DBExpectConnectCheckException::expectConnected();
67
            }
68 2
        } catch (PDOException $e) {
69 1
            throw new DBExpectConnectCheckException($e->getMessage(), $e->getCode());
70
        }
71
72
        try {
73 3
            $sql = $this->getSqlGetAllTable();
74 3
            $query = $this->getPDOInstance()->query($sql);
75 3
            $tables = $query->fetchAll(PDO::FETCH_COLUMN);
76 2
            foreach ($tablesExist as $tableName) {
77 2
                if (array_search($tableName, $tables) === false) {
78 2
                    throw DBBaseTableExistCheckException::tableDoesNotExist($tableName);
79
                }
80
            }
81 2
        } catch (\PDOException $e) {
82 1
            throw new DBBaseTableExistCheckException($e->getMessage(), $e->getCode(), $e);
83
        }
84 1
    }
85
86
    /**
87
     * @return array
88
     */
89 5
    public function getTablesExist()
90
    {
91 5
        return $this->tablesExist;
92
    }
93
94
    /**
95
     * @param array $tablesExist
96
     */
97 5
    protected function setTablesExist($tablesExist)
98
    {
99 5
        $this->tablesExist = $tablesExist;
100 5
    }
101
102
    /**
103
     * @return string
104
     */
105 3
    public function getSqlGetAllTable()
106
    {
107 3
        return $this->sqlGetAllTable;
108
    }
109
110
    /**
111
     * @param string $sqlGetAllTable
112
     */
113 5
    public function setSqlGetAllTable($sqlGetAllTable)
114
    {
115 5
        $this->sqlGetAllTable = $sqlGetAllTable;
116 5
    }
117
}
118