Completed
Push — master ( 5666c1...f0d869 )
by Arman
01:12 queued 01:07
created

Database::getOrmClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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\Traits\RelationalTrait;
22
use Quantum\Di\Exceptions\DiException;
23
use Quantum\Exceptions\BaseException;
24
use Quantum\Loader\Setup;
25
use ReflectionException;
26
27
/**
28
 * Class Database
29
 * @package Quantum\Libraries\Database
30
 */
31
class Database
32
{
33
34
    use RelationalTrait;
35
36
    const ADAPTERS = [
37
        'sleekdb' => SleekDbal::class,
38
        'mysql' => IdiormDbal::class,
39
        'sqlite' => IdiormDbal::class,
40
        'pgsql' => IdiormDbal::class,
41
    ];
42
43
    /**
44
     * Database configurations
45
     * @var array
46
     */
47
    private $configs;
48
49
    /**
50
     * Database instance
51
     * @var Database|null
52
     */
53
    private static $instance = null;
54
55
    /**
56
     * @var string
57
     */
58
    private $ormClass;
59
60
    /**
61
     * @throws BaseException
62
     * @throws ConfigException
63
     * @throws DiException
64
     * @throws ReflectionException
65
     */
66
    private function __construct()
67
    {
68
        if (!config()->has('database')) {
69
            config()->import(new Setup('config', 'database'));
70
        }
71
72
        $adapter = config()->get('database.default');
73
74
        $this->ormClass = self::getAdapterClass($adapter);
0 ignored issues
show
Bug introduced by
It seems like $adapter can also be of type null; however, parameter $adapter of Quantum\Libraries\Databa...base::getAdapterClass() 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

74
        $this->ormClass = self::getAdapterClass(/** @scrutinizer ignore-type */ $adapter);
Loading history...
75
76
        $this->configs = config()->get('database.' . $adapter);
77
78
        if (!$this->ormClass::getConnection()) {
79
            $this->ormClass::connect($this->configs);
80
        }
81
    }
82
83
    /**
84
     * Get Instance
85
     * @return Database
86
     */
87
    public static function getInstance(): Database
88
    {
89
        if (self::$instance === null) {
90
            self::$instance = new self();
91
        }
92
93
        return self::$instance;
94
    }
95
96
    /**
97
     * Gets the ORM class
98
     * @return string
99
     */
100
    public function getOrmClass(): string
101
    {
102
        return $this->ormClass;
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
     * @param string $adapter
116
     * @return string
117
     * @throws BaseException
118
     */
119
    private static function getAdapterClass(string $adapter): string
120
    {
121
        if (!array_key_exists($adapter, self::ADAPTERS)) {
122
            throw DatabaseException::adapterNotSupported($adapter);
123
        }
124
125
        return self::ADAPTERS[$adapter];
126
    }
127
}