Completed
Push — master ( 6f2475...9a35ef )
by Nate
16:01
created

Install::createIndexes()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 191
Code Lines 148

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 191
rs 8.2857
c 0
b 0
f 0
cc 1
eloc 148
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
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organizations\migrations;
10
11
use craft\db\Migration;
12
use craft\records\Element as ElementRecord;
13
use craft\records\FieldLayout as FieldLayoutRecord;
14
use craft\records\Site as SiteRecord;
15
use craft\records\User as UserRecord;
16
use flipbox\organizations\records\Organization as OrganizationRecord;
17
use flipbox\organizations\records\Type as OrganizationTypeRecord;
18
use flipbox\organizations\records\TypeAssociation as OrganizationTypeAssociationRecord;
19
use flipbox\organizations\records\TypeSiteSettings as OrganizationTypeSiteSettingsRecord;
20
use flipbox\organizations\records\UserAssociation as OrganizationUserRecord;
21
use flipbox\organizations\records\UserCategory as OrganizationUserCategoryRecord;
22
use flipbox\organizations\records\UserCategoryAssociation as OrganizationUserCategoryAssociationRecord;
23
24
/**
25
 * @author Flipbox Factory <[email protected]>
26
 * @since 1.0.0
27
 */
28
class Install extends Migration
29
{
30
    /**
31
     * @inheritdoc
32
     */
33
    public function safeUp()
34
    {
35
        $this->createTables();
36
        $this->createIndexes();
37
        $this->addForeignKeys();
38
39
        return true;
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function safeDown()
46
    {
47
        $this->dropTableIfExists(OrganizationUserCategoryAssociationRecord::tableName());
48
        $this->dropTableIfExists(OrganizationUserCategoryRecord::tableName());
49
        $this->dropTableIfExists(OrganizationUserRecord::tableName());
50
        $this->dropTableIfExists(OrganizationTypeAssociationRecord::tableName());
51
        $this->dropTableIfExists(OrganizationTypeSiteSettingsRecord::tableName());
52
        $this->dropTableIfExists(OrganizationTypeRecord::tableName());
53
        $this->dropTableIfExists(OrganizationRecord::tableName());
54
55
        return true;
56
    }
57
58
    /**
59
     * Creates the tables.
60
     *
61
     * @return void
62
     */
63
    protected function createTables()
64
    {
65
        $this->createTable(OrganizationRecord::tableName(), [
66
            'id' => $this->primaryKey(),
67
            'state' => $this->string(),
68
            'dateJoined' => $this->dateTime()->notNull(),
69
            'dateCreated' => $this->dateTime()->notNull(),
70
            'dateUpdated' => $this->dateTime()->notNull(),
71
            'uid' => $this->uid()
72
        ]);
73
74
        $this->createTable(OrganizationTypeRecord::tableName(), [
75
            'id' => $this->primaryKey(),
76
            'handle' => $this->string()->notNull(),
77
            'name' => $this->string()->notNull(),
78
            'fieldLayoutId' => $this->integer(),
79
            'dateCreated' => $this->dateTime()->notNull(),
80
            'dateUpdated' => $this->dateTime()->notNull(),
81
            'uid' => $this->uid()
82
        ]);
83
84
        $this->createTable(OrganizationTypeSiteSettingsRecord::tableName(), [
85
            'typeId' => $this->integer()->notNull(),
86
            'siteId' => $this->integer()->notNull(),
87
            'enabledByDefault' => $this->boolean()->defaultValue(true)->notNull(),
88
            'hasUrls' => $this->boolean()->defaultValue(true)->notNull(),
89
            'uriFormat' => $this->text(),
90
            'template' => $this->string(500),
91
            'dateCreated' => $this->dateTime()->notNull(),
92
            'dateUpdated' => $this->dateTime()->notNull(),
93
            'uid' => $this->uid()
94
        ]);
95
96
        $this->createTable(OrganizationTypeAssociationRecord::tableName(), [
97
            'typeId' => $this->integer()->notNull(),
98
            'organizationId' => $this->integer()->notNull(),
99
            'sortOrder' => $this->smallInteger()->unsigned(),
100
            'dateCreated' => $this->dateTime()->notNull(),
101
            'dateUpdated' => $this->dateTime()->notNull(),
102
            'uid' => $this->uid()
103
        ]);
104
105
        $this->createTable(OrganizationUserRecord::tableName(), [
106
            'id' => $this->primaryKey(),
107
            'userId' => $this->integer()->notNull(),
108
            'organizationId' => $this->integer()->notNull(),
109
            'sortOrder' => $this->smallInteger()->unsigned(),
110
            'dateCreated' => $this->dateTime()->notNull(),
111
            'dateUpdated' => $this->dateTime()->notNull(),
112
            'uid' => $this->uid()
113
        ]);
114
115
        $this->createTable(OrganizationUserCategoryRecord::tableName(), [
116
            'id' => $this->primaryKey(),
117
            'handle' => $this->string()->notNull(),
118
            'name' => $this->string()->notNull(),
119
            'dateCreated' => $this->dateTime()->notNull(),
120
            'dateUpdated' => $this->dateTime()->notNull(),
121
            'uid' => $this->uid()
122
        ]);
123
124
        $this->createTable(OrganizationUserCategoryAssociationRecord::tableName(), [
125
            'userId' => $this->integer()->notNull(),
126
            'categoryId' => $this->integer()->notNull(),
127
            'sortOrder' => $this->smallInteger()->unsigned(),
128
            'dateCreated' => $this->dateTime()->notNull(),
129
            'dateUpdated' => $this->dateTime()->notNull(),
130
            'uid' => $this->uid()
131
        ]);
132
    }
133
134
    /**
135
     * Creates the indexes.
136
     *
137
     * @return void
138
     */
139
    protected function createIndexes()
140
    {
141
        $this->createIndex(
142
            $this->db->getIndexName(
143
                OrganizationTypeRecord::tableName(),
144
                'handle',
145
                true
146
            ),
147
            OrganizationTypeRecord::tableName(),
148
            'handle',
149
            true
150
        );
151
        $this->createIndex(
152
            $this->db->getIndexName(
153
                OrganizationTypeRecord::tableName(),
154
                'fieldLayoutId',
155
                false,
156
                true
157
            ),
158
            OrganizationTypeRecord::tableName(),
159
            'fieldLayoutId',
160
            false
161
        );
162
163
        $this->addPrimaryKey(
164
            $this->db->getPrimaryKeyName(
165
                OrganizationTypeSiteSettingsRecord::tableName(),
166
                ['typeId', 'siteId']
167
            ),
168
            OrganizationTypeSiteSettingsRecord::tableName(),
169
            ['typeId', 'siteId']
170
        );
171
        $this->createIndex(
172
            $this->db->getIndexName(
173
                OrganizationTypeSiteSettingsRecord::tableName(),
174
                'typeId',
175
                false,
176
                true
177
            ),
178
            OrganizationTypeSiteSettingsRecord::tableName(),
179
            'typeId',
180
            false
181
        );
182
        $this->createIndex(
183
            $this->db->getIndexName(
184
                OrganizationTypeSiteSettingsRecord::tableName(),
185
                'siteId',
186
                false,
187
                true
188
            ),
189
            OrganizationTypeSiteSettingsRecord::tableName(),
190
            'siteId',
191
            false
192
        );
193
        $this->createIndex(
194
            $this->db->getIndexName(
195
                OrganizationTypeSiteSettingsRecord::tableName(),
196
                'typeId,siteId',
197
                true
198
            ),
199
            OrganizationTypeSiteSettingsRecord::tableName(),
200
            'typeId,siteId',
201
            true
202
        );
203
204
        $this->addPrimaryKey(
205
            $this->db->getPrimaryKeyName(
206
                OrganizationTypeAssociationRecord::tableName(),
207
                ['typeId', 'organizationId']
208
            ),
209
            OrganizationTypeAssociationRecord::tableName(),
210
            ['typeId', 'organizationId']
211
        );
212
        $this->createIndex(
213
            $this->db->getIndexName(
214
                OrganizationTypeAssociationRecord::tableName(),
215
                'typeId',
216
                false,
217
                true
218
            ),
219
            OrganizationTypeAssociationRecord::tableName(),
220
            'typeId',
221
            false
222
        );
223
        $this->createIndex(
224
            $this->db->getIndexName(
225
                OrganizationTypeAssociationRecord::tableName(),
226
                'organizationId',
227
                false,
228
                true
229
            ),
230
            OrganizationTypeAssociationRecord::tableName(),
231
            'organizationId',
232
            false
233
        );
234
        $this->createIndex(
235
            $this->db->getIndexName(
236
                OrganizationTypeAssociationRecord::tableName(),
237
                'typeId,organizationId',
238
                true
239
            ),
240
            OrganizationTypeAssociationRecord::tableName(),
241
            'typeId,organizationId',
242
            true
243
        );
244
245
        $this->createIndex(
246
            $this->db->getIndexName(
247
                OrganizationUserRecord::tableName(),
248
                'userId',
249
                false,
250
                true
251
            ),
252
            OrganizationUserRecord::tableName(),
253
            'userId',
254
            false
255
        );
256
        $this->createIndex(
257
            $this->db->getIndexName(
258
                OrganizationUserRecord::tableName(),
259
                'organizationId',
260
                false,
261
                true
262
            ),
263
            OrganizationUserRecord::tableName(),
264
            'organizationId',
265
            false
266
        );
267
        $this->createIndex(
268
            $this->db->getIndexName(
269
                OrganizationUserRecord::tableName(),
270
                'userId,organizationId',
271
                true
272
            ),
273
            OrganizationUserRecord::tableName(),
274
            'userId,organizationId',
275
            true
276
        );
277
278
        $this->createIndex(
279
            $this->db->getIndexName(
280
                OrganizationUserCategoryRecord::tableName(),
281
                'handle',
282
                true
283
            ),
284
            OrganizationUserCategoryRecord::tableName(),
285
            'handle',
286
            true
287
        );
288
289
        $this->addPrimaryKey(
290
            $this->db->getPrimaryKeyName(
291
                OrganizationUserCategoryAssociationRecord::tableName(),
292
                ['userId', 'categoryId']
293
            ),
294
            OrganizationUserCategoryAssociationRecord::tableName(),
295
            ['userId', 'categoryId']
296
        );
297
        $this->createIndex(
298
            $this->db->getIndexName(
299
                OrganizationUserCategoryAssociationRecord::tableName(),
300
                'userId',
301
                false,
302
                true
303
            ),
304
            OrganizationUserCategoryAssociationRecord::tableName(),
305
            'userId',
306
            false
307
        );
308
        $this->createIndex(
309
            $this->db->getIndexName(
310
                OrganizationUserCategoryAssociationRecord::tableName(),
311
                'categoryId',
312
                false,
313
                true
314
            ),
315
            OrganizationUserCategoryAssociationRecord::tableName(),
316
            'categoryId',
317
            false
318
        );
319
        $this->createIndex(
320
            $this->db->getIndexName(
321
                OrganizationUserCategoryAssociationRecord::tableName(),
322
                'userId,categoryId',
323
                true
324
            ),
325
            OrganizationUserCategoryAssociationRecord::tableName(),
326
            'userId,categoryId',
327
            true
328
        );
329
    }
330
331
    /**
332
     * Adds the foreign keys.
333
     *
334
     * @return void
335
     */
336
    protected function addForeignKeys()
337
    {
338
339
        $this->addForeignKey(
340
            $this->db->getForeignKeyName(
341
                OrganizationRecord::tableName(),
342
                'id'
343
            ),
344
            OrganizationRecord::tableName(),
345
            'id',
346
            ElementRecord::tableName(),
347
            'id',
348
            'CASCADE',
349
            null
350
        );
351
352
        $this->addForeignKey(
353
            $this->db->getForeignKeyName(
354
                OrganizationTypeRecord::tableName(),
355
                'fieldLayoutId'
356
            ),
357
            OrganizationTypeRecord::tableName(),
358
            'fieldLayoutId',
359
            FieldLayoutRecord::tableName(),
360
            'id',
361
            'SET NULL',
362
            null
363
        );
364
365
        $this->addForeignKey(
366
            $this->db->getForeignKeyName(
367
                OrganizationTypeSiteSettingsRecord::tableName(),
368
                'typeId'
369
            ),
370
            OrganizationTypeSiteSettingsRecord::tableName(),
371
            'typeId',
372
            OrganizationTypeRecord::tableName(),
373
            'id',
374
            'CASCADE',
375
            'CASCADE'
376
        );
377
        $this->addForeignKey(
378
            $this->db->getForeignKeyName(
379
                OrganizationTypeSiteSettingsRecord::tableName(),
380
                'siteId'
381
            ),
382
            OrganizationTypeSiteSettingsRecord::tableName(),
383
            'siteId',
384
            SiteRecord::tableName(),
385
            'id',
386
            'CASCADE',
387
            'CASCADE'
388
        );
389
390
        $this->addForeignKey(
391
            $this->db->getForeignKeyName(
392
                OrganizationTypeAssociationRecord::tableName(),
393
                'typeId'
394
            ),
395
            OrganizationTypeAssociationRecord::tableName(),
396
            'typeId',
397
            OrganizationTypeRecord::tableName(),
398
            'id',
399
            'CASCADE',
400
            'CASCADE'
401
        );
402
        $this->addForeignKey(
403
            $this->db->getForeignKeyName(
404
                OrganizationTypeAssociationRecord::tableName(),
405
                'organizationId'
406
            ),
407
            OrganizationTypeAssociationRecord::tableName(),
408
            'organizationId',
409
            OrganizationRecord::tableName(),
410
            'id',
411
            'CASCADE',
412
            'CASCADE'
413
        );
414
415
        $this->addForeignKey(
416
            $this->db->getForeignKeyName(
417
                OrganizationUserRecord::tableName(),
418
                'userId'
419
            ),
420
            OrganizationUserRecord::tableName(),
421
            'userId',
422
            UserRecord::tableName(),
423
            'id',
424
            'CASCADE',
425
            'CASCADE'
426
        );
427
        $this->addForeignKey(
428
            $this->db->getForeignKeyName(
429
                OrganizationUserRecord::tableName(),
430
                'organizationId'
431
            ),
432
            OrganizationUserRecord::tableName(),
433
            'organizationId',
434
            OrganizationRecord::tableName(),
435
            'id',
436
            'CASCADE',
437
            'CASCADE'
438
        );
439
440
        $this->addForeignKey(
441
            $this->db->getForeignKeyName(
442
                OrganizationUserCategoryAssociationRecord::tableName(),
443
                'categoryId'
444
            ),
445
            OrganizationUserCategoryAssociationRecord::tableName(),
446
            'categoryId',
447
            OrganizationUserCategoryRecord::tableName(),
448
            'id',
449
            'CASCADE',
450
            'CASCADE'
451
        );
452
        $this->addForeignKey(
453
            $this->db->getForeignKeyName(
454
                OrganizationUserCategoryAssociationRecord::tableName(),
455
                'userId'
456
            ),
457
            OrganizationUserCategoryAssociationRecord::tableName(),
458
            'userId',
459
            OrganizationUserRecord::tableName(),
460
            'id',
461
            'CASCADE',
462
            'CASCADE'
463
        );
464
    }
465
}
466