Migrate   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A preSyncActions() 0 10 3
A renameColumn() 0 13 2
A renameTable() 0 5 4
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Suico\Common;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * @category        Module
17
 * @copyright       {@link https://xoops.org/ XOOPS Project}
18
 * @license         GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
19
 * @author          Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
20
 */
21
22
use XoopsModules\Suico\Common;
23
24
/**
25
 * Class Migrate synchronize existing tables with target schema
26
 *
27
 * @category  Migrate
28
 * @author    Richard Griffith <[email protected]>
29
 * @copyright 2016 XOOPS Project (https://xoops.org)
30
 * @license   GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
31
 * @link      https://xoops.org
32
 */
33
class Migrate extends \Xmf\Database\Migrate
34
{
35
    private $renameTables;
36
    private $renameColumns;
37
38
    /**
39
     * Migrate constructor.
40
     * @param \XoopsModules\Suico\Common\Configurator|null $configurator
41
     */
42
    public function __construct(
43
        ?Common\Configurator $configurator = null
44
    ) {
45
        if (null !== $configurator) {
46
            $this->renameTables  = $configurator->renameTables;
47
            $this->renameColumns = $configurator->renameColumns;
48
            $moduleDirName       = \basename(\dirname(__DIR__, 2));
49
            parent::__construct($moduleDirName);
50
        }
51
    }
52
53
    /**
54
     * Perform any upfront actions before synchronizing the schema
55
     *
56
     * Some typical uses include
57
     *   table and column renames
58
     *   data conversions
59
     */
60
    protected function preSyncActions(): void
61
    {
62
        // rename table
63
        if ($this->renameTables && \is_array($this->renameTables)) {
64
            $this->renameTable();
65
        }
66
        $this->renameColumn('suico_notes', 'Note_id', 'note_id');
67
        $this->renameColumn('suico_notes', 'Note_text', 'note_text');
68
        $this->renameColumn('suico_notes', 'Note_from', 'note_from');
69
        $this->renameColumn('suico_notes', 'Note_to', 'note_to');
70
    }
71
72
    /**
73
     * rename table if needed
74
     */
75
    private function renameTable(): void
76
    {
77
        foreach ($this->renameTables as $oldName => $newName) {
78
            if ($this->tableHandler->useTable($oldName) && !$this->tableHandler->useTable($newName)) {
79
                $this->tableHandler->renameTable($oldName, $newName);
80
            }
81
        }
82
    }
83
84
    /**
85
     * @param $tableName
86
     * @param $columnName
87
     * @param $newName
88
     */
89
    private function renameColumn(
90
        $tableName,
91
        $columnName,
92
        $newName
93
    ): void {
94
        if ($this->tableHandler->useTable($tableName)) {
95
            $attributes = $this->tableHandler->getColumnAttributes($tableName, $columnName);
96
            //            if (false !== strpos($attributes, ' int(')) {
97
            $this->tableHandler->alterColumn(
98
                $tableName,
99
                $columnName,
100
                $attributes,
0 ignored issues
show
Bug introduced by
It seems like $attributes can also be of type false; however, parameter $attributes of Xmf\Database\Tables::alterColumn() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

100
                /** @scrutinizer ignore-type */ $attributes,
Loading history...
101
                $newName
102
            );
103
            //            }
104
        }
105
    }
106
}
107