Connect::getConnection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 2
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Silviooosilva\CacheerPhp\Core;
4
5
use PDO;
6
use PDOException;
7
use Silviooosilva\CacheerPhp\Exceptions\ConnectionException;
8
9
/**
10
 * Class Connect
11
 * @author Sílvio Silva <https://github.com/silviooosilva>
12
 * @package Silviooosilva\CacheerPhp
13
 */
14
class Connect
15
{
16
    public static string $connection = 'sqlite';
17
    private static ?PDOException $error = null;
18
19
20
    /**
21
    * @param array|null $database
22
    * @return PDO|null
23
    */
24
    public static function getInstance(?array $database = null)
25
    {
26
        $pdo = ConnectionFactory::createConnection($database);
27
        if ($pdo) {
0 ignored issues
show
introduced by
$pdo is of type PDO, thus it always evaluated to true.
Loading history...
28
            MigrationManager::migrate($pdo);
29
        }
30
        return $pdo;
31
    }
32
33
    /**
34
    * @param string $connection
35
    * @return void
36
    */
37
    public static function setConnection(string $connection)
38
    {
39
        $drivers = ['mysql', 'sqlite', 'pgsql'];
40
        if (!in_array($connection, $drivers)) {
41
            throw ConnectionException::create("Only ['MySQL(mysql)', 'SQLite(sqlite)', 'PgSQL(pgsql)'] are available at the moment...");
42
        }
43
        self::$connection = $connection;
44
    }
45
46
    /**
47
    * @return string
48
    */
49
    public static function getConnection()
50
    {
51
        return self::$connection;
52
    }
53
54
    /**
55
    * @return PDOException|null
56
    */
57
    public static function getError()
58
    {
59
        return self::$error;
60
    }
61
62
    private function __construct() {}
63
    private function __clone() {}
64
}
65