m181213_233502_add_site_id   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 60
c 0
b 0
f 0
dl 0
loc 110
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B safeUp() 0 43 6
A addForeignKeys() 0 20 1
A createIndexes() 0 21 1
A safeDown() 0 4 1
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace nystudio107\retour\migrations;
4
5
use craft\db\Migration;
6
use yii\base\NotSupportedException;
7
8
/**
9
 * m181213_233502_add_site_id migration.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
10
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
11
class m181213_233502_add_site_id extends Migration
0 ignored issues
show
Coding Style introduced by
Class name must begin with a capital letter
Loading history...
Coding Style introduced by
Class name is not valid; consider M181213_233502_Add_Site_Id instead
Loading history...
12
{
13
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
14
     * @inheritdoc
15
     * @throws NotSupportedException
0 ignored issues
show
Coding Style introduced by
Tag value for @throws tag indented incorrectly; expected 5 spaces but found 1
Loading history...
16
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
17
    public function safeUp(): bool
18
    {
19
        // Drop the locale columns
20
        if ($this->db->columnExists('{{%retour_redirects}}', 'locale')) {
21
            $this->dropColumn(
22
                '{{%retour_redirects}}',
23
                'locale'
24
            );
25
        }
26
        if ($this->db->columnExists('{{%retour_static_redirects}}', 'locale')) {
27
            $this->dropColumn(
28
                '{{%retour_static_redirects}}',
29
                'locale'
30
            );
31
        }
32
        // Add in the siteId columns
33
        if (!$this->db->columnExists('{{%retour_redirects}}', 'siteId')) {
34
            $this->addColumn(
35
                '{{%retour_redirects}}',
36
                'siteId',
37
                $this->integer()->null()->after('uid')->defaultValue(null)
38
            );
39
        }
40
        if (!$this->db->columnExists('{{%retour_static_redirects}}', 'siteId')) {
41
            $this->addColumn(
42
                '{{%retour_static_redirects}}',
43
                'siteId',
44
                $this->integer()->null()->after('uid')->defaultValue(null)
45
            );
46
        }
47
        if (!$this->db->columnExists('{{%retour_stats}}', 'siteId')) {
48
            $this->addColumn(
49
                '{{%retour_stats}}',
50
                'siteId',
51
                $this->integer()->null()->after('uid')->defaultValue(null)
52
            );
53
        }
54
        // Add foreign keys
55
        $this->addForeignKeys();
56
        // Create indexes
57
        $this->createIndexes();
58
59
        return true;
60
    }
61
62
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
63
     * @inheritdoc
64
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
65
    public function safeDown()
66
    {
67
        echo "m181213_233502_add_site_id cannot be reverted.\n";
68
        return false;
69
    }
70
71
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
72
     * @return void
73
     */
74
    protected function addForeignKeys(): void
75
    {
76
        $this->addForeignKey(
77
            $this->db->getForeignKeyName(),
78
            '{{%retour_static_redirects}}',
79
            'siteId',
80
            '{{%sites}}',
81
            'id',
82
            'CASCADE',
83
            'CASCADE'
84
        );
85
86
        $this->addForeignKey(
87
            $this->db->getForeignKeyName(),
88
            '{{%retour_stats}}',
89
            'siteId',
90
            '{{%sites}}',
91
            'id',
92
            'CASCADE',
93
            'CASCADE'
94
        );
95
    }
96
97
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
98
     * @return void
99
     */
100
    protected function createIndexes(): void
101
    {
102
        $this->createIndex(
103
            $this->db->getIndexName(),
104
            '{{%retour_redirects}}',
105
            'siteId',
106
            false
107
        );
108
109
        $this->createIndex(
110
            $this->db->getIndexName(),
111
            '{{%retour_static_redirects}}',
112
            'siteId',
113
            false
114
        );
115
116
        $this->createIndex(
117
            $this->db->getIndexName(),
118
            '{{%retour_stats}}',
119
            'siteId',
120
            false
121
        );
122
    }
123
}
124