Passed
Push — master ( ef436a...1feae4 )
by Anthony
02:41
created

Messagerie::getIdIdentiteExist()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 3
nop 1
1
<?php
2
	namespace modules\messagerie\app\controller;
3
4
	use core\App;
5
	use core\functions\ChaineCaractere;
6
7
	class Messagerie {
8
		public static $url_message;
9
10
		private $id_message;
11
		private $objet;
12
		private $message;
13
		private $date_message;
14
		private $url;
15
16
		private $id_expediteur;
17
		private $pseudo_expediteur;
18
19
		private $pseudo_receveur;
20
		private $id_receveur;
21
22
		private $values = [];
23
		
24
		
25
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
26
		/**
27
		 * @param null $type_boite
28
		 * initialisation de la récupération des messages des différents boites
29
		 */
30
		public function __construct($type_boite = null) {
31
			$dbc = App::getDb();
0 ignored issues
show
Unused Code introduced by
$dbc is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
32
33
			if ($type_boite !== null) {
34
				if ($type_boite == "boite réception") {
35
					$this->getBoiteReception();
36
				}
37
				else if ($type_boite == "messages envoyés") {
38
					$this->getMessagesEnvoyes();
39
				}
40
				else if ($type_boite == "messages supprimés") {
41
					$this->getMessageSupprimes();
42
				}
43
44
				//on check les messages supprimes d'il y a plus de 15 jours et on les delete de la bdd
45
				//a changer fonction qui se lancera une fois par jour et qui degagera tous les messages
46
				//supprimes de plus vieux que 15 jours
47
				/*$today = new \DateTime();
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
48
				$date_del =  $today->sub(new \DateInterval("P15D"))->format("Y-m-d H:i:s");
49
50
				$query = $dbc->select("_messagerie_message.ID_message")
51
					->from("_messagerie_boite_reception")
52
					->from("_messagerie_message")
53
					->where("_messagerie_message.date", "<", $date_del, "AND")
54
					->where("(_messagerie_boite_reception.ID_identite = 1 OR _messagerie_message.ID_expediteur = 1)", "", "", "AND", true)
55
					->where("_messagerie_boite_reception.ID_message", "=", "_messagerie_message.ID_message", "", true)
56
					->get();
57
58
				if ((is_array($query)) && (count($query) > 0)) {
59
					foreach ($query as $obj) {
60
						$this->setDeleteMessageBySystem($obj->ID_message);
61
					}
62
				}*/
63
			}
64
		}
65
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
66
		
67
		
68
		
69
		//-------------------------- GETTER ----------------------------------------------------------------------------//
70
		public function getIdMessage() {
71
			return $this->id_message;
72
		}
73
		public function getObjet() {
74
			return $this->objet;
75
		}
76
		public function getMessage() {
77
			return $this->message;
78
		}
79
		public function getDateMessage(){
80
		    return $this->date_message;
81
		}
82
		public function getUrl(){
83
		    return $this->url;
84
		}
85
		public function getIdExpediteur() {
86
			return $this->id_expediteur;
87
		}
88
		public function getPseudoExpediteur() {
89
			return $this->pseudo_expediteur;
90
		}
91
		public function getIdReceveur() {
92
			return $this->id_receveur;
93
		}
94
		public function getPseudoReceveur() {
95
			return $this->pseudo_receveur;
96
		}
97
		public function getValues(){
98
		    return ["messagerie" => $this->values];
99
		}
100
101
		/**
102
		 * fonction qui permet de récupérer tous les messages dans la boite de récéption
103
		 */
104 View Code Duplication
		private function getBoiteReception() {
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...
105
			$dbc = App::getDb();
106
107
			$query = $dbc->select()
108
				->from("_messagerie_boite_reception")
109
				->from("_messagerie_message")
110
				->from("identite")
111
				->where("_messagerie_boite_reception.ID_identite", "=", 1, "AND")
112
				->where("_messagerie_boite_reception.supprimer", " IS ", "NULL", "AND", true)
113
				->where("_messagerie_boite_reception.ID_message", "=", "_messagerie_message.ID_message", "AND", true)
114
				->where("_messagerie_message.ID_expediteur", "=", "identite.ID_identite", "", true)
115
				->get();
116
117
			if ((is_array($query)) && (count($query) > 0)) {
118
				foreach ($query as $obj) {
119
					$arr = [
120
						"id_message" => $obj->ID_message,
121
						"objet" => $obj->objet,
122
						"date_message" => $obj->date,
123
						"id_expediteur" => $obj->ID_expediteur,
124
						"pseudo_expediteur" => $obj->pseudo,
125
						"url" => $obj->url
126
					];
127
128
					$this->values[] = $arr;
129
				}
130
			}
131
		}
132
133
		/**
134
		 * fonction qui permet de récupérer tous les messages dans la boite des messages envoyes
135
		 */
136 View Code Duplication
		private function getMessagesEnvoyes() {
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...
137
			$dbc = App::getDb();
138
139
			$query = $dbc->select()
140
				->from("_messagerie_boite_reception")
141
				->from("_messagerie_message")
142
				->from("identite")
143
				->where("_messagerie_message.ID_expediteur", "=", 1, "AND")
144
				->where("_messagerie_boite_reception.ID_message", "=", "_messagerie_message.ID_message", "AND", true)
145
				->where("_messagerie_boite_reception.ID_identite", "=", "identite.ID_identite", "", true)
146
				->get();
147
148
			if ((is_array($query)) && (count($query) > 0)) {
149
				foreach ($query as $obj) {
150
					$arr = [
151
						"id_message" => $obj->ID_message,
152
						"objet" => $obj->objet,
153
						"date_message" => $obj->date,
154
						"id_expediteur" => $obj->ID_expediteur,
155
						"pseudo_receveur" => $obj->pseudo,
156
						"url" => $obj->url
157
					];
158
159
					$this->values[] = $arr;
160
				}
161
			}
162
		}
163
164
		/**
165
		 * fonction qui récupère tous les messages supprimés
166
		 */
167 View Code Duplication
		private function getMessageSupprimes() {
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...
168
			$dbc = App::getDb();
169
170
			$query = $dbc->select()
171
				->from("_messagerie_boite_reception")
172
				->from("_messagerie_message")
173
				->from("identite")
174
				->where("_messagerie_boite_reception.ID_identite", "=", 1, "AND")
175
				->where("_messagerie_boite_reception.supprimer", "=", 1, "AND")
176
				->where("_messagerie_boite_reception.ID_message", "=", "_messagerie_message.ID_message", "AND", true)
177
				->where("_messagerie_message.ID_expediteur", "=", "identite.ID_identite", "", true)
178
				->get();
179
180
			if ((is_array($query)) && (count($query) > 0)) {
181
				foreach ($query as $obj) {
182
					$arr = [
183
						"id_message" => $obj->ID_message,
184
						"objet" => $obj->objet,
185
						"date_message" => $obj->date,
186
						"id_expediteur" => $obj->ID_expediteur,
187
						"pseudo_expediteur" => $obj->pseudo,
188
						"url" => $obj->url
189
					];
190
191
					$this->values[] = $arr;
192
				}
193
			}
194
		}
195
196
		/*
197
		 * fonction qui permetlors de l'envoit d'un message d'être sur que le membre existe
198
		 */
199 View Code Duplication
		private function getIdIdentiteExist($pseudo) {
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...
200
			$dbc = App::getDb();
201
202
			$pseudo = trim($pseudo);
203
204
			$query = $dbc->select("ID_identite")->from("identite")->where("pseudo", "=", $pseudo)->get();
205
206
			if ((count($query) == 1) && (is_array($query))) {
207
				foreach ($query as $obj) {
208
					return $obj->ID_identite;
209
				}
210
			}
211
212
			return false;
213
		}
214
215
		/**
216
		 * @param $url_message
217
		 * fonction qui récupère un message suivant une url
218
		 */
219 View Code Duplication
		public function getUnMessage($url_message) {
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...
220
			$dbc = App::getDb();
221
222
			$query = $dbc->select()
223
				->from("_messagerie_message")
224
				->from("_messagerie_boite_reception")
225
				->from("identite")
226
				->where("_messagerie_message.url", "=", $url_message, "AND")
227
				->where("_messagerie_message.ID_expediteur", "=", "identite.ID_identite", "AND", true)
228
				->where("_messagerie_boite_reception.ID_message", "=", "_messagerie_message.ID_message", "", true)
229
				->get();
230
231
			if ((is_array($query)) && (count($query) > 0)) {
232
				foreach ($query as $obj) {
233
					$this->values = [
234
						"id_message" => $obj->ID_message,
235
						"objet" => $obj->objet,
236
						"date_message" => $obj->date,
237
						"id_expediteur" => $obj->ID_expediteur,
238
						"pseudo_expediteur" => $obj->pseudo,
239
						"url" => $obj->url,
240
						"supprimer" => $obj->supprimer
241
					];
242
				}
243
			}
244
			else {
245
				return false;
246
			}
247
		}
248
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
249
		
250
		
251
		
252
		//-------------------------- SETTER ----------------------------------------------------------------------------//
253
		/**
254
		 * @param $id_message
255
		 * pour passer le message en supprimé, il sera alors consultable dans la table messages supprimés
256
		 */
257 View Code Duplication
		public function setArchiverMessage($id_message) {
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...
258
			$dbc = App::getDb();
259
260
			$dbc->update("supprimer", 1)->from("_messagerie_boite_reception")
261
				->where("ID_message", "=", $id_message, "AND")
262
				->where("ID_identite", "=", 1)
263
				->set();
264
		}
265
266
		/**
267
		 * @param $id_message
268
		 */
269 View Code Duplication
		public function setSupprimerMessage($id_message) {
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...
Coding Style introduced by
setSupprimerMessage uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
270
			$dbc = App::getDb();
271
272
			$dbc->delete()->from("_messagerie_boite_reception")
273
				->where("ID_message", "=", $id_message, "AND")
274
				->where("ID_identite", "=", $_SESSION['idlongin'])
275
				->del();
276
		}
277
278
		/**
279
		 * @param $objet
280
		 * @param $destinataire
281
		 * @param $message
282
		 * @return bool
283
		 *
284
		 * fonction qui sert à envoyer un message à un ou plusieurs destinataires
285
		 */
286
		public function setEnvoyerMessage($objet, $destinataire, $message) {
287
			$dbc = App::getDb();
288
289
			//on test si un ou plusieurs destinataires ++ si ils existent
290
			if (ChaineCaractere::FindInString($destinataire, ",")) {
291
				$destinataires = explode(",", $destinataire);
292
				$c = count($destinataires);
293
294
				for ($i=0 ; $i<$c ; $i++) {
295
					if ($this->getIdIdentiteExist($destinataires[$i]) !== false) {
296
						$destinataires[] = $this->getIdIdentiteExist($destinataires[$i]);
297
					}
298
					else {
299
						return false;
300
					}
301
				}
302
			}
303
			else {
304
				if ($this->getIdIdentiteExist($destinataire) !== false) {
305
					$destinataires[] = $this->getIdIdentiteExist($destinataire);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$destinataires was never initialized. Although not strictly required by PHP, it is generally a good practice to add $destinataires = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
306
				}
307
				else {
308
					return false;
309
				}
310
			}
311
312
			//cela veut dire qu'on a au moin 1 membre à qui envoyer le message
313
			if (count($destinataires) > 0) {
314
				$dbc->insert("message", $message)
315
					->insert("objet", $objet)
316
					->insert("url", ChaineCaractere::setUrl($objet))
317
					->insert("date", date("Y-m-d H:i:s"))
318
					->insert("ID_expediteur", 1)
319
					->into("_messagerie_message")
320
					->set();
321
322
				$id_message = $dbc->lastInsertId();
323
324
				foreach ($destinataires as $destinataire) {echo("gddg $destinataire");
325
					$dbc->insert("ID_identite", $destinataire)
326
						->insert("ID_message", $id_message)
327
						->into("_messagerie_boite_reception")
328
						->set();
329
				}
330
331
				return true;
332
			}
333
334
			return false;
335
		}
336
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
337
	}