Migrate::renameTable()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 0
1
<?php
2
3
namespace XoopsModules\Extcal\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
28
    /**
29
     * Migrate constructor.
30
     * @throws \RuntimeException
31
     * @throws \InvalidArgumentException
32
     */
33
    public function __construct()
34
    {
35
        $class = __NAMESPACE__ . '\\' . 'Configurator';
36
        if (!\class_exists($class)) {
37
            throw new \RuntimeException("Class '$class' not found");
38
        }
39
        $configurator       = new $class();
40
        $this->renameTables = $configurator->renameTables;
41
42
        $moduleDirName = \basename(\dirname(__DIR__, 2));
43
        parent::__construct($moduleDirName);
44
    }
45
46
//    public function __construct(Configurator $configurator = null)
47
//    {
48
//        if (null !== $configurator) {
49
//            $this->renameTables = $configurator->renameTables;
50
//
51
//            $moduleDirName = basename(dirname(__DIR__, 2));
52
//            parent::__construct($moduleDirName);
53
//        }
54
//    }
55
56
    /**
57
     * rename table if needed
58
     */
59
    private function renameTable()
60
    {
61
        foreach ($this->renameTables as $oldName => $newName) {
62
            if ($this->tableHandler->useTable($oldName) && !$this->tableHandler->useTable($newName)) {
63
                $this->tableHandler->renameTable($oldName, $newName);
64
            }
65
        }
66
    }
67
68
    /**
69
     * @param $tableName
70
     * @param $columnName
71
     * @param $newName
72
     */
73
    private function renameColumn($tableName, $columnName, $newName)
74
    {
75
        if ($this->tableHandler->useTable($tableName)) {
76
            $attributes = $this->tableHandler->getColumnAttributes($tableName, $columnName);
77
            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

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