IndexColumn::formIndexName()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 10
cc 3
nc 2
nop 0
crap 3
1
<?php
2
3
4
namespace carono\yii2migrate;
5
6
7
class IndexColumn
8
{
9
    /**
10
     * @var Migration
11
     */
12
    public $migrate;
13
    protected $_unique;
14
    protected $_name;
15
    protected $_columns;
16
    protected $_table;
17
    protected $_length;
18
19 1
    public function length($value)
20
    {
21 1
        $this->_length = $value;
22 1
        return $this;
23
    }
24
25
    /**
26
     * @param $migrate
27
     * @return $this
28
     */
29 11
    public function setMigrate($migrate)
30
    {
31 11
        $this->migrate = $migrate;
32 11
        return $this;
33
    }
34
35
    /**
36
     * @param bool $value
37
     * @return $this
38
     */
39 11
    public function unique($value = true)
40
    {
41 11
        $this->_unique = $value;
42 11
        return $this;
43
    }
44
45
    /**
46
     * @param $name
47
     * @return $this
48
     */
49 2
    public function name($name)
50
    {
51 2
        $this->_name = $name;
52 2
        return $this;
53
    }
54
55
    /**
56
     * @param array $columns
57
     * @return IndexColumn
58
     */
59 11
    public function columns($columns)
60
    {
61 11
        $this->_columns = $columns;
62 11
        return $this;
63
    }
64
65
    /**
66
     * @param $table
67
     * @return $this
68
     */
69 10
    public function table($table)
70
    {
71 10
        $this->_table = $table;
72 10
        return $this;
73
    }
74
75
    /**
76
     * @return mixed
77
     */
78 1
    public function getTable()
79
    {
80 1
        return $this->_table;
81
    }
82
83
    /**
84
     * @return mixed
85
     */
86 1
    public function getColumns()
87
    {
88 1
        return $this->_columns;
89
    }
90
91
    /**
92
     * @return mixed
93
     */
94 1
    public function getUnique()
95
    {
96 1
        return $this->_unique;
97
    }
98
99 9
    public function formIndexName()
100
    {
101 9
        $unique = $this->_unique;
102 9
        $columns = $this->_columns;
103 9
        if (!$this->_name) {
104 8
            $indexName = Migration::formIndexName($this->_table, $columns, $unique ? '_unq' : '_idx', $this->migrate->db->tablePrefix);
105 8
            $name = $this->migrate->expandTablePrefix($indexName);
106
        } else {
107 1
            $name = $this->_name;
108
        }
109 9
        return $name;
110
    }
111
112 6
    public function apply()
113
    {
114 6
        $columns = $this->_columns;
115 6
        if ($this->_length) {
116 1
            foreach ($columns as $idx => $column) {
117 1
                $columns[$idx] = "{$column}({$this->_length})";
118
            }
119
        }
120 6
        $this->migrate->createIndex($this->formIndexName(), $this->_table, $columns, $this->_unique);
121 6
    }
122
123 3
    public function remove()
124
    {
125 3
        $this->migrate->dropIndex($this->formIndexName(), $this->_table);
126 3
    }
127
128 1
    public function getName()
129
    {
130 1
        return $this->_name;
131
    }
132
}