Passed
Push — master ( cea7d1...f386a3 )
by huang
03:09
created

Migration::change()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 21
nc 9
nop 0
dl 0
loc 33
rs 5.3846
c 0
b 0
f 0
ccs 0
cts 25
cp 0
crap 72
1
<?php
2
/**
3
 * @author    jan huang <[email protected]>
4
 * @copyright 2016
5
 *
6
 * @see      https://www.github.com/janhuang
7
 * @see      http://www.fast-d.cn/
8
 */
9
10
namespace FastD\Model;
11
12
use Phinx\Db\Table;
13
use Phinx\Db\Table\Column;
14
use Phinx\Migration\AbstractMigration;
15
16
/**
17
 * Class Migration.
18
 */
19
abstract class Migration extends AbstractMigration
20
{
21
    public function change()
22
    {
23
        $table = $this->setUp();
24
        if (!$table->exists()) {
25
            $hasAvailable = $hasCreatedColumn = $hasUpdatedColumn = false;
26
            array_map(
27
                function (Column $column) use (&$hasCreatedColumn, &$hasAvailable, &$hasUpdatedColumn) {
28
                    if ('is_available' === $column->getName()) {
29
                        $hasAvailable = true;
30
31
                        return;
32
                    }
33
                    if ('created' === $column->getName()) {
34
                        $hasCreatedColumn = true;
35
36
                        return;
37
                    }
38
                    if ('updated' === $column->getName()) {
39
                        $hasUpdatedColumn = true;
40
41
                        return;
42
                    }
43
                },
44
                $table->getPendingColumns()
45
            );
46
            !$hasAvailable && $table->addColumn('is_available', 'boolean');
47
            !$hasCreatedColumn && $table->addColumn('created_at', 'datetime');
48
            !$hasUpdatedColumn && $table->addColumn('updated_at', 'datetime');
49
50
            $table->create();
51
        }
52
        $this->dataSet($table);
53
    }
54
55
    /**
56
     * @return Table
57
     */
58
    abstract public function setUp();
59
60
    /**
61
     * @param Table $table
62
     *
63
     * @return mixed
64
     */
65
    abstract public function dataSet(Table $table);
66
}
67