UpdateManager::update()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 17
ccs 0
cts 13
cp 0
crap 12
rs 9.9666
1
<?php
2
3
namespace CodexShaper\DBM\Database\Schema;
4
5
use Doctrine\DBAL\Schema\Comparator;
6
use Doctrine\DBAL\Schema\SchemaException;
7
use Doctrine\DBAL\Schema\Table as DoctrineTable;
8
use Doctrine\DBAL\Schema\TableDiff;
9
10
class UpdateManager
11
{
12
    protected $data;
13
    protected $newTable;
14
    protected $originalTable;
15
16
    /**
17
     * Update table.
18
     *
19
     * @param array $table
20
     *
21
     * @return void
22
     */
23
    public function update($table)
24
    {
25
        if (! is_array($table)) {
0 ignored issues
show
introduced by
The condition is_array($table) is always true.
Loading history...
26
            $table = json_decode($table, true);
27
        }
28
29
        $tableName = $table['oldName'];
30
31
        if (! SchemaManager::getInstance()->tablesExist($tableName)) {
32
            throw SchemaException::tableDoesNotExist($table['oldName']);
33
        }
34
35
        $this->newTable = Table::prepareTable($table);
36
        $this->data = $table;
37
        $this->originalTable = static::listTableDetails($table['oldName']);
38
39
        $this->updateTable();
40
    }
41
42
    /**
43
     * Get all table details.
44
     *
45
     * @param string $tableName
46
     *
47
     * @return \CodexShaper\DBM\Database\Schema\Table
48
     */
49
    public static function listTableDetails($tableName)
50
    {
51
        $columns = SchemaManager::getInstance()->listTableColumns($tableName);
52
53
        $foreignKeys = [];
54
        if (SchemaManager::getInstance()->getDatabasePlatform()->supportsForeignKeyConstraints()) {
55
            $foreignKeys = SchemaManager::getInstance()->listTableForeignKeys($tableName);
56
        }
57
58
        $indexes = SchemaManager::getInstance()->listTableIndexes($tableName);
59
60
        return new DoctrineTable($tableName, $columns, $indexes, $foreignKeys, false, []);
61
    }
62
63
    /**
64
     * Update table.
65
     *
66
     * @return void
67
     */
68
    public function updateTable()
69
    {
70
        $newTableName = '';
71
72
        if ($this->newTable->getName() != $this->originalTable->getName()) {
73
            $newTableName = $this->newTable->getName();
74
            // Make sure the new name doesn't already exist
75
            if (SchemaManager::getInstance()->tablesExist($newTableName)) {
76
                throw SchemaException::tableAlreadyExists($newTableName);
77
            }
78
        }
79
80
        // Rename columns
81
        $this->renameColumns();
82
83
        $tableDiff = (new Comparator())->diffTable($this->originalTable, $this->newTable);
84
85
        // Add new table name to tableDiff
86
        if ($newTableName) {
87
            if (! $tableDiff) {
88
                $tableDiff = new TableDiff($this->data['oldName']);
89
                $tableDiff->fromTable = $this->originalTable;
90
            }
91
92
            $tableDiff->newName = $newTableName;
93
        }
94
95
        // Update the table
96
        if ($tableDiff) {
97
            SchemaManager::getInstance()->alterTable($tableDiff);
98
        }
99
    }
100
101
    /**
102
     * Rename columns.
103
     *
104
     * @return void
105
     */
106
    protected function renameColumns()
107
    {
108
        $renamedColumnsDiff = new TableDiff($this->data['oldName']);
109
        $renamedColumnsDiff->fromTable = $this->originalTable;
110
111
        $countRenamedColumns = 0;
112
113
        foreach ($this->data['columns'] as $column) {
114
            $oldName = $column['oldName'];
115
            if ($this->originalTable->hasColumn($oldName)) {
116
                $newName = $column['name'];
117
                if ($newName != $oldName) {
118
                    // Count total Renamed Columns
119
                    $countRenamedColumns++;
120
                    $renamedColumnsDiff->renamedColumns[$oldName] = $this->newTable->getColumn($newName);
121
                }
122
            }
123
        }
124
125
        if ($renamedColumnsDiff) {
0 ignored issues
show
introduced by
$renamedColumnsDiff is of type Doctrine\DBAL\Schema\TableDiff, thus it always evaluated to true.
Loading history...
126
            SchemaManager::getInstance()->alterTable($renamedColumnsDiff);
127
            // Refresh original table after renaming the columns
128
            $this->originalTable = SchemaManager::getInstance()->listTableDetails($this->data['oldName']);
129
        }
130
    }
131
}
132