Passed
Push — master ( 0ed5cc...1c5a7a )
by Anthony
07:34
created

Messagerie::setEnvoyerMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 3
1
<?php
2
	namespace modules\messagerie\app\controller;
3
4
	use core\App;
5
6
	class Messagerie {
7
		public static $url_message;
8
9
		private $id_message;
10
		private $objet;
11
		private $message;
12
		private $date_message;
13
		private $url;
14
15
		private $id_expediteur;
16
		private $pseudo_expediteur;
17
18
		private $pseudo_receveur;
19
		private $id_receveur;
20
21
		private $values = [];
22
		
23
		
24
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
25
		/**
26
		 * @param null $type_boite
27
		 * initialisation de la récupération des messages des différents boites
28
		 */
29
		public function __construct($type_boite = null) {
30
			$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...
31
32
			if ($type_boite !== null) {
33
				if ($type_boite == "boite réception") {
34
					$this->getBoiteReception();
35
				}
36
				else if ($type_boite == "messages envoyés") {
37
					$this->getMessagesEnvoyes();
38
				}
39
				else if ($type_boite == "messages supprimés") {
40
					$this->getMessageSupprimes();
41
				}
42
43
				//on check les messages supprimes d'il y a plus de 15 jours et on les delete de la bdd
44
				//a changer fonction qui se lancera une fois par jour et qui degagera tous les messages
45
				//supprimes de plus vieux que 15 jours
46
				/*$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...
47
				$date_del =  $today->sub(new \DateInterval("P15D"))->format("Y-m-d H:i:s");
48
49
				$query = $dbc->select("_messagerie_message.ID_message")
50
					->from("_messagerie_boite_reception")
51
					->from("_messagerie_message")
52
					->where("_messagerie_message.date", "<", $date_del, "AND")
53
					->where("(_messagerie_boite_reception.ID_identite = 1 OR _messagerie_message.ID_expediteur = 1)", "", "", "AND", true)
54
					->where("_messagerie_boite_reception.ID_message", "=", "_messagerie_message.ID_message", "", true)
55
					->get();
56
57
				if ((is_array($query)) && (count($query) > 0)) {
58
					foreach ($query as $obj) {
59
						$this->setDeleteMessageBySystem($obj->ID_message);
60
					}
61
				}*/
62
			}
63
		}
64
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
65
		
66
		
67
		
68
		//-------------------------- GETTER ----------------------------------------------------------------------------//
69
		public function getIdMessage() {
70
			return $this->id_message;
71
		}
72
		public function getObjet() {
73
			return $this->objet;
74
		}
75
		public function getMessage() {
76
			return $this->message;
77
		}
78
		public function getDateMessage(){
79
		    return $this->date_message;
80
		}
81
		public function getUrl(){
82
		    return $this->url;
83
		}
84
		public function getIdExpediteur() {
85
			return $this->id_expediteur;
86
		}
87
		public function getPseudoExpediteur() {
88
			return $this->pseudo_expediteur;
89
		}
90
		public function getIdReceveur() {
91
			return $this->id_receveur;
92
		}
93
		public function getPseudoReceveur() {
94
			return $this->pseudo_receveur;
95
		}
96
		public function getValues(){
97
		    return ["messagerie" => $this->values];
98
		}
99
100
		/**
101
		 * fonction qui permet de récupérer tous les messages dans la boite de récéption
102
		 */
103
		private function getBoiteReception() {
104
			$dbc = App::getDb();
105
106
			$query = $dbc->select()
107
				->from("_messagerie_boite_reception")
108
				->from("_messagerie_message")
109
				->from("identite")
110
				->where("_messagerie_boite_reception.ID_identite", "=", 1, "AND")
111
				->where("_messagerie_boite_reception.supprimer", " IS ", "NULL", "AND", true)
112
				->where("_messagerie_boite_reception.ID_message", "=", "_messagerie_message.ID_message", "AND", true)
113
				->where("_messagerie_message.ID_expediteur", "=", "identite.ID_identite", "", true)
114
				->get();
115
116
			if ((is_array($query)) && (count($query) > 0)) {
117
				foreach ($query as $obj) {
118
					$arr = [
119
						"id_message" => $obj->ID_message,
120
						"objet" => $obj->objet,
121
						"date_message" => $obj->date,
122
						"id_expediteur" => $obj->ID_expediteur,
123
						"pseudo_expediteur" => $obj->pseudo,
124
						"url" => $obj->url
125
					];
126
127
					$this->values[] = $arr;
128
				}
129
			}
130
		}
131
132
		/**
133
		 * fonction qui permet de récupérer tous les messages dans la boite des messages envoyes
134
		 */
135 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...
136
			$dbc = App::getDb();
137
138
			$query = $dbc->select()
139
				->from("_messagerie_boite_reception")
140
				->from("_messagerie_message")
141
				->from("identite")
142
				->where("_messagerie_message.ID_expediteur", "=", 1, "AND")
143
				->where("_messagerie_boite_reception.ID_message", "=", "_messagerie_message.ID_message", "AND", true)
144
				->where("_messagerie_boite_reception.ID_identite", "=", "identite.ID_identite", "", true)
145
				->get();
146
147
			if ((is_array($query)) && (count($query) > 0)) {
148
				foreach ($query as $obj) {
149
					$arr = [
150
						"id_message" => $obj->ID_message,
151
						"objet" => $obj->objet,
152
						"date_message" => $obj->date,
153
						"id_expediteur" => $obj->ID_expediteur,
154
						"pseudo_receveur" => $obj->pseudo,
155
						"url" => $obj->url
156
					];
157
158
					$this->values[] = $arr;
159
				}
160
			}
161
		}
