Passed
Push — main ( 7375bf...02734e )
by Julio
01:50
created

Connect::close()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
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 PDO $instance;
16
17
    /** @var PDOException */
18
    private static PDOException $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
                    self::connectionString(),
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 string
49
     */
50
    private static function connectionString(): string
51
    {
52
        if (DLC["engine"] === "oci") {
53
            return "oci:dbname=" . DLC["dbname"] . ";charset=" . DLC["charset"];
54
        }
55
        return DLC["driver"] . ":host=" . DLC["host"] . ";dbname=" . DLC["dbname"] . ";port=" . DLC["port"];
56
    }
57
58
    /**
59
     * @return PDOException
60
     */
61
    public static function getError(): PDOException
62
    {
63
        return self::$error;
64
    }
65
66
    /**
67
     * Connect clone.
68
     */
69
    private function __clone()
70
    {
71
    }
72
73
}