Passed
Branch v1.4.0 (c6dc92)
by Wanderson
01:17
created

Connection   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getPdo() 0 3 1
A fetchCount() 0 5 1
A query() 0 5 1
A fetchAll() 0 5 1
A connect() 0 9 2
A validate() 0 4 2
A stmt() 0 6 1
A isValid() 0 3 2
A getLastInsertId() 0 3 1
1
<?php
2
3
namespace Win\Database;
4
5
use PDO;
6
use PDOException;
7
use PDOStatement;
8
use Win\Mvc\Application;
9
use Win\Singleton\SingletonTrait;
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|null */
22
	protected $pdoException;
23
24
	/**
25
	 * Cria e retorna conexão PDO
26
	 * @param string[] $dbConfig
27
	 * @throws PDOException
28
	 * @return PDO
29
	 */
30
	abstract protected function createPdo(&$dbConfig);
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
			Orm::setConnection($this);
49
		} catch (PDOException $ex) {
50
			$this->pdoException = $ex;
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
	 * Redireciona para 503 caso a conexão tenha falhado
65
	 */
66
	public function validate()
67
	{
68
		if (!is_null($this->pdoException)) {
69
			Application::app()->errorPage(503, $this->pdoException->getMessage());
70
		}
71
	}
72
73
	/**
74
	 * @param string $query
75
	 * @param string $values
76
	 * @return bool
77
	 */
78
	public function query($query, $values = [])
79
	{
80
		$stmt = $this->pdo->prepare($query);
81
82
		return $stmt->execute($values);
0 ignored issues
show
Bug introduced by
It seems like $values can also be of type string; however, parameter $input_parameters of PDOStatement::execute() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
		return $stmt->execute(/** @scrutinizer ignore-type */ $values);
Loading history...
83
	}
84
85
	/**
86
	 * @param string $query
87
	 * @param string $values
88
	 * @return PDOStatement|null
89
	 */
90
	public function stmt($query, $values = [])
91
	{
92
		$stmt = $this->pdo->prepare($query);
93
		$stmt->execute($values);
0 ignored issues
show
Bug introduced by
It seems like $values can also be of type string; however, parameter $input_parameters of PDOStatement::execute() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
		$stmt->execute(/** @scrutinizer ignore-type */ $values);
Loading history...
94
95
		return $stmt;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $stmt also could return the type boolean which is incompatible with the documented return type PDOStatement|null.
Loading history...
96
	}
97
98
	/**
99
	 * @param string $query
100
	 * @param string $values
101
	 * @return mixed[]
102
	 */
103
	public function fetchAll($query, $values = [])
104
	{
105
		$stmt = $this->stmt($query, $values);
0 ignored issues
show
Bug introduced by
It seems like $values can also be of type array; however, parameter $values of Win\Database\Connection::stmt() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

105
		$stmt = $this->stmt($query, /** @scrutinizer ignore-type */ $values);
Loading history...
106
107
		return $stmt->fetchAll(PDO::FETCH_ASSOC);
108
	}
109
110
	/**
111
	 * @param string $query
112
	 * @param mixed[] $values
113
	 * @return int
114
	 */
115
	public function fetchCount($query, $values)
116
	{
117
		$stmt = $this->stmt($query, $values);
0 ignored issues
show
Bug introduced by
$values of type array<mixed,mixed> is incompatible with the type string expected by parameter $values of Win\Database\Connection::stmt(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

117
		$stmt = $this->stmt($query, /** @scrutinizer ignore-type */ $values);
Loading history...
118
119
		return $stmt->fetchColumn();
120
	}
121
122
	/** @return string */
123
	public function getLastInsertId()
124
	{
125
		return $this->pdo->lastInsertId();
126
	}
127
}
128