Passed
Push — master ( 1fbb37...9e3033 )
by Alexey
07:10
created

Db::makeMigration()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 3
dl 0
loc 12
ccs 0
cts 10
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
class Db extends Module {
4
5
    public $connection = null;
6
    public $connect = false;
7
    public $dbConfig = [];
8
    public $curQuery = null;
9
    public $className = '';
10
    public $QueryClassName = '';
11
    public $ResultClassName = '';
12
    public $migrationsVersions = [];
13
14
    public function init($param = null) {
15
        if (!$param) {
16
            $param = isset($this->config['default']) ? $this->config['default'] : 'local';
17
        }
18
        if (!is_array($param)) {
19
            if (!($dbOption = Db\Options::get($param, 'connect_alias', ['array' => true]))) {
20
                return false;
21
            }
22
23
            $db = $dbOption;
24
        } else {
25
            $db = $param;
26
        }
27
        $className = 'Db\\' . $db['driver'];
28
        $this->connection = new $className();
29
        $this->connection->init($db);
30
        $this->connection->dbInstance = $this;
31
        $this->connect = $this->connection->connect;
32
        $this->dbConfig = $db;
33
34
        $this->className = 'Db\\' . $this->dbConfig['driver'];
35
        $this->QueryClassName = 'Db\\' . $this->dbConfig['driver'] . '\\Query';
36
        $this->ResultClassName = 'Db\\' . $this->dbConfig['driver'] . '\\Result';
37
    }
38
39
    public function loadMigrationsVersion($code) {
40
        if (!$this->connect) {
41
            return false;
42
        }
43
        $version = Db\Migration::getList(['where' => ['code', $code], 'order' => ['date_create', 'desc'], 'limit' => 1, 'key' => false]);
44
        if ($version) {
45
            $this->migrationsVersions[$code] = $version[0]->version;
46
        }
47
        return true;
48
    }
49
50
    public function compareMigrations($code, $migrations) {
51
        if (!isset($this->migrationsVersions[$code])) {
52
            $this->loadMigrationsVersion($code);
53
        }
54
        if (!isset($this->migrationsVersions[$code]) || !isset($migrations[$this->migrationsVersions[$code]])) {
55
            return $migrations;
56
        }
57
        $startVersion = $this->migrationsVersions[$code];
58
        end($migrations);
59
        if ($startVersion == key($migrations)) {
60
            return [];
61
        }
62
        $pos = 0;
63
        foreach ($migrations as $migrationVersion => $migration) {
64
            if ($startVersion == $migrationVersion) {
65
                return array_slice($migration, $pos, null, true);
66
            }
67
            $pos++;
68
        }
69
        return [];
70
    }
71
72
    public function makeMigration($code, $version, $migration) {
73
        if (!isset($migration['up'])) {
74
            return false;
75
        }
76
        if (is_callable($migration['up'])) {
77
            $migration['up']();
78
        }
79
        $this->migrationsVersions[$code] = $version;
80
        $migrationVersion = new Db\Migration(['code' => $code, 'version' => $version]);
81
        $migrationVersion->save();
82
        return true;
83
    }
84
85
    public function __call($name, $params) {
86
        if (!is_object($this->connection)) {
87
            return false;
88
        }
89
        if (method_exists($this->className, $name)) {
90
            return call_user_func_array(array($this->connection, $name), $params);
91
        }
92
        if (method_exists($this->QueryClassName, $name)) {
93
            if (!is_object($this->curQuery)) {
94
                $this->curQuery = new $this->QueryClassName($this->connection);
95
            }
96
            return call_user_func_array(array($this->curQuery, $name), $params);
97
        }
98
99
        return false;
100
    }
101
102
    public function newQuery() {
103
        if ($this->QueryClassName) {
104
            return new $this->QueryClassName($this->connection);
105
        }
106
        return false;
107
    }
108
109 View Code Duplication
    public function __get($name) {
110
        if (isset($this->connection->$name)) {
111
            return $this->connection->$name;
112
        }
113
        if (!is_object($this->curQuery)) {
114
            $this->curQuery = $this->newQuery();
115
        }
116
        if (isset($this->curQuery->$name)) {
117
            return $this->curQuery->$name;
118
        }
119
    }
120
121 View Code Duplication
    public function __set($name, $value) {
122
        if (isset($this->connection->$name)) {
123
            return $this->connection->$name = $value;
124
        }
125
        if (!is_object($this->curQuery)) {
126
            $this->curQuery = $this->newQuery();
127
        }
128
        if (isset($this->curQuery->$name)) {
129
            return $this->curQuery->$name = $value;
130
        }
131
    }
132
}