Completed
Push — master ( 670622...a5ffbc )
by Edson
01:42
created

Database::fetch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Keep;
4
5
use PDO;
6
use PDOStatement;
7
use stdClass;
8
9
class Database
10
{
11
    use Table;
12
    use Clause;
13
    use Keyword;
14
    use Operators;
15
    use Statement;
16
17
    private $conn;
18
    private $exec;
19
20
    public function connection(Driver $driver): PDO
21
    {
22
        return $this->conn = new PDO($driver->getDsn(), $driver->getUser(), $driver->getPass());
23
    }
24
25
    public function prepare(): Database
26
    {
27
        $this->exec = $this->conn->prepare($this->getQuery());
28
        return $this;
29
    }
30
31
    public function bind(): Database
32
    {
33
        foreach ($this->attributes as $key => $value) {
34
            $this->exec->bindValue(":{$key}", $value);
35
        }
36
37
        return $this;
38
    }
39
40
    public function fetch()
41
    {
42
        return $this->exec->fetch(PDO::FETCH_OBJ);
43
    }
44
45
    public function fetchAll()
46
    {
47
        return $this->exec->fetchAll(PDO::FETCH_OBJ);
48
    }
49
50
    public function exec()
51
    {
52
        if ($this->exec->execute()) {
53
            return $this->exec;
54
        }
55
56
        return null;
57
    }
58
59
    public function execute()
60
    {
61
        $execute = $this->prepare()->bind()->exec();
62
63
        $this->reset();
64
65
        return $execute;
66
    }
67
68
    public function getQuery(): string
69
    {
70
        return trim($this->statement.' '.$this->clause.' '.$this->operators.' '.$this->keyword);
71
    }
72
73
    public function reset()
74
    {
75
        $this->statement = '';
76
        $this->clause = '';
77
        $this->operators = '';
78
        $this->keyword = '';
79
        $this->attributes = [];
80
    }
81
}
82