Connect   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 17
c 4
b 0
f 0
dl 0
loc 59
rs 10
wmc 8

5 Methods

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