Passed
Push — master ( 2c321e...f12c68 )
by Henri
01:20
created

Connect   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
b 0
f 0
dl 0
loc 28
rs 10
wmc 5
1
<?php
2
3
namespace HnrAzevedo\Datamanager;
4
5
use PDO;
6
use Exception;
7
use HnrAzevedo\Datamanager\DatamanagerException;
8
9
class Connect
10
{
11
    private static $instance;
12
13
    public static function getInstance(): ?PDO
14
    {
15
        if (empty(self::$instance)) {
16
            try {
17
18
                if(!defined('DATAMANAGER_CONFIG')){
19
                    throw new DatamanagerException("Information for connection to the database not defined.");
20
                }
21
22
                self::$instance = new PDO(
23
                    DATAMANAGER_CONFIG['driver'] . ':host='.DATAMANAGER_CONFIG['host'] . ';port='.DATAMANAGER_CONFIG['port'] . ';dbname='.DATAMANAGER_CONFIG['database'] . ';charset='.DATAMANAGER_CONFIG['charset'],
24
                    DATAMANAGER_CONFIG['username'],
25
                    DATAMANAGER_CONFIG['password'],
26
                    DATAMANAGER_CONFIG['options']
27
                );
28
            } catch (Exception $exception) {
29
                throw new DatamanagerException(str_replace(['SQLSTATE[HY000]',"[{$exception->getCode()}]"], '', $exception->getMessage()), $exception->getCode(), $exception);
30
            }
31
        }
32
        return self::$instance;
33
    }
34
35
    public static function destroy(){
36
        self::$instance = null;
37
    }
38
39
}
40