Completed
Pull Request — master (#4)
by
unknown
09:38 queued 20s
created

Link   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 51
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B getConnection() 0 35 6
1
<?php
2
3
/**
4
 * Classe criada por Márcio Lucas R de Oliveira (lukee)
5
 * E-mail: [email protected]
6
 * Date: 19/10/2016
7
 * Time: 18:50
8
 */
9
namespace Phiber\ORM;
10
11
use PDO;
12
use Phiber\ORM\Exceptions\PhiberException;
13
use Phiber\Util\Internationalization;
14
use Phiber\Util\JsonReader;
15
16
/**
17
 * Classe responsável por criar a conexão do banco.
18
 */
19
class Link
20
{
21
    const PATH_CONFIG_FILE = '/phiber_config.json';
22
23
    /**
24
     * Undocumented variable
25
     *
26
     * @var [type]
27
     */
28
    private $instancia;
29
30
    /**
31
     * Função responsável por fazer a conexão com o banco.
32
     * @throws PhiberException
33
     */
34
    public function getConnection()
35
    {
36
        try {
37
            if ($this->instancia == null) {
38
39
                $json = new JsonReader(BASE_DIR . self::PATH_CONFIG_FILE);
40
41
                if (!empty(glob(dirname(__DIR__, 4) . self::PATH_CONFIG_FILE )[0])) {
42
                    $json = new JsonReader(glob(dirname(__DIR__, 4) . PATH_CONFIG_FILE)[0]);
43
                }
44
45
                $json= $json->read();
46
                try {
47
48
                    $connectionCache = $json->phiber->link->connection_cache == 1 ? true : false;
49
50
                    $this->instancia = new PDO(
51
                        $json->phiber->link->url,
52
                        $json->phiber->link->user,
53
                        $json->phiber->link->password,
54
                        array(
55
                            PDO::ATTR_PERSISTENT => $connectionCache
56
                        )
57
                    );
58
                } catch (PhiberException $e) {
59
                    throw new PhiberException(new Internationalization("database_connection_error"));
60
                }
61
            }
62
63
            return $this->instancia;
64
65
        } catch (PhiberException $e) {
66
            throw new PhiberException(new Internationalization("database_connection_error"));
67
        }
68
    }
69
}