Passed
Push — develop ( d23825...f4f765 )
by Andrew
06:42
created

Install::createIndexes()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 89
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 64
c 2
b 0
f 0
dl 0
loc 89
rs 8.7853
cc 1
nc 1
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 nystudio107\retour\widgets\RetourWidget;
15
16
use Craft;
17
use craft\db\Migration;
18
19
/**
20
 * @author    nystudio107
21
 * @package   Retour
22
 * @since     3.0.0
23
 */
24
class Install extends Migration
25
{
26
    // Public Properties
27
    // =========================================================================
28
29
    /**
30
     * @var string The database driver to use
31
     */
32
    public $driver;
33
34
    // Public Methods
35
    // =========================================================================
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function safeUp()
41
    {
42
        $this->driver = Craft::$app->getConfig()->getDb()->driver;
43
        if ($this->createTables()) {
44
            $this->createIndexes();
45
            $this->addForeignKeys();
46
            // Refresh the db schema caches
47
            Craft::$app->db->schema->refresh();
48
            $this->insertDefaultData();
49
        }
50
51
        // Update retour widget type
52
        $this->update('{{%widgets}}', [
53
            'type' => RetourWidget::class
54
        ], ['type' => 'Retour']);
55
56
        return true;
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function safeDown()
63
    {
64
        $this->driver = Craft::$app->getConfig()->getDb()->driver;
65
        $this->removeTables();
66
67
        return true;
68
    }
69
70
    // Protected Methods
71
    // =========================================================================
72
73
    /**
74
     * @return bool
75
     */
76
    protected function createTables(): bool
77
    {
78
        $tablesCreated = false;
79
80
        $tableSchema = Craft::$app->db->schema->getTableSchema('{{%retour_redirects}}');
81
        if ($tableSchema === null) {
82
            $tablesCreated = true;
83
            $this->createTable(
84
                '{{%retour_redirects}}',
85
                [
86
                    'id' => $this->primaryKey(),
87
                    'dateCreated' => $this->dateTime()->notNull(),
88
                    'dateUpdated' => $this->dateTime()->notNull(),
89
                    'uid' => $this->uid(),
90
91
                    'siteId' => $this->integer()->null()->defaultValue(null),
92
                    'associatedElementId' => $this->integer()->notNull(),
93
                    'enabled' => $this->boolean()->defaultValue(true),
94
                    'redirectSrcUrl' => $this->string(255)->defaultValue(''),
95
                    'redirectSrcUrlParsed' => $this->string(255)->defaultValue(''),
96
                    'redirectSrcMatch' => $this->string(32)->defaultValue('pathonly'),
97
                    'redirectMatchType' => $this->string(32)->defaultValue('exactmatch'),
98
                    'redirectDestUrl' => $this->string(255)->defaultValue(''),
99
                    'redirectHttpCode' => $this->integer()->defaultValue(301),
100
                    'hitCount' => $this->integer()->defaultValue(1),
101
                    'hitLastTime' => $this->dateTime(),
102
                ]
103
            );
104
        }
105
106
        $tableSchema = Craft::$app->db->schema->getTableSchema('{{%retour_static_redirects}}');
107
        if ($tableSchema === null) {
108
            $tablesCreated = true;
109
            $this->createTable(
110
                '{{%retour_static_redirects}}',
111
                [
112
                    'id' => $this->primaryKey(),
113
                    'dateCreated' => $this->dateTime()->notNull(),
114
                    'dateUpdated' => $this->dateTime()->notNull(),
115
                    'uid' => $this->uid(),
116
117
                    'siteId' => $this->integer()->null()->defaultValue(null),
118
                    'associatedElementId' => $this->integer()->notNull(),
119
                    'enabled' => $this->boolean()->defaultValue(true),
120
                    'redirectSrcUrl' => $this->string(255)->defaultValue(''),
121
                    'redirectSrcUrlParsed' => $this->string(255)->defaultValue(''),
122
                    'redirectSrcMatch' => $this->string(32)->defaultValue('pathonly'),
123
                    'redirectMatchType' => $this->string(32)->defaultValue('exactmatch'),
124
                    'redirectDestUrl' => $this->string(255)->defaultValue(''),
125
                    'redirectHttpCode' => $this->integer()->defaultValue(301),
126
                    'hitCount' => $this->integer()->defaultValue(1),
127
                    'hitLastTime' => $this->dateTime(),
128
                ]
129
            );
130
        }
131
132
        $tableSchema = Craft::$app->db->schema->getTableSchema('{{%retour_stats}}');
133
        if ($tableSchema === null) {
134
            $tablesCreated = true;
135
            $this->createTable(
136
                '{{%retour_stats}}',
137
                [
138
                    'id' => $this->primaryKey(),
139
                    'dateCreated' => $this->dateTime()->notNull(),
140
                    'dateUpdated' => $this->dateTime()->notNull(),
141
                    'uid' => $this->uid(),
142
143
                    'siteId' => $this->integer()->null()->defaultValue(null),
144
                    'redirectSrcUrl' => $this->string(255)->defaultValue(''),
145
                    'referrerUrl' => $this->string(2000)->defaultValue(''),
146
                    'remoteIp' => $this->string(45)->defaultValue(''),
147
                    'userAgent' => $this->string(255)->defaultValue(''),
148
                    'exceptionMessage' => $this->string(255)->defaultValue(''),
149
                    'exceptionFilePath' => $this->string(255)->defaultValue(''),
150
                    'exceptionFileLine' => $this->integer()->defaultValue(0),
151
                    'hitCount' => $this->integer()->defaultValue(1),
152
                    'hitLastTime' => $this->dateTime(),
153
                    'handledByRetour' => $this->boolean()->defaultValue(false),
154
                ]
155
            );
156
        }
157
158
        return $tablesCreated;
159
    }
160
161
    /**
162
     * @return void
163
     */
164
    protected function createIndexes()
165
    {
166
        $this->createIndex(
167
            $this->db->getIndexName(
168
                '{{%retour_static_redirects}}',
169
                'redirectSrcUrlParsed',
170
                false
171
            ),
172
            '{{%retour_static_redirects}}',
173
            'redirectSrcUrlParsed',
174
            false
175
        );
176
177
        $this->createIndex(
178
            $this->db->getIndexName(
179
                '{{%retour_redirects}}',
180
                'redirectSrcUrlParsed',
181
                false
182
            ),
183
            '{{%retour_redirects}}',
184
            'redirectSrcUrlParsed',
185
            false
186
        );
187
188
        $this->createIndex(
189
            $this->db->getIndexName(
190
                '{{%retour_static_redirects}}',
191
                'redirectSrcUrl',
192
                false
193
            ),
194
            '{{%retour_static_redirects}}',
195
            'redirectSrcUrl',
196
            false
197
        );
198
199
        $this->createIndex(
200
            $this->db->getIndexName(
201
                '{{%retour_redirects}}',
202
                'redirectSrcUrl',
203
                false
204
            ),
205
            '{{%retour_redirects}}',
206
            'redirectSrcUrl',
207
            false
208
        );
209
210
        $this->createIndex(
211
            $this->db->getIndexName(
212
                '{{%retour_stats}}',
213
                'redirectSrcUrl',
214
                false
215
            ),
216
            '{{%retour_stats}}',
217
            'redirectSrcUrl',
218
            false
219
        );
220
221
        $this->createIndex(
222
            $this->db->getIndexName(
223
                '{{%retour_redirects}}',
224
                'siteId',
225
                false
226
            ),
227
            '{{%retour_redirects}}',
228
            'siteId',
229
            false
230
        );
231
232
        $this->createIndex(
233
            $this->db->getIndexName(
234
                '{{%retour_static_redirects}}',
235
                'siteId',
236
                false
237
            ),
238
            '{{%retour_static_redirects}}',
239
            'siteId',
240
            false
241
        );
242
243
244
        $this->createIndex(
245
            $this->db->getIndexName(
246
                '{{%retour_stats}}',
247
                'siteId',
248
                false
249
            ),
250
            '{{%retour_stats}}',
251
            'siteId',
252
            false
253
        );
254
    }
255
256
    /**
257
     * @return void
258
     */
259
    protected function addForeignKeys()
260
    {
261
        $this->addForeignKey(
262
            $this->db->getForeignKeyName('{{%retour_redirects}}', 'associatedElementId'),
263
            '{{%retour_redirects}}',
264
            'associatedElementId',
265
            '{{%elements}}',
266
            'id',
267
            'CASCADE',
268
            'CASCADE'
269
        );
270
271
        $this->addForeignKey(
272
            $this->db->getForeignKeyName('{{%retour_static_redirects}}', 'siteId'),
273
            '{{%retour_static_redirects}}',
274
            'siteId',
275
            '{{%sites}}',
276
            'id',
277
            'CASCADE',
278
            'CASCADE'
279
        );
280
281
        $this->addForeignKey(
282
            $this->db->getForeignKeyName('{{%retour_stats}}', 'siteId'),
283
            '{{%retour_stats}}',
284
            'siteId',
285
            '{{%sites}}',
286
            'id',
287
            'CASCADE',
288
            'CASCADE'
289
        );
290
    }
291
292
    /**
293
     * @return void
294
     */
295
    protected function insertDefaultData()
296
    {
297
    }
298
299
    /**
300
     * @return void
301
     */
302
    protected function removeTables()
303
    {
304
        $this->dropTableIfExists('{{%retour_redirects}}');
305
        $this->dropTableIfExists('{{%retour_static_redirects}}');
306
        $this->dropTableIfExists('{{%retour_stats}}');
307
    }
308
}
309