Database::prepare()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
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) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
			$nb = 0;
95
			foreach ($query as $obj) {
96
				$nb = $obj["nb"];
97
			}
98
			
99
			if ($nb > 0) {
100
				return true;
101
			}
102
			
103
			return false;
104
		}
105
		//-------------------------- FIN FUNCTION QUI FONT DES REQUETES SUR LA BDD --------------------------------------------//
106
		public function quote($quote) {
107
			return $this->getPdo()->quote($quote);
108
		}
109
110
		public function lastInsertId() {
111
			return $this->getPdo()->lastInsertId();
112
		}
113
	}