GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SchemaTable::getPreviousVersion()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
namespace Dami\Migration;
4
5
use Rentgen\Database\Connection\Connection;
6
use Rentgen\Database\Table;
7
use Rentgen\Database\Column\DateTimeColumn;
8
use Rentgen\Database\Column\BigIntegerColumn;
9
use Rentgen\Database\Constraint\PrimaryKey;
10
use Rentgen\Schema\Manipulation;
11
use Rentgen\Schema\Info;
12
13
/**
14
 * @author Arek Jaskólski <[email protected]>
15
 */
16
class SchemaTable
17
{
18
    private $connection;
19
    private $manipulation;
20
    private $info;
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param Connection   $connection   Connection instance.
26
     * @param Manipulation $manipulation Manipulation instance.
27
     * @param Info         $info         Info instance.
28
     */
29
    public function __construct(Connection $connection, Manipulation $manipulation, Info $info)
30
    {
31
        $this->connection = $connection;
32
        $this->manipulation = $manipulation;
33
        $this->info = $info;
34
    }
35
36
    /**
37
     * Migrare to given version.
38
     *
39
     * @param string $version Version of migration.
40
     *
41
     * @return void
42
     */
43
    public function migrateToVersion($version)
44
    {
45
        $this->createIfNotExists();
46
        $currentVersion = $this->getCurrentVersion();
47
        if ((int) $version > $currentVersion) {
48
            $this->up($version);
49
        } else {
50
            $this->down($version);
51
        }
52
    }
53
54
    /**
55
     * Get all versions of migrations.
56
     *
57
     * @return array Versions of migrations.
58
     */
59
    public function getVersions()
60
    {
61
        $this->createIfNotExists();
62
        $sql = 'SELECT version FROM schema_migration ORDER BY version DESC';
63
        $versions = array();
64
        foreach ($this->connection->query($sql) as $row) {
65
            $versions[] = $row['version'];
66
        }
67
68
        return $versions;
69
    }
70
71
    /**
72
     * Get current version of migration.
73
     *
74
     * @return integer
75
     */
76
    public function getCurrentVersion()
77
    {
78
        $versions = $this->getVersions();
79
80
        return count($versions) > 0 ? $versions[0] : 0;
81
    }
82
83
    /**
84
     * Get previous version of migration.
85
     *
86
     * @return integer
87
     */
88
    public function getPreviousVersion()
89
    {
90
        $versions = $this->getVersions();
91
92
        return count($versions) > 1 ? $versions[1] : 0;
93
    }
94
95
    /**
96
     * Add a new migrate entry.
97
     *
98
     * @param string $version Version of migration.
99
     *
100
     * @return void
101
     */
102
    private function up($version)
103
    {
104
        $sql = sprintf('INSERT INTO schema_migration (version,created_at) VALUES (%s, now())', $version);
105
        $this->connection->execute($sql);
106
    }
107
108
    /**
109
     * Delete a migrate entry.
110
     *
111
     * @param string $version Version of migration.
112
     *
113
     * @return void
114
     */
115
    private function down($version)
116
    {
117
        $sql = sprintf('DELETE FROM schema_migration WHERE version = \'%s\'', $version);
118
        $this->connection->execute($sql);
119
    }
120
121
    /**
122
     * Create schema table if not exists.
123
     *
124
     * @return void
125
     */
126
    private function createIfNotExists()
127
    {
128
        $table = new Table('schema_migration');
129
        if ($this->info->isTableExists($table)) {
130
            return;
131
        }
132
        $primaryKey = new PrimaryKey(array('version'));
133
        $primaryKey->disableAutoIncrement();
134
135
        $table->addConstraint($primaryKey);
136
        $table
137
            ->addColumn(new BigIntegerColumn('version'))
138
            ->addColumn(new DateTimeColumn('created_at', array('nullable' => false, 'default' => 'now()')));
139
        $this->manipulation->create($table);
140
    }
141
}
142