Passed
Branch v1.5.1 (f7b1b3)
by Wanderson
01:50
created

Connection::execute()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 3
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Win\Repositories\Database;
4
5
use PDO;
6
use PDOException;
7
use PDOStatement;
8
use Win\Common\Traits\SingletonTrait;
9
use Win\Request\HttpException;
10
11
/**
12
 * Conexão com banco de dados
13
 */
14
abstract class Connection
15
{
16
	use SingletonTrait;
17
18
	/** @var PDO */
19
	protected $pdo;
20
21
	/** @var PDOException */
22
	public $pdoException;
23
24
	/**
25
	 * Cria e retorna conexão PDO
26
	 * @param string[] $db Configuração de Conexão
27
	 * @throws PDOException
28
	 * @return PDO
29
	 */
30
	abstract protected function createPdo(&$db);
31
32
	/** @return PDO */
33
	public function getPdo()
34
	{
35
		return $this->pdo;
36
	}
37
38
	/**
39
	 * Cria uma conexão com um banco de dados
40
	 * @param string[] $dbConfig
41
	 */
42
	public function connect($dbConfig)
43
	{
44
		try {
45
			$this->pdo = $this->createPdo($dbConfig);
46
			$this->pdo->exec('set names utf8');
47
			$this->pdoException = null;
48
		} catch (\PDOException $e) {
49
			$message = 'Houve um erro ao conectar o banco de dados';
50
			throw new HttpException($message, 503, $e);
51
		}
52
	}
53
54
	/**
55
	 * Retorna TRUE caso a conexão tenha sido bem sucedida
56
	 * @return bool
57
	 */
58
	public function isValid()
59
	{
60
		return is_null($this->pdoException) && $this->pdo instanceof \PDO;
61
	}
62
63
	/**
64
	 * @param string $query
65
	 * @param mixed[] $values
66
	 * @return bool
67
	 */
68
	public function execute($query, $values = [])
69
	{
70
		try {
71
			$stmt = $this->pdo->prepare($query);
72
73
			return $stmt->execute($values);
74
		} catch (PDOException $e) {
75
			throw new DatabaseException($e);
76
		}
77
	}
78
79
	/**
80
	 * @param string $query
81
	 * @param mixed[] $values
82
	 * @return PDOStatement|bool
83
	 */
84
	public function stmt($query, $values = [])
85
	{
86
		try {
87
			$stmt = $this->pdo->prepare($query);
88
			$stmt->execute($values);
89
90
			return $stmt;
91
		} catch (PDOException $e) {
92
			throw new DatabaseException($e);
93
		}
94
	}
95
96
	/**
97
	 * @param string $query
98
	 * @param mixed[] $values
99
	 * @return mixed[]
100
	 */
101
	public function fetchAll($query, $values = [])
102
	{
103
		return $this->stmt($query, $values)->fetchAll(PDO::FETCH_ASSOC);
104
	}
105
106
	/**
107
	 * @param string $query
108
	 * @param mixed[] $values
109
	 * @return mixed[]
110
	 */
111
	public function fetch($query, $values)
112
	{
113
		return $this->stmt($query, $values)->fetch();
114
	}
115
116
	/**
117
	 * @param string $query
118
	 * @param mixed[] $values
119
	 * @return int
120
	 */
121
	public function fetchCount($query, $values)
122
	{
123
		return $this->stmt($query, $values)->fetchColumn();
124
	}
125
126
	/** @return string */
127
	public function getLastInsertId()
128
	{
129
		return $this->pdo->lastInsertId();
130
	}
131
}
132