Connection   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 25
c 1
b 1
f 0
dl 0
loc 109
rs 10
wmc 12

9 Methods

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