Passed
Push — master ( 05ee30...e8b784 )
by Michael
05:15
created

Migrate   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 15
eloc 30
c 1
b 1
f 0
dl 0
loc 85
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A renameTable() 0 5 4
A preSyncActions() 0 9 5
A __construct() 0 12 2
A renameColumns() 0 15 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Adslight\Common;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
 */
16
17
use InvalidArgumentException;
18
use RuntimeException;
19
use XoopsModules\Adslight\{
20
    Common\Configurator
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsModules\Adslight\Common\Configurator. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
21
};
22
23
/**
24
 * Class Migrate synchronize existing tables with target schema
25
 *
26
 * @category  Migrate
27
 * @author    Richard Griffith <[email protected]>
28
 * @copyright 2016 XOOPS Project (https://xoops.org)
29
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
30
 * @link      https://xoops.org
31
 */
32
class Migrate extends \Xmf\Database\Migrate
33
{
34
    private $moduleDirName;
35
    private $renameColumns;
36
    private $renameTables;
37
38
    /**
39
     * Migrate constructor.
40
     * @throws \RuntimeException
41
     * @throws \InvalidArgumentException
42
     */
43
    public function __construct()
44
    {
45
        $class = __NAMESPACE__ . '\\' . 'Configurator';
46
        if (!\class_exists($class)) {
47
            throw new \RuntimeException("Class '$class' not found");
48
        }
49
        $configurator       = new $class();
50
        $this->renameTables = $configurator->renameTables;
51
        $this->renameColumns = $configurator->renameColumns;
52
53
        $this->moduleDirName = \basename(\dirname(__DIR__, 2));
54
        parent::__construct($this->moduleDirName);
55
    }
56
57
58
    /**
59
     * rename table if needed
60
     */
61
    private function renameTable(): void
62
    {
63
        foreach ($this->renameTables as $oldName => $newName) {
64
            if ($this->tableHandler->useTable($oldName) && !$this->tableHandler->useTable($newName)) {
65
                $this->tableHandler->renameTable($oldName, $newName);
66
            }
67
        }
68
    }
69
70
71
    /**
72
     * rename columns if needed
73
     */
74
    private function renameColumns(): void
75
    {
76
        $tables = new \Xmf\Database\Tables();
77
        foreach ($this->renameColumns as $table) {
78
            $tableName = $table['tablename'];
79
            $tableExists = $tables->useTable($tableName);
80
            if ($tableExists) {
81
                $oldName = $table['from'];
82
                $newName = $table['to'];
83
                $tableDetails=$tables->dumpTables();
0 ignored issues
show
Unused Code introduced by
The assignment to $tableDetails is dead and can be removed.
Loading history...
84
85
                $attributes = $tables->getColumnAttributes($tableName, $oldName);
86
//                if (false !== \strpos($attributes, ' int(')) {
87
                if (false !== $attributes) {
88
                    $tables->alterColumn($tableName, $oldName, $attributes, $newName);
89
90
//                    $tables->getTableIndexes()  update($tableName, [$newName => "($newName)"], '', false);
91
//
92
//                    $tables->dropIndex($name, $table)
93
//                    $tables->addIndex($name, $table, $column, $unique = false)
94
95
96
                }
97
            }
98
        }
99
    }
100
101
    /**
102
     * Perform any upfront actions before synchronizing the schema
103
     *
104
     * Some typical uses include
105
     *   table and column renames
106
     *   data conversions
107
     */
108
    protected function preSyncActions()
109
    {
110
        // rename table
111
        if ($this->renameTables && \is_array($this->renameTables)) {
112
            $this->renameTable();
113
        }
114
        // rename column
115
        if ($this->renameColumns && \is_array($this->renameColumns)) {
116
            $this->renameColumns();
117
        }
118
    }
119
}
120