Passed
Pull Request — master (#212)
by Arman
05:55 queued 03:04
created

Database::resolveQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 7
rs 10
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.9.6
13
 */
14
15
namespace Quantum\Libraries\Database;
16
17
use Quantum\Libraries\Database\Exceptions\DatabaseException;
18
use Quantum\Libraries\Database\Adapters\Idiorm\IdiormDbal;
19
use Quantum\Libraries\Database\Adapters\Sleekdb\SleekDbal;
20
use Quantum\Libraries\Config\Exceptions\ConfigException;
21
use Quantum\Libraries\Database\Contracts\DbalInterface;
22
use Quantum\Libraries\Database\Traits\RelationalTrait;
23
use Quantum\Di\Exceptions\DiException;
24
use Quantum\Exceptions\BaseException;
25
use Quantum\Loader\Setup;
26
use ReflectionException;
27
28
/**
29
 * Class Database
30
 * @package Quantum\Libraries\Database
31
 */
32
class Database
33
{
34
35
    use RelationalTrait;
36
37
    const ADAPTERS = [
38
        'sleekdb' => SleekDbal::class,
39
        'mysql' => IdiormDbal::class,
40
        'sqlite' => IdiormDbal::class,
41
        'pgsql' => IdiormDbal::class,
42
    ];
43
44
    /**
45
     * Database configurations
46
     * @var array
47
     */
48
    private $configs = [];
49
50
    /**
51
     * Database instance
52
     * @var Database|null
53
     */
54
    private static $instance = null;
55
56
    /**
57
     * @var string
58
     */
59
    private $ormClass;
60
61
    /**
62
     * @throws BaseException
63
     * @throws ConfigException
64
     * @throws DiException
65
     * @throws ReflectionException
66
     */
67
    private function __construct()
68
    {
69
        if (!config()->has('database')) {
70
            config()->import(new Setup('config', 'database'));
71
        }
72
73
        $adapterName = config()->get('database.current');
74
75
        if (!array_key_exists($adapterName, self::ADAPTERS)) {
76
            throw DatabaseException::adapterNotSupported($adapterName);
0 ignored issues
show
Bug introduced by
It seems like $adapterName can also be of type null; however, parameter $name of Quantum\Exceptions\BaseE...::adapterNotSupported() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

76
            throw DatabaseException::adapterNotSupported(/** @scrutinizer ignore-type */ $adapterName);
Loading history...
77
        }
78
79
        $this->ormClass = self::ADAPTERS[$adapterName];
80
81
        if (!class_exists($this->ormClass)) {
82
            throw DatabaseException::ormClassNotFound($this->ormClass);
83
        }
84
85
        $this->configs = config()->get('database.' . $adapterName);
86
87
        if (!$this->ormClass::getConnection()) {
88
            $this->ormClass::connect($this->configs);
89
        }
90
    }
91
92
    /**
93
     * Get Instance
94
     * @return Database
95
     */
96
    public static function getInstance(): Database
97
    {
98
        if (self::$instance === null) {
99
            self::$instance = new self();
100
        }
101
102
        return self::$instance;
103
    }
104
105
    /**
106
     * Gets the DB configurations
107
     * @return array|null
108
     */
109
    public function getConfigs(): ?array
110
    {
111
        return $this->configs;
112
    }
113
114
    /**
115
     * Gets the ORM class
116
     * @return string
117
     */
118
    public function getOrmClass(): string
119
    {
120
        return $this->ormClass;
121
    }
122
123
    /**
124
     * Gets the ORM
125
     * @param string $table
126
     * @param string $idColumn
127
     * @param array $foreignKeys
128
     * @param array $hidden
129
     * @return DbalInterface
130
     */
131
    public function getOrm(string $table, string $idColumn = 'id', array $foreignKeys = [], array $hidden = []): DbalInterface
132
    {
133
        return new $this->ormClass($table, $idColumn, $foreignKeys, $hidden);
134
    }
135
}