162
163
		/**
164
		 * fonction qui récupère tous les messages supprimés
165
		 */
166
		private function getMessageSupprimes() {
167
			$dbc = App::getDb();
168
169
			$query = $dbc->select()
170
				->from("_messagerie_boite_reception")
171
				->from("_messagerie_message")
172
				->from("identite")
173
				->where("_messagerie_boite_reception.ID_identite", "=", 1, "AND")
174
				->where("_messagerie_boite_reception.supprimer", "=", 1, "AND")
175
				->where("_messagerie_boite_reception.ID_message", "=", "_messagerie_message.ID_message", "AND", true)
176
				->where("_messagerie_message.ID_expediteur", "=", "identite.ID_identite", "", true)
177
				->get();
178
179
			if ((is_array($query)) && (count($query) > 0)) {
180
				foreach ($query as $obj) {
181
					$id_message[] = $obj->ID_message;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$id_message was never initialized. Although not strictly required by PHP, it is generally a good practice to add $id_message = 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...
182
					$objet[] = $obj->objet;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$objet was never initialized. Although not strictly required by PHP, it is generally a good practice to add $objet = 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...
183
					$date_message[] = $obj->date;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$date_message was never initialized. Although not strictly required by PHP, it is generally a good practice to add $date_message = 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...
184
					$id_expediteur[] = $obj->ID_expediteur;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$id_expediteur was never initialized. Although not strictly required by PHP, it is generally a good practice to add $id_expediteur = 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...
185
					$pseudo_expediteur[] = $obj->pseudo;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$pseudo_expediteur was never initialized. Although not strictly required by PHP, it is generally a good practice to add $pseudo_expediteur = 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...
186
					$url[] = $obj->url;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$url was never initialized. Although not strictly required by PHP, it is generally a good practice to add $url = 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...
187
				}
188
189
				foreach ($query as $obj) {
190
					$arr = [
191
						"id_message" => $obj->ID_message,
192
						"objet" => $obj->objet,
193
						"date_message" => $obj->date,
194
						"id_expediteur" => $obj->ID_expediteur,
195
						"pseudo_expediteur" => $obj->pseudo,
196
						"url" => $obj->url
197
					];
198
199
					$this->values[] = $arr;
200
				}
201
			}
202
		}
203
204
		/**
205
		 * @param $url_message
206
		 * fonction qui récupère un message suivant une url
207
		 */
208 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...
209
			$dbc = App::getDb();
210
211
			$query = $dbc->select()
212
				->from("_messagerie_message")
213
				->from("_messagerie_boite_reception")
214
				->from("identite")
215
				->where("_messagerie_message.url", "=", $url_message, "AND")
216
				->where("_messagerie_message.ID_expediteur", "=", "identite.ID_identite", "AND", true)
217
				->where("_messagerie_boite_reception.ID_message", "=", "_messagerie_message.ID_message", "", true)
218
				->get();
219
220
			if ((is_array($query)) && (count($query) > 0)) {
221
				foreach ($query as $obj) {
222
					$this->values = [
223
						"id_message" => $obj->ID_message,
224
						"objet" => $obj->objet,
225
						"date_message" => $obj->date,
226
						"id_expediteur" => $obj->ID_expediteur,
227
						"pseudo_expediteur" => $obj->pseudo,
228
						"url" => $obj->url
229
					];
230
				}
231
			}
232
			else {
233
				return false;
234
			}
235
		}
236
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
237
		
238
		
239
		
240
		//-------------------------- SETTER ----------------------------------------------------------------------------//
241
		/**
242
		 * @param $id_message
243
		 * pour passer le message en supprimé, il sera alors consultable dans la table messages supprimés
244
		 */
245 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...
246
			$dbc = App::getDb();
247
248
			$dbc->update("supprimer", 1)->from("_messagerie_boite_reception")
249
				->where("ID_message", "=", $id_message, "AND")
250
				->where("ID_identite", "=", 1)
251
				->set();
252
		}
253
254
		/**
255
		 * @param $id_message
256
		 */
257 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...
258
			$dbc = App::getDb();
259
260
			$dbc->delete()->from("_messagerie_boite_reception")
261
				->where("ID_message", "=", $id_message, "AND")
262
				->where("ID_identite", "=", $_SESSION['idlongin'])
263
				->del();
264
		}
265
266
		public function setEnvoyerMessage($objet, $destinataires, $message) {
0 ignored issues
show
Unused Code introduced by
The parameter $objet is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $destinataires is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $message is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
267
268
		}
269
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
270
	}