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

Migration   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 41
ccs 0
cts 21
cp 0
rs 10
wmc 6
lcom 0
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
B change() 0 26 6
setUp() 0 1 ?
dataSet() 0 1 ?
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