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

MigrateAdapter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 97
ccs 0
cts 64
cp 0
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A initialize() 0 13 2
A versions() 0 9 2
A version() 0 8 2
A up() 0 10 1
A down() 0 9 1
A hasMigrateTable() 0 10 2
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