Passed
Branch scrutinizer (dafd44)
by Wanderson
01:40
created

Database   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 2 2
A validate() 0 4 2
A connect() 0 7 2
A getPDO() 0 2 1
1
<?php
2
3
namespace Win\Connection;
4
5
use PDO;
6
use PDOException;
7
use Win\DesignPattern\Singleton;
8
use Win\Mvc\Application;
9
10
/**
11
 * Conexão com banco de dados
12
 *
13
 */
14
abstract class Database {
15
16
	use Singleton;
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
	 * @return PDO
28
	 */
29
	abstract protected function createPdo(&$dbConfig);
30
31
	/** @return PDO */
32
	final public function getPDO() {
33
		return $this->pdo;
34
	}
35
36
	/**
37
	 * Cria uma conexão com um banco de dados
38
	 * @param string[] $dbConfig
39
	 */
40
	public function connect($dbConfig) {
41
		try {
42
			$this->pdo = $this->createPdo($dbConfig);
43
			$this->pdo->exec("set names utf8");
44
			$this->pdoException = null;
45
		} catch (PDOException $ex) {
46
			$this->pdoException = $ex;
47
		}
48
	}
49
50
	/**
51
	 * Retorna TRUE caso a conexão tenha sido bem sucedida
52
	 * @return boolean
53
	 */
54
	public function isValid() {
55
		return (is_null($this->pdoException) && $this->pdo instanceof \PDO);
56
	}
57
58
	/**
59
	 * Redireciona para 503 caso a conexão tenha falhado
60
	 */
61
	public function validate() {
62
		if (!$this->isValid()):
63
			Application::app()->errorPage(503);
64
			Application::app()->view->addData('error', $this->pdoException->getMessage());
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on null. ( Ignorable by Annotation )

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

64
			Application::app()->view->addData('error', $this->pdoException->/** @scrutinizer ignore-call */ getMessage());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
		endif;
66
	}
67
68
}
69