Passed
Push — master ( 1faf28...95951e )
by Anthony
02:22
created

Database   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 125
Duplicated Lines 16 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 20
loc 125
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
	namespace core\database;
3
4
	use core\HTML\flashmessage\FlashMessage;
5
	use PDO;
6
7
	class Database {
8
		use Querybuilder;
9
10
		private $db_type;
11
		private $db_name;
12
		private $db_user;
13
		private $db_pass;
14
		private $db_host;
15
		private $dbc;
16
17
18
19
		//-------------------------- CONSTRUCTEUR ----------------------------------------------------------------------------//
20
		public function __construct($db_type, $db_name, $db_user, $db_pass, $db_host) {
21
			$this->db_type = $db_type;
22
			$this->db_name = $db_name;
23
			$this->db_user = $db_user;
24
			$this->db_pass = $db_pass;
25
			$this->db_host = $db_host;
26
		}
27
		//-------------------------- FIN CONSTRUCTEUR ----------------------------------------------------------------------------//
28
29
30
31
		//-------------------------- GETTER ----------------------------------------------------------------------------//
32
		/**
33
		 * function qui fait la connexion a la bdd ne peu etre appelee que dans la classe
34
		 * @return PDO
35
		 */
36
		private function getPdo() {
37
			if ($this->dbc === null) {
38
				$dbc = new PDO($this->db_type.':host='.$this->db_host.';dbname='.$this->db_name, $this->db_user, $this->db_pass);
39
				$dbc->exec("set names utf8");
40
				$this->dbc = $dbc;
41
			}
42
			return $this->dbc;
43
		}
44
		//-------------------------- FIN GETTER ----------------------------------------------------------------------------//
45
46
		//-------------------------- FUNCTION QUI FONT DES REQUETES SUR LA BDD --------------------------------------------//
47
		/**
48
		 * effectue une requete en selectr dans la BDD, si ok on renvoit les donnees sinon on renvoi une erreur
49
		 * @param $req
50
		 * @return array
51
		 */
52
		public function query($req) {
53
			$query = $this->getPdo()->query($req);
54
55
			if ($query) {
56
				$obj = $query->fetchAll(PDO::FETCH_OBJ);
57
				return $obj;
58
			}
59
			else {
60
				FlashMessage::setFlash("Une erreur est survenue en executant cette requette : ".$req);
61
			}
62
		}
63
64
		/**
65
		 * fonction qui prepare une requete et qui l'envoi, marche pour insert et update et delete
66
		 * @param $req -> la req a executer
67
		 * @param $value -> le ou les tableaux de valeurs
68
		 */
69
		public function prepare($req, $value) {
70
			$query = $this->getPdo()->prepare($req);
71
			//si on a plusieurs tableaux
72
			if (!$query->execute($value)) {
73
				FlashMessage::setFlash("Une erreur est survenue en executant cette requette : ".$req);
74
			}
75
76
			return $query->fetchAll(PDO::FETCH_OBJ);
77
		}
78
79
		/**
80
		 * pour savoir si une valeur sur un champ précis existe deja en bdd, renvoi true si vrai
81
		 * @param $table
82
		 * @param $champ
83
		 * @param $value
84
		 * @return boolean|null
85
		 */
86
		public function rechercherEgalite($table, $champ, $value, $id_table = null, $id = null) {
87
			if ($id == null) {
88
				$query = $this->getPdo()->query("SELECT COUNT($champ) as nb FROM $table WHERE $champ LIKE '$value'");
89
			}
90
			else {
91
				$query = $this->getPdo()->query("SELECT COUNT($champ) as nb FROM $table WHERE $champ LIKE '$value' AND $id_table != $id");
92
			}
93
94
			if (count($query) > 0) {
95
				foreach ($query as $obj) {
96
					$nb = $obj["nb"];
97
				}
98
99
				if ((isset($nb)) && ($nb != 0)) return true;
100
			}
101
			else {
102
				return false;
103
			}
104
		}
105
		//-------------------------- FIN FUNCTION QUI FONT DES REQUETES SUR LA BDD --------------------------------------------//
106
107
108
		/**
109
		 * tester si une table dans la base donnee existe
110
		 * @param string $table definit la table pour laquelle on doit tester l'existance
111
		 * @return boolean
112
		 */
113
		public function TestTableExist($table) {
114
			$query = $this->getPdo()->query("SHOW TABLES LIKE '$table'");
115
116
			if ($query->rowCount() > 0) {
117
				return true;
118
			}
119
			else {
120
				return false;
121
			}
122
		}
123
124
		public function quote($quote) {
125
			return $this->getPdo()->quote($quote);
126
		}
127
128
		public function lastInsertId() {
129
			return $this->getPdo()->lastInsertId();
130
		}
131
	}