m180328_085629_add_indexes   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 12 3
A createIndexes() 0 3 2
A safeUp() 0 6 1
A safeDown() 0 6 1
A dropIndexes() 0 3 2
1
<?php
2
3
use yii\db\Migration;
4
5
/**
6
 * Class m180328_085629_add_indexes
7
 */
8
class m180328_085629_add_indexes extends Migration
9
{
10
    private $columns = [];
11
    private $tableName;
12
    private $indexPrefix = 'ix_';
13
14
15
    private function setUp($model) {
16
        $this->tableName = $model;
17
18
        if ($model === 'language') {
19
            $this->columns =[
20
                'language', 'name'
21
            ];
22
        }
23
24
        if ($model === 'language_translation') {
25
            $this->columns =[
26
                'model_class', 'model_field_name', 'model_id'
27
            ];
28
        }
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function safeUp()
35
    {
36
        $this->setUp('language');
37
        $this->createIndexes();
38
        $this->setUp('language_translation');
39
        $this->createIndexes();
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function safeDown()
46
    {
47
        $this->setUp('language');
48
        $this->dropIndexes();
49
        $this->setUp('language_translation');
50
        $this->dropIndexes();
51
    }
52
53
54
55
56
    private function createIndexes(){
57
        foreach ($this->columns as $column) {
58
            $this->createIndex($this->indexPrefix.$column,$this->tableName, $column);
59
        }
60
    }
61
62
    private function dropIndexes(){
63
        foreach ($this->columns as $column) {
64
            $this->dropIndex($this->indexPrefix.$column,$this->tableName);
65
        }
66
    }
67
68
}
69