MysqlDatabase::deleteStatement()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace Starkerxp\DatabaseChecker\Structure;
4
5
6
use Starkerxp\DatabaseChecker\Exception\DatabaseHasNotDefinedException;
7
use Starkerxp\DatabaseChecker\LoggerTrait;
8
9
class MysqlDatabase implements DatabaseInterface
10
{
11
12
    use LoggerTrait;
13
14
    /**
15
     * @var string
16
     */
17
    private $database;
18
19
    /**
20
     * @var string
21
     */
22
    private $collate;
23
24
    /**
25
     * @var MysqlDatabaseTable[]
26
     */
27
    private $tables = [];
28
29
    /**
30
     * DatabaseTableStructure constructor.
31
     *
32
     * @param $database
33
     *
34
     * @throws DatabaseHasNotDefinedException
35
     */
36
    public function __construct($database)
37
    {
38
        if (empty($database)) {
39
            $this->critical('You need to define name of your database');
40
            throw new DatabaseHasNotDefinedException('');
41
        }
42
        $this->database = $database;
43
    }
44
45
    public function addTable(MysqlDatabaseTable $table): void
46
    {
47
        $table->setDatabase($this->getDatabase());
48
        if (!empty($this->getCollate())) {
49
            $table->setCollate($this->getCollate());
50
        }
51
        $this->tables[$table->getTable()] = $table;
52
    }
53
54
    /**
55
     * @return mixed
56
     */
57
    public function getDatabase()
58
    {
59
        return $this->database;
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    public function getCollate(): ?string
66
    {
67
        return $this->collate;
68
    }
69
70
    /**
71
     * @param string $collate
72
     */
73
    public function setCollate($collate): void
74
    {
75
        $this->collate = $collate;
76
    }
77
78
    public function removeTable($tableName): void
79
    {
80
        unset($this->tables[$tableName]);
81
    }
82
83
    public function toArray()
84
    {
85
        $export = [];
86
        $export['tables'] = [];
87
        $tables = $this->getTables();
88
        foreach ($tables as $table) {
89
            $arrayTable = $table->toArray();
90
            unset($arrayTable['table']);
91
            $export['tables'] = array_merge($export['tables'], $arrayTable);
92
        }
93
        $export['collate'] = $this->getCollate();
94
        $export = array_filter($export);
95
96
        return $export;
97
    }
98
99
    /**
100
     * @return MysqlDatabaseTable[]
101
     */
102
    public function getTables(): array
103
    {
104
        return $this->tables;
105
    }
106
107
    /**
108
     * @return array
109
     *
110
     */
111
    public function createStatement()
112
    {
113
        $modifications = [sprintf('CREATE DATABASE IF NOT EXISTS `%s`;', $this->getDatabase())];
114
115
        return $modifications;
116
    }
117
118
    /**
119
     * @return array
120
     *
121
     * @throws \RuntimeException
122
     */
123
    public function alterStatement()
124
    {
125
        $collateTmp = $this->getCollate();
126
        $collate = $collateTmp == '' ? '' : sprintf('CONVERT TO CHARACTER SET %s COLLATE %s', explode('_', $collateTmp)[0], $collateTmp);
127
        if ($collate == '') {
128
            return [];
129
        }
130
        return [
131
            sprintf('ALTER DATABASE %s CHARACTER SET %s COLLATE %s;', $this->database, explode('_', $collateTmp)[0], $collateTmp),
132
        ];
133
    }
134
135
    public function deleteStatement()
136
    {
137
        return [];
138
    }
139
140
}
141