Test Failed
Branch master (1fbe3c)
by Aleksandr
02:14
created

ForeignKeyColumn::onDeleteNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace carono\yii2migrate;
4
5
6
use carono\yii2migrate\exceptions\ForeignKeyException;
7
use carono\yii2migrate\helpers\SchemaHelper;
8
use yii\db\ColumnSchemaBuilder;
9
10
/**
11
 * Class ForeignKeyColumn
12
 *
13
 * @package carono\yii2installer
14
 */
15
class ForeignKeyColumn extends ColumnSchemaBuilder
16
{
17
    const FK_CASCADE = 'CASCADE';
18
    const FK_DEFAULT = 'SET DEFAULT';
19
    const FK_NULL = 'SET NULL';
20
    const FK_RESTRICT = 'RESTRICT';
21
    const FK_NO_ACTION = 'NO ACTION';
22
23
    protected $_onDelete = self::FK_CASCADE;
24
    protected $_onUpdate;
25
    protected $_refTable;
26
    protected $_refColumn;
27
    protected $_sourceTable;
28
    protected $_sourceColumn;
29
    protected $_name;
30
    /**
31
     * @var Migration
32
     */
33
    public $migrate;
34
35
    /**
36
     * @param $migrate
37
     * @return $this
38
     */
39
    public function setMigrate($migrate)
40
    {
41
        $this->migrate = $migrate;
42
        $this->db = $migrate->db;
43
        return $this;
44
    }
45
46
    /**
47
     * @param $value
48
     * @return $this
49
     */
50
    public function setName($value)
51
    {
52
        $this->_name = $value;
53
        return $this;
54
    }
55
56
    /**
57
     * @return mixed
58
     */
59
    public function getName()
60
    {
61
        return $this->_name;
62
    }
63
64
    public function formIndexName()
65
    {
66
        if ($this->_name) {
67
            $name = $this->_name;
68
        } else {
69
            $name = $this->formName($this->getSourceTable(), $this->getSourceColumn(), $this->getRefTable(), $this->getRefColumn());
70
        }
71
        $name = $this->migrate->expandTablePrefix($name);
72
        return $name;
73
    }
74
75
    /**
76
     * @throws ForeignKeyException
77
     */
78
    public function apply()
79
    {
80
        $this->migrate->addForeignKey(
81
            $this->getName(), $this->getSourceTable(), $this->getSourceColumn(), $this->getRefTable(),
82
            $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

82
            $this->getRefColumn(), $this->getOnDelete(), /** @scrutinizer ignore-type */ $this->getOnUpdate()
Loading history...
83
        );
84
    }
85
86
    public function remove()
87
    {
88
        $this->migrate->dropForeignKeyByColumn($this->getSourceTable(), $this->getSourceColumn());
89
    }
90
91
    /**
92
     * @return null|string
93
     */
94
    public function getRefTable()
95
    {
96
        return $this->_refTable;
97
    }
98
99
    /**
100
     * @param $table
101
     * @return mixed
102
     * @throws ForeignKeyException
103
     */
104
    protected function getRefColumnByTable($table)
105
    {
106
        if (!$this->db->getTableSchema($table, true)) {
107
            throw new ForeignKeyException("Ref table '$table' not found");
108
        }
109
        $pk = current($this->migrate->db->getTableSchema($table)->primaryKey);
110
        if (!$pk){
111
            throw new ForeignKeyException("Primary key not found in ref table '$table'");
112
        }
113
        return $pk;
114
    }
115
116
    /**
117
     * @return null|string
118
     */
119
    public function getRefColumn()
120
    {
121
        if (!$this->_refColumn && $this->migrate) {
122
            $this->_refColumn = $this->getRefColumnByTable($this->getRefTable());
123
        }
124
        return $this->_refColumn;
125
    }
126
127
    /**
128
     * @return null|string
129
     */
130
    public function getSourceTable()
131
    {
132
        return $this->_sourceTable;
133
    }
134
135
    /**
136
     * @return null|string
137
     */
138
    public function getSourceColumn()
139
    {
140
        return $this->_sourceColumn;
141
    }
142
143
    /**
144
     * @return string
145
     */
146
    public function getOnDelete()
147
    {
148
        return $this->_onDelete;
149
    }
150
151
    /**
152
     * @return null|string
153
     */
154
    public function getOnUpdate()
155
    {
156
        return $this->_onUpdate;
157
    }
158
159
    /**
160
     * @param $string
161
     * @return $this
162
     */
163
    public function onDelete($string)
164
    {
165
        $this->_onDelete = $string;
166
        return $this;
167
    }
168
169
    /**
170
     * @param $string
171
     * @return $this
172
     */
173
    public function onUpdate($string)
174
    {
175
        $this->_onUpdate = $string;
176
        return $this;
177
    }
178
179
    /**
180
     * @return ForeignKeyColumn
181
     */
182
    public function onDeleteCascade()
183
    {
184
        return $this->onDelete(self::FK_CASCADE);
185
    }
186
187
    /**
188
     * @return ForeignKeyColumn
189
     */
190
    public function onDeleteNull()
191
    {
192
        return $this->onDelete(self::FK_NULL);
193
    }
194
195
    /**
196
     * @return ForeignKeyColumn
197
     */
198
    public function onDeleteDefault()
199
    {
200
        return $this->onDelete(self::FK_DEFAULT);
201
    }
202
203
    /**
204
     * @return ForeignKeyColumn
205
     */
206
    public function onDeleteRestrict()
207
    {
208
        return $this->onDelete(self::FK_RESTRICT);
209
    }
210
211
    /**
212
     * @return ForeignKeyColumn
213
     */
214
    public function onDeleteNoAction()
215
    {
216
        return $this->onDelete(self::FK_NO_ACTION);
217
    }
218
219
    /**
220
     * @return ForeignKeyColumn
221
     */
222
    public function onUpdateCascade()
223
    {
224
        return $this->onUpdate(self::FK_CASCADE);
225
    }
226
227
    /**
228
     * @return ForeignKeyColumn
229
     */
230
    public function onUpdateNull()
231
    {
232
        return $this->onUpdate(self::FK_NULL);
233
    }
234
235
    /**
236
     * @return ForeignKeyColumn
237
     */
238
    public function onUpdateDefault()
239
    {
240
        return $this->onUpdate(self::FK_DEFAULT);
241
    }
242
243
    /**
244
     * @return ForeignKeyColumn
245
     */
246
    public function onUpdateRestrict()
247
    {
248
        return $this->onUpdate(self::FK_RESTRICT);
249
    }
250
251
    /**
252
     * @return ForeignKeyColumn
253
     */
254
    public function onUpdateNoAction()
255
    {
256
        return $this->onUpdate(self::FK_NO_ACTION);
257
    }
258
259
    /**
260
     * @param $name
261
     * @return $this
262
     */
263
    public function refTable($name)
264
    {
265
        $this->_refTable = $name;
266
        return $this;
267
    }
268
269
    /**
270
     * @param $name
271
     * @return $this
272
     */
273
    public function sourceColumn($name)
274
    {
275
        $this->_sourceColumn = $name;
276
        return $this;
277
    }
278
279
    /**
280
     * @param $table
281
     * @param $column
282
     * @param $refTable
283
     * @param $refColumn
284
     * @return string
285
     */
286
    public function formName($table, $column, $refTable, $refColumn)
287
    {
288
        $table = SchemaHelper::removeSchema($this->migrate->expandTablePrefix($table));
289
        $refTable = SchemaHelper::removeSchema($this->migrate->expandTablePrefix($refTable));
290
        return "{$table}[{$column}]_{$refTable}[{$refColumn}]_fk";
291
    }
292
293
    /**
294
     * @param $name
295
     *
296
     * @return $this
297
     */
298
    public function sourceTable($name)
299
    {
300
        $this->_sourceTable = $name;
301
        return $this;
302
    }
303
304
    /**
305
     * @param $name
306
     * @return $this
307
     */
308
    public function refColumn($name)
309
    {
310
        $this->_refColumn = $name;
311
        return $this;
312
    }
313
}