Passed
Push — master ( 52dcbe...46e258 )
by Aleksandr
02:20
created

ForeignKeyColumn   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 214
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 214
rs 10
c 0
b 0
f 0
wmc 25

21 Methods

Rating   Name   Duplication   Size   Complexity  
A onDeleteDefault() 0 3 1
A refColumn() 0 4 1
A refTable() 0 4 1
A onDeleteCascade() 0 3 1
A getRefTable() 0 3 1
A sourceColumn() 0 4 1
A removeSchema() 0 7 2
A getRefColumn() 0 7 3
A setName() 0 4 1
A getOnUpdate() 0 3 1
A onDelete() 0 4 1
A remove() 0 3 1
A getName() 0 9 2
A getSourceColumn() 0 3 1
A apply() 0 5 1
A getOnDelete() 0 3 1
A onDeleteNull() 0 3 1
A getSourceTable() 0 3 1
A setMigrate() 0 4 1
A formName() 0 5 1
A sourceTable() 0 4 1
1
<?php
2
3
namespace carono\yii2migrate;
4
5
6
use yii\db\ColumnSchemaBuilder;
7
8
/**
9
 * Class ForeignKeyColumn
10
 *
11
 * @package carono\yii2installer
12
 */
13
class ForeignKeyColumn extends ColumnSchemaBuilder
14
{
15
    const FK_CASCADE = 'CASCADE';
16
    const FK_DEFAULT = 'SET DEFAULT';
17
    const FK_NULL = 'SET NULL';
18
    public $_onDelete = self::FK_CASCADE;
19
    public $_onUpdate = null;
20
    protected $_refTable = null;
21
    protected $_refColumn = null;
22
    protected $_sourceTable = null;
23
    protected $_sourceColumn = null;
24
    protected $_name;
25
    /**
26
     * @var Migration
27
     */
28
    public $migrate;
29
30
    /**
31
     * @param $migrate
32
     * @return $this
33
     */
34
    public function setMigrate($migrate)
35
    {
36
        $this->migrate = $migrate;
37
        return $this;
38
    }
39
40
    /**
41
     * @param $value
42
     * @return $this
43
     */
44
    public function setName($value)
45
    {
46
        $this->_name = $value;
47
        return $this;
48
    }
49
50
    /**
51
     * @return mixed
52
     */
53
    public function getName()
54
    {
55
        if ($this->_name) {
56
            $name = $this->_name;
57
        } else {
58
            $name = self::formName($this->getSourceTable(), $this->getSourceColumn(), $this->getRefTable(), $this->getRefColumn());
59
        }
60
        $name = $this->migrate->expandTablePrefix($name);
61
        return $name;
62
    }
63
64
    public function apply()
65
    {
66
        return $this->migrate->addForeignKey(
67
            $this->getName(), $this->getSourceTable(), $this->getSourceColumn(), $this->getRefTable(),
68
            $this->getRefColumn(), $this->getOnDelete(), $this->getOnUpdate()
0 ignored issues
show
Bug introduced by
It seems like $this->getOnUpdate() can also be of type string; however, parameter $update of carono\yii2migrate\Migration::addForeignKey() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
            $this->getRefColumn(), $this->getOnDelete(), /** @scrutinizer ignore-type */ $this->getOnUpdate()
Loading history...
69
        );
70
    }
71
72
    public function remove()
73
    {
74
        $this->migrate->dropForeignKeyByColumn($this->getSourceTable(), $this->getSourceColumn());
75
    }
76
77
    /**
78
     * @return null|string
79
     */
80
    public function getRefTable()
81
    {
82
        return $this->_refTable;
83
    }
84
85
    /**
86
     * @return null|string
87
     */
88
    public function getRefColumn()
89
    {
90
        if (!$this->_refColumn && $this->migrate) {
91
            $pk = $this->migrate->db->getTableSchema($this->getRefTable())->primaryKey;
92
            $this->refColumn(current($pk));
93
        }
94
        return $this->_refColumn;
95
    }
96
97
    /**
98
     * @return null|string
99
     */
100
    public function getSourceTable()
101
    {
102
        return $this->_sourceTable;
103
    }
104
105
    /**
106
     * @return null|string
107
     */
108
    public function getSourceColumn()
109
    {
110
        return $this->_sourceColumn;
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getOnDelete()
117
    {
118
        return $this->_onDelete;
119
    }
120
121
    /**
122
     * @return null|string
123
     */
124
    public function getOnUpdate()
125
    {
126
        return $this->_onUpdate;
127
    }
128
129
    /**
130
     * @param $string
131
     * @return $this
132
     */
133
    public function onDelete($string)
134
    {
135
        $this->_onDelete = $string;
136
        return $this;
137
    }
138
139
    /**
140
     * @return ForeignKeyColumn
141
     */
142
    public function onDeleteCascade()
143
    {
144
        return $this->onDelete(self::FK_CASCADE);
145
    }
146
147
    /**
148
     * @return ForeignKeyColumn
149
     */
150
    public function onDeleteNull()
151
    {
152
        return $this->onDelete(self::FK_NULL);
153
    }
154
155
    public function onDeleteDefault()
156
    {
157
        $this->_onDelete = self::FK_DEFAULT;
158
    }
159
160
    /**
161
     * @param $name
162
     * @return $this
163
     */
164
    public function refTable($name)
165
    {
166
        $this->_refTable = $name;
167
        return $this;
168
    }
169
170
    /**
171
     * @param $name
172
     * @return $this
173
     */
174
    public function sourceColumn($name)
175
    {
176
        $this->_sourceColumn = $name;
177
        return $this;
178
    }
179
180
    /**
181
     * @param $str
182
     * @return mixed
183
     */
184
    private static function removeSchema($str)
185
    {
186
        if (strpos($str, '.') !== false) {
187
            $arr = explode('.', $str);
188
            return $arr[1];
189
        } else {
190
            return $str;
191
        }
192
    }
193
194
    /**
195
     * @param $table
196
     * @param $column
197
     * @param $refTable
198
     * @param $refColumn
199
     * @return string
200
     */
201
    public static function formName($table, $column, $refTable, $refColumn)
202
    {
203
        $table = self::removeSchema($table);
204
        $refTable = self::removeSchema($refTable);
205
        return "{$table}[{$column}]_{$refTable}[{$refColumn}]_fk";
206
    }
207
208
    /**
209
     * @param $name
210
     *
211
     * @return $this
212
     */
213
    public function sourceTable($name)
214
    {
215
        $this->_sourceTable = $name;
216
        return $this;
217
    }
218
219
    /**
220
     * @param $name
221
     * @return $this
222
     */
223
    public function refColumn($name)
224
    {
225
        $this->_refColumn = $name;
226
        return $this;
227
    }
228
}