Passed
Pull Request — master (#48)
by Arman
03:56
created

Database::getConfigs()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
c 0
b 0
f 0
nc 6
nop 0
dl 0
loc 17
rs 9.6111
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.6.0
13
 */
14
15
namespace Quantum\Libraries\Database;
16
17
use Quantum\Exceptions\DatabaseException;
18
use Quantum\Loader\Setup;
19
20
/**
21
 * Class Database
22
 * @package Quantum\Libraries\Database
23
 */
24
class Database
25
{
26
27
    /**
28
     * Database configurations
29
     * @var array
30
     */
31
    private $configs = [];
32
33
    /**
34
     * Database instance
35
     * @var \Quantum\Libraries\Database\Database|null
36
     */
37
    private static $instance = null;
38
39
    /**
40
     * Database constructor.
41
     * @throws \Quantum\Exceptions\DatabaseException
42
     */
43
    private function __construct()
44
    {
45
        $this->configs = $this->getConfigs();
46
    }
47
48
    /**
49
     * Get Instance
50
     * @return \Quantum\Libraries\Database\Database|null
51
     */
52
    public static function getInstance(): ?Database
53
    {
54
        if (self::$instance === null) {
55
            self::$instance = new self();
56
        }
57
58
        return self::$instance;
59
    }
60
61
    /**
62
     * Gets the ORM
63
     * @param string $table
64
     * @param string $idColumn
65
     * @return \Quantum\Libraries\Database\DbalInterface
66
     * @throws \Quantum\Exceptions\DatabaseException
67
     */
68
    public function getOrm(string $table, string $idColumn = 'id'): DbalInterface
69
    {
70
        $ormClass = $this->getOrmClass();
71
72
        return new $ormClass($table, $idColumn);
73
    }
74
75
    /**
76
     * Raw execute
77
     * @param string $query
78
     * @param array $parameters
79
     * @return bool
80
     * @throws \Quantum\Exceptions\DatabaseException
81
     */
82
    public static function execute(string $query, array $parameters = []): bool
83
    {
84
        return self::resolveQuery(__FUNCTION__, $query, $parameters);
85
    }
86
87
    /**
88
     * Raw query
89
     * @param string $query
90
     * @param array $parameters
91
     * @return array
92
     * @throws \Quantum\Exceptions\DatabaseException
93
     */
94
    public static function query(string $query, array $parameters = []): array
95
    {
96
        return self::resolveQuery(__FUNCTION__, $query, $parameters);
97
    }
98
99
    /**
100
     * Gets the last query executed
101
     * @return string|null
102
     * @throws \Quantum\Exceptions\DatabaseException
103
     */
104
    public static function lastQuery(): ?string
105
    {
106
        return self::resolveQuery(__FUNCTION__);
107
    }
108
109
    /**
110
     * Get an array containing all the queries
111
     * run on a specified connection up to now.
112
     * @return array
113
     * @throws \Quantum\Exceptions\DatabaseException
114
     */
115
    public static function queryLog(): array
116
    {
117
        return self::resolveQuery(__FUNCTION__);
118
    }
119
120
    /**
121
     * Gets the DB configurations
122
     * @return array
123
     * @throws \Quantum\Exceptions\ConfigException
124
     * @throws \Quantum\Exceptions\DatabaseException
125
     * @throws \Quantum\Exceptions\DiException
126
     * @throws \ReflectionException
127
     */
128
    protected function getConfigs(): ?array
129
    {
130
        if (!config()->has('database') || !config()->has('database.current')) {
131
            config()->import(new Setup('config', 'database'));
132
        }
133
134
        $currentKey = config()->get('database.current');
135
136
        if (!config()->has('database.' . $currentKey)) {
137
            throw DatabaseException::incorrectConfig();
138
        }
139
140
        if (!config()->has('database.' . $currentKey . '.orm')) {
141
            throw DatabaseException::incorrectConfig();
142
        }
143
144
        return config()->get('database.' . $currentKey);
145
    }
146
147
    /**
148
     * Gets the ORM class
149
     * @return string
150
     * @throws \Quantum\Exceptions\DatabaseException
151
     */
152
    protected function getOrmClass(): string
153
    {
154
        $ormClass = $this->configs['orm'];
155
156
        if (!class_exists($ormClass)) {
157
            throw DatabaseException::ormClassNotFound($ormClass);
158
        }
159
160
        if (!$ormClass::getConnection()) {
161
            $ormClass::connect($this->configs);
162
        }
163
164
        return $ormClass;
165
    }
166
167
    /**
168
     * Resolves the requested query
169
     * @param string $method
170
     * @param string $query
171
     * @param array $parameters
172
     * @return mixed
173
     * @throws \Quantum\Exceptions\DatabaseException
174
     */
175
    protected static function resolveQuery(string $method, string $query = '', array $parameters = [])
176
    {
177
        $self = self::getInstance();
178
179
        $ormClass = $self->getOrmClass();
180
181
        return $ormClass::$method($query, $parameters);
182
    }
183
184
}
185