Connect   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
eloc 13
c 3
b 1
f 1
dl 0
loc 50
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __clone() 0 1 1
A getConnection() 0 3 1
A setConnection() 0 7 2
A getError() 0 3 1
A getInstance() 0 7 2
A __construct() 0 1 1
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