Passed
Pull Request — master (#88)
by Michael
08:00 queued 04:03
created

Migrate::moveDoColumns()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 13
rs 9.9
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Tdmdownloads\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
 * Class Migrate synchronize existing tables with target schema
17
 *
18
 * @category  Migrate
19
 * @author    Richard Griffith <[email protected]>
20
 * @copyright 2016 XOOPS Project (https://xoops.org)
21
 * @license   GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
22
 * @link      https://xoops.org
23
 */
24
class Migrate extends \Xmf\Database\Migrate
25
{
26
    private $renameTables;
27
    private $renameColumns;
28
29
    /**
30
     * Migrate constructor.
31
     * @throws \RuntimeException
32
     * @throws \InvalidArgumentException
33
     */
34
    public function __construct()
35
    {
36
        $class = __NAMESPACE__ . '\\' . 'Configurator';
37
38
        if (!\class_exists($class)) {
39
            throw new \RuntimeException("Class '$class' not found");
40
        }
41
42
        $configurator = new $class();
43
44
        $this->renameTables = $configurator->renameTables;
45
46
        $moduleDirName = \basename(dirname(__DIR__, 2));
47
48
        parent::__construct($moduleDirName);
49
    }
50
51
52
    /**
53
     * rename table if needed
54
     */
55
    private function renameTable()
56
    {
57
        foreach ($this->renameTables as $oldName => $newName) {
58
            if ($this->tableHandler->useTable($oldName) && !$this->tableHandler->useTable($newName)) {
59
                $this->tableHandler->renameTable($oldName, $newName);
60
            }
61
        }
62
    }
63
64
    /**
65
     * rename columns if needed
66
     */
67
    private function renameColumns()
68
    {
69
        foreach ($this->renameColumns as $tableName) {
70
            if ($this->tableHandler->useTable($tableName)) {
71
                $oldName = $tableName['from'];
72
                $newName = $tableName['to'];
73
                $attributes = $this->tableHandler->getColumnAttributes($tableName, $oldName);
74
                if (false !== \strpos($attributes, ' int(')) {
0 ignored issues
show
Bug introduced by
It seems like $attributes can also be of type boolean; however, parameter $haystack of strpos() 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

74
                if (false !== \strpos(/** @scrutinizer ignore-type */ $attributes, ' int(')) {
Loading history...
75
                    $this->tableHandler->alterColumn($tableName, $oldName, $attributes, $newName);
76
                }
77
            }
78
        }
79
    }
80
81
    /**
82
     * Perform any upfront actions before synchronizing the schema
83
     *
84
     * Some typical uses include
85
     *   table and column renames
86
     *   data conversions
87
     */
88
    protected function preSyncActions()
89
    {
90
        // rename table
91
        if ($this->renameTables && \is_array($this->renameTables)) {
92
            $this->renameTable();
93
        }
94
        // rename column
95
        if ($this->renameColumns && \is_array($this->renameColumns)) {
96
            $this->renameColumns();
97
        }
98
    }
99
}
100