Completed
Pull Request — master (#22)
by Runner
19:52
created

Migration::change()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 17
c 0
b 0
f 0
nc 5
nop 0
dl 0
loc 26
ccs 0
cts 21
cp 0
crap 42
rs 8.439
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\Migration\AbstractMigration;
14
use Phinx\Db\Table\Column;
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
26
            $hasCreatedColumn = $hasUpdatedColumn = false;
27
            array_map(
28
                function (Column $column) use (&$hasCreatedColumn, &$hasUpdatedColumn) {
29
                    if ('created' === $column->getName()) {
30
                        $hasCreatedColumn = true;
31
                        return;
32
                    }
33
                    if ('updated' === $column->getName()) {
34
                        $hasUpdatedColumn = true;
35
                        return;
36
                    }
37
                },
38
                $table->getPendingColumns()
39
            );
40
            !$hasCreatedColumn && $table->addColumn('created', 'datetime');
41
            !$hasUpdatedColumn && $table->addColumn('updated', 'datetime');
42
43
            $table->create();
44
        }
45
        $this->dataSet($table);
46
    }
47
48
    /**
49
     * @return Table
50
     */
51
    abstract public function setUp();
52
53
    /**
54
     * @param Table $table
55
     *
56
     * @return mixed
57
     */
58
    abstract public function dataSet(Table $table);
59
}
60