Passed
Pull Request — master (#30)
by Mathieu
24:17 queued 14:14
created

MigrationModel::createMysqlMigrationTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Suricate\Migrations;
6
7
use Suricate\DBObject;
8
9
class MigrationModel extends DBObject
10
{
11
    protected $tableName = 'suricate_migrations';
12
    protected $tableIndex = 'name';
13
    // FIXME: set config ?
14
15
    public function __construct()
16
    {
17
        parent::__construct();
18
19
        $this->dbVariables = [
20
            'name',
21
            'date_added',
22
        ];
23
24
        $this->readOnlyVariables = ['date_added'];
25
    }
26
27
    public function createMigrationTable()
28
    {
29
        $this->connectDB();
30
        if ($this->dbLink !== false) {
0 ignored issues
show
introduced by
The condition $this->dbLink !== false is always true.
Loading history...
31
32
            $dbParameters = $this->dbLink->getConfigParameters();
33
            switch ($dbParameters['type']) {
34
                case 'mysql':
35
                    if (!$this->doesMigrationTableExists('mysql')) {
0 ignored issues
show
Unused Code introduced by
The call to Suricate\Migrations\Migr...sMigrationTableExists() has too many arguments starting with 'mysql'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
                    if (!$this->/** @scrutinizer ignore-call */ doesMigrationTableExists('mysql')) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
36
                        $this->createMysqlMigrationTable();
37
                        return true;
38
                    }
39
40
                    break;
41
                case 'sqlite':
42
                    if (!$this->doesMigrationTableExists('sqlite')) {
43
                        $this->createSqliteMigrationTable();
44
                        return true;
45
                    }
46
                    break;
47
            }
48
49
            return false;
50
        }
51
    }
52
53
    private function createMysqlMigrationTable()
54
    {
55
        $sql = <<<EOD
56
        CREATE TABLE IF NOT EXISTS `{$this->tableName}` (
57
            `name` varchar(255) NOT NULL UNIQUE,
58
            `date_added` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
59
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 
60
EOD;
61
62
        $this->dbLink->query($sql);
63
    }
64
65
    private function createSqliteMigrationTable()
66
    {
67
        // FIXME:
68
        $sql = <<<EOD
69
        CREATE TABLE IF NOT EXISTS "{$this->tableName}" (
70
            "name"	TEXT NOT NULL UNIQUE,
71
            "date_added" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
72
        );
73
EOD;
74
75
        return $sql;
76
    }
77
78
    private function doesMigrationTableExists(): bool
79
    {
80
        $dbParameters = $this->dbLink->getConfigParameters();
81
        $dbType = $dbParameters['type'];
82
83
        if ($dbType === 'sqlite') {
84
            $sql = "SELECT count(*) FROM sqlite_master WHERE type='table' AND name=:table";
0 ignored issues
show
Unused Code introduced by
The assignment to $sql is dead and can be removed.
Loading history...
85
            $sqlParams = [
0 ignored issues
show
Unused Code introduced by
The assignment to $sqlParams is dead and can be removed.
Loading history...
86
                "table" => $this->getTableName()
87
            ];
88
            // FIXME:
89
        }
90
91
        if ($dbType === 'mysql') {
92
            $sql = "SELECT count(*) FROM information_schema.tables " .
93
                " WHERE table_schema=:schema " .
94
                " AND table_name =:tableName ";
95
96
            $sqlParams = [
97
                "schema" => $dbParameters['database'],
98
                "tableName" => $this->getTableName()
99
            ];
100
            $res = $this->dbLink->query($sql, $sqlParams)->fetchColumn();
101
102
            return ((int) $res) === 1;
103
        }
104
105
        return true;
106
    }
107
}
108