Completed
Push — master ( 747e00...006ae1 )
by Changwan
03:04
created

MigrateAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Database\Migrator;
3
4
use Wandu\Database\Manager;
5
use Wandu\Database\Query\CreateQuery;
6
7
class MigrateAdapter implements MigrateAdapterInterface
8
{
9
    /** @var \Wandu\Database\Contracts\ConnectionInterface */
10
    protected $connection;
11
12
    /** @var string */
13
    protected $tableName;
14
15
    public function __construct(Manager $manager, Configuration $config)
16
    {
17
        $this->tableName = $config->getTable();
18
        $this->connection = $manager->getConnection($config->getConnection());
19
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function initialize()
25
    {
26
        if (!$this->hasMigrateTable()) {
27
            $builder = $this->connection->createQueryBuilder($this->tableName);
28
            $this->connection->query(
29
                $builder->create(function (CreateQuery $query) {
30
                    $query->string('version', 30);
31
                    $query->longText('source');
32
                })
33
            );
34
        }
35
        return $this;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function versions()
42
    {
43
        if (!$this->hasMigrateTable()) {
44
            return [];
45
        }
46
        $builder = $this->connection->createQueryBuilder($this->tableName);
47
        $versions = $this->connection->all($builder->select()->orderBy('version'));
48
        return $versions->toArray();
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function version($id)
55
    {
56
        if (!$this->hasMigrateTable()) {
57
            return null;
58
        }
59
        $builder = $this->connection->createQueryBuilder($this->tableName);
60
        return $this->connection->first($builder->select()->where('version', $id));
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function up($id, $source)
67
    {
68
        $this->connection->query(
69
            $this->connection->createQueryBuilder($this->tableName)->insert([
70
                'version' => $id,
71
                'source' => $source,
72
            ])
73
        );
74
        return $this;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function down($id)
81
    {
82
        $this->connection->query(
83
            $this->connection->createQueryBuilder($this->tableName)->delete()->where([
84
                'version' => $id,
85
            ])
86
        );
87
        return $this;
88
    }
89
90
    /**
91
     * @return bool
92
     */
93
    protected function hasMigrateTable()
94
    {
95
        $builder = $this->connection->createQueryBuilder($this->tableName);
96
        try {
97
            $this->connection->first($builder->select()->take(1));
98
        } catch (\PDOException $e) {
99
            return false;
100
        }
101
        return true;
102
    }
103
}
104