Completed
Push — master ( 5924b0...d78793 )
by Ivan
02:02
created

Driver::test()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 8
nc 3
nop 0
1
<?php
2
3
namespace vakata\database\driver\pdo;
4
5
use \vakata\database\DBException;
6
use \vakata\database\DriverInterface;
7
use \vakata\database\DriverAbstract;
8
use \vakata\database\StatementInterface;
9
use \vakata\database\schema\Table;
10
use \vakata\database\schema\TableRelation;
11
12
class Driver extends DriverAbstract implements DriverInterface
13
{
14
    protected $lnk = null;
15
16
    public function __construct(array $connection)
17
    {
18
        $temp = explode('://', $connection['orig'], 2)[1];
19
        $temp = array_pad(explode('?', $temp, 2), 2, '');
20
        $connection = [];
21
        parse_str($temp[1], $connection['opts']);
22
        $temp = $temp[0];
23
        if (strpos($temp, '@') !== false) {
24
            $temp = array_pad(explode('@', $temp, 2), 2, '');
25
            list($connection['user'], $connection['pass']) = array_pad(explode(':', $temp[0], 2), 2, '');
26
            $temp = $temp[1];
27
        }
28
        $connection['dsn'] = $temp;
29
        $this->connection = $connection;
30
    }
31
    public function __destruct()
32
    {
33
        $this->disconnect();
34
    }
35
    protected function connect()
36
    {
37
        if ($this->lnk === null) {
38
            try {
39
                $this->lnk = new \PDO(
40
                    $this->connection['dsn'],
41
                    isset($this->connection['user']) ? $this->connection['user'] : '',
42
                    isset($this->connection['pass']) ? $this->connection['pass'] : '',
43
                    isset($this->connection['opts']) ? $this->connection['opts'] : []
44
                );
45
            } catch (\PDOException $e) {
46
                throw new DBException('Connect error: ' . $e->getMessage());
47
            }
48
        }
49
    }
50
    public function test() : bool
51
    {
52
        if ($this->lnk) {
53
            return true;
54
        }
55
        try {
56
            $this->connect();
57
            return true;
58
        } catch (\Exception $e) {
59
            return false;
60
        }
61
    }
62
    protected function disconnect()
63
    {
64
        $this->lnk = null;
65
    }
66
    public function prepare(string $sql) : StatementInterface
67
    {
68
        $this->connect();
69
        try {
70
            return new Statement($this->lnk->prepare($sql), $this->lnk);
71
        } catch (\PDOException $e) {
72
            throw new DBException($e->getMessage());
73
        }
74
    }
75
76
    public function begin() : bool
77
    {
78
        $this->connect();
79
        return $this->lnk->beginTransaction();
80
    }
81
    public function commit() : bool
82
    {
83
        $this->connect();
84
        return $this->lnk->commit();
85
    }
86
    public function rollback() : bool
87
    {
88
        $this->connect();
89
        return $this->lnk->rollback();
90
    }
91
}