Passed
Push — master ( 8affd7...9f396d )
by Aleksandr
02:40
created

IndexColumn::setMigrate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
18
    /**
19
     * @param $migrate
20
     * @return $this
21
     */
22
    public function setMigrate($migrate)
23
    {
24
        $this->migrate = $migrate;
25
        return $this;
26
    }
27
28
    /**
29
     * @param bool $value
30
     * @return $this
31
     */
32
    public function unique($value = true)
33
    {
34
        $this->_unique = $value;
35
        return $this;
36
    }
37
38
    /**
39
     * @param $name
40
     * @return $this
41
     */
42
    public function name($name)
43
    {
44
        $this->_name = $name;
45
        return $this;
46
    }
47
48
    /**
49
     * @param array $columns
50
     * @return IndexColumn
51
     */
52
    public function columns($columns)
53
    {
54
        $this->_columns = $columns;
55
        return $this;
56
    }
57
58
    /**
59
     * @param $table
60
     * @return $this
61
     */
62
    public function table($table)
63
    {
64
        $this->_table = $table;
65
        return $this;
66
    }
67
68
    public function formIndexName()
69
    {
70
        $unique = $this->_unique;
71
        $columns = $this->_columns;
72
        if (!$this->_name) {
73
            $indexName = Migration::formIndexName($this->_table, $columns, $unique ? 'unq' : 'idx');
74
            $name = $this->migrate->expandTablePrefix($indexName);
75
        } else {
76
            $name = $this->_name;
77
        }
78
        return $name;
79
    }
80
81
    public function apply()
82
    {
83
        $this->migrate->createIndex($this->formIndexName(), $this->_table, $this->_columns, $this->_unique);
84
    }
85
86
    public function remove()
87
    {
88
        $this->migrate->dropIndex($this->formIndexName(), $this->_table);
89
    }
90
}