Passed
Push — main ( 573444...d5439f )
by Julio
01:40
created

Connect::getInstance()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 15
rs 9.9332
1
<?php
2
3
namespace CodefusionTM\DataLayer;
4
5
use PDO;
6
use PDOException;
7
8
/**
9
 * Class Connect
10
 * @package CodefusionTM\DataLayer
11
 */
12
class Connect
13
{
14
    /** @var PDO */
15
    private static $instance;
16
17
    /** @var PDOException */
18
    private static $error;
19
20
    /**
21
     * Connect constructor.
22
     */
23
    private function __construct()
24
    {
25
    }
26
27
    /**
28
     * @return PDO
29
     */
30
    public static function getInstance(): ?PDO
31
    {
32
        if (empty(self::$instance)) {
33
            try {
34
                self::$instance = new PDO(
35
                    DLC["driver"] . ":host=" . DLC["host"] . ";dbname=" . DLC["dbname"] . ";port=" . DLC["port"],
36
                    DLC["username"],
37
                    DLC["passwd"],
38
                    DLC["options"]
39
                );
40
            } catch (PDOException $exception) {
41
                self::$error = $exception;
42
            }
43
        }
44
        return self::$instance;
45
    }
46
47
    /**
48
     * @return PDOException|null
49
     */
50
    public static function getError(): ?PDOException
51
    {
52
        return self::$error;
53
    }
54
55
    /**
56
     * Connect clone.
57
     */
58
    private function __clone()
59
    {
60
    }
61
}