Passed
Push — develop ( a6ed4d...77e1ed )
by Andrew
04:27
created

Install::createTables()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 76
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 76
rs 8.9818
c 0
b 0
f 0
cc 4
nc 8
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Retour plugin for Craft CMS 3.x
4
 *
5
 * Retour allows you to intelligently redirect legacy URLs, so that you don't
6
 * lose SEO value when rebuilding & restructuring a website
7
 *
8
 * @link      https://nystudio107.com/
9
 * @copyright Copyright (c) 2018 nystudio107
10
 */
11
12
namespace nystudio107\retour\migrations;
13
14
use Craft;
15
use craft\config\DbConfig;
16
use craft\db\Migration;
17
18
/**
19
 * @author    nystudio107
20
 * @package   Retour
21
 * @since     3.0.0
22
 */
23
class Install extends Migration
24
{
25
    // Public Properties
26
    // =========================================================================
27
28
    /**
29
     * @var string The database driver to use
30
     */
31
    public $driver;
32
33
    // Public Methods
34
    // =========================================================================
35
36
    /**
37
     * @inheritdoc
38
     */
39
    public function safeUp()
40
    {
41
        $this->driver = Craft::$app->getConfig()->getDb()->driver;
42
        if ($this->createTables()) {
43
            $this->createIndexes();
44
            $this->addForeignKeys();
45
            // Refresh the db schema caches
46
            Craft::$app->db->schema->refresh();
47
            $this->insertDefaultData();
48
        }
49
50
        return true;
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public function safeDown()
57
    {
58
        $this->driver = Craft::$app->getConfig()->getDb()->driver;
59
        $this->removeTables();
60
61
        return true;
62
    }
63
64
    // Protected Methods
65
    // =========================================================================
66
67
    /**
68
     * @return bool
69
     */
70
    protected function createTables(): bool
71
    {
72
        $tablesCreated = false;
73
74
        $tableSchema = Craft::$app->db->schema->getTableSchema('{{%retour_redirects}}');
75
        if ($tableSchema === null) {
76
            $tablesCreated = true;
77
            $this->createTable(
78
                '{{%retour_redirects}}',
79
                [
80
                    'id'          => $this->primaryKey(),
81
                    'dateCreated' => $this->dateTime()->notNull(),
82
                    'dateUpdated' => $this->dateTime()->notNull(),
83
                    'uid'         => $this->uid(),
84
85
                    'locale'               => $this->string(12)->defaultValue('en-US'),
86
                    'associatedElementId'  => $this->integer()->notNull(),
87
                    'redirectSrcUrl'       => $this->string(255)->defaultValue(''),
88
                    'redirectSrcUrlParsed' => $this->string(255)->defaultValue(''),
89
                    'redirectSrcMatch'     => $this->string(32)->defaultValue('pathonly'),
90
                    'redirectMatchType'    => $this->string(32)->defaultValue('exactmatch'),
91
                    'redirectDestUrl'      => $this->string(255)->defaultValue(''),
92
                    'redirectHttpCode'     => $this->integer()->defaultValue(301),
93
                    'hitCount'             => $this->integer()->defaultValue(1),
94
                    'hitLastTime'          => $this->dateTime(),
95
                ]
96
            );
97
        }
98
99
        $tableSchema = Craft::$app->db->schema->getTableSchema('{{%retour_static_redirects}}');
100
        if ($tableSchema === null) {
101
            $tablesCreated = true;
102
            $this->createTable(
103
                '{{%retour_static_redirects}}',
104
                [
105
                    'id'          => $this->primaryKey(),
106
                    'dateCreated' => $this->dateTime()->notNull(),
107
                    'dateUpdated' => $this->dateTime()->notNull(),
108
                    'uid'         => $this->uid(),
109
110
                    'locale'               => $this->string(12)->defaultValue('en-US'),
111
                    'associatedElementId'  => $this->integer()->notNull(),
112
                    'redirectSrcUrl'       => $this->string(255)->defaultValue(''),
113
                    'redirectSrcUrlParsed' => $this->string(255)->defaultValue(''),
114
                    'redirectSrcMatch'     => $this->string(32)->defaultValue('pathonly'),
115
                    'redirectMatchType'    => $this->string(32)->defaultValue('exactmatch'),
116
                    'redirectDestUrl'      => $this->string(255)->defaultValue(''),
117
                    'redirectHttpCode'     => $this->integer()->defaultValue(301),
118
                    'hitCount'             => $this->integer()->defaultValue(1),
119
                    'hitLastTime'          => $this->dateTime(),
120
                ]
121
            );
122
        }
123
124
        $tableSchema = Craft::$app->db->schema->getTableSchema('{{%retour_stats}}');
125
        if ($tableSchema === null) {
126
            $tablesCreated = true;
127
            $this->createTable(
128
                '{{%retour_stats}}',
129
                [
130
                    'id'          => $this->primaryKey(),
131
                    'dateCreated' => $this->dateTime()->notNull(),
132
                    'dateUpdated' => $this->dateTime()->notNull(),
133
                    'uid'         => $this->uid(),
134
135
                    'redirectSrcUrl'  => $this->string(255)->defaultValue(''),
136
                    'referrerUrl'     => $this->string(2000)->defaultValue(''),
137
                    'remoteIp'        => $this->string(45)->defaultValue(''),
138
                    'hitCount'        => $this->integer()->defaultValue(1),
139
                    'hitLastTime'     => $this->dateTime(),
140
                    'handledByRetour' => $this->boolean()->defaultValue(false),
141
                ]
142
            );
143
        }
144
145
        return $tablesCreated;
146
    }
147
148
    /**
149
     * @return void
150
     */
151
    protected function createIndexes()
152
    {
153
        $this->createIndex(
154
            $this->db->getIndexName(
155
                '{{%retour_static_redirects}}',
156
                'redirectSrcUrlParsed',
157
                true
158
            ),
159
            '{{%retour_static_redirects}}',
160
            'redirectSrcUrlParsed',
161
            true
162
        );
163
        $this->createIndex(
164
            $this->db->getIndexName(
165
                '{{%retour_redirects}}',
166
                'redirectSrcUrlParsed',
167
                true
168
            ),
169
            '{{%retour_redirects}}',
170
            'redirectSrcUrlParsed',
171
            true
172
        );
173
    }
174
175
    /**
176
     * @return void
177
     */
178
    protected function addForeignKeys()
179
    {
180
        $this->addForeignKey(
181
            $this->db->getForeignKeyName('{{%retour_redirects}}', 'siteId'),
182
            '{{%retour_redirects}}',
183
            'associatedElementId',
184
            '{{%elements}}',
185
            'id',
186
            'CASCADE',
187
            'CASCADE'
188
        );
189
190
        /*
191
        $this->addForeignKey(
192
            $this->db->getForeignKeyName('{{%retour_redirects}}', 'siteId'),
193
            '{{%retour_redirects}}',
194
            'siteId',
195
            '{{%sites}}',
196
            'id',
197
            'CASCADE',
198
            'CASCADE'
199
        );
200
201
        $this->addForeignKey(
202
            $this->db->getForeignKeyName('{{%retour_stats}}', 'siteId'),
203
            '{{%retour_stats}}',
204
            'siteId',
205
            '{{%sites}}',
206
            'id',
207
            'CASCADE',
208
            'CASCADE'
209
        );
210
        */
211
    }
212
213
    /**
214
     * @return void
215
     */
216
    protected function insertDefaultData()
217
    {
218
    }
219
220
    /**
221
     * @return void
222
     */
223
    protected function removeTables()
224
    {
225
        $this->dropTableIfExists('{{%retour_redirects}}');
226
        $this->dropTableIfExists('{{%retour_static_redirects}}');
227
        $this->dropTableIfExists('{{%retour_stats}}');
228
    }
229
}
230