Passed
Push — master ( ab8373...e7b2e8 )
by Anthony
02:31
created

Messagerie::setEnvoyerMessage()   B

Complexity

Conditions 8
Paths 11

Size

Total Lines 56
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 56
rs 7.3333
cc 8
eloc 36
nc 11
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
			if ($type_boite !== null) {
32
				if ($type_boite == "boite réception") {
33
					$this->getBoiteReception();
34
				}
35
				else if ($type_boite == "messages envoyés") {
36
					$this->getMessagesEnvoyes();
37
				}
38
				else if ($type_boite == "messages supprimés") {
39
					$this->getMessageSupprimes();
40
				}
41
			}
42
		}
43
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
44
		
45
		
46
		
47
		//-------------------------- GETTER ----------------------------------------------------------------------------//
48
		public function getIdMessage() {
49
			return $this->id_message;
50
		}
51
		public function getObjet() {
52
			return $this->objet;
53
		}
54
		public function getMessage() {
55
			return $this->message;
56
		}
57
		public function getDateMessage(){
58
		    return $this->date_message;
59
		}
60
		public function getUrl(){
61
		    return $this->url;
62
		}
63
		public function getIdExpediteur() {
64
			return $this->id_expediteur;
65
		}
66
		public function getPseudoExpediteur() {
67
			return $this->pseudo_expediteur;
68
		}
69
		public function getIdReceveur() {
70
			return $this->id_receveur;
71
		}
72
		public function getPseudoReceveur() {
73
			return $this->pseudo_receveur;
74
		}
75
		public function getValues(){
76
		    return ["messagerie" => $this->values];
77
		}
78
79
		/**
80
		 * fonction qui permet de récupérer tous les messages dans la boite de récéption
81
		 */
82 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...
83
			$dbc = App::getDb();
84
85
			$query = $dbc->select()
86
				->from("_messagerie_boite_reception")
87
				->from("_messagerie_message")
88
				->from("identite")
89
				->where("_messagerie_boite_reception.ID_identite", "=", 1, "AND")
90
				->where("_messagerie_boite_reception.supprimer", " IS ", "NULL", "AND", true)
91
				->where("_messagerie_boite_reception.ID_message", "=", "_messagerie_message.ID_message", "AND", true)
92
				->where("_messagerie_message.ID_expediteur", "=", "identite.ID_identite", "", true)
93
				->get();
94
95
			if ((is_array($query)) && (count($query) > 0)) {
96
				foreach ($query as $obj) {
97
					$arr = [
98
						"id_message" => $obj->ID_message,
99
						"objet" => $obj->objet,
100
						"date_message" => $obj->date,
101
						"id_expediteur" => $obj->ID_expediteur,
102
						"pseudo_expediteur" => $obj->pseudo,
103
						"url" => $obj->url
104
					];
105
106
					$this->values[] = $arr;
107
				}
108
			}
109
		}
110
111
		/**
112
		 * fonction qui permet de récupérer tous les messages dans la boite des messages envoyes
113
		 */
114 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...
115
			$dbc = App::getDb();
116
117
			$query = $dbc->select()
118
				->from("_messagerie_boite_reception")
119
				->from("_messagerie_message")
120
				->from("identite")
121
				->where("_messagerie_message.ID_expediteur", "=", 1, "AND")
122
				->where("_messagerie_boite_reception.ID_message", "=", "_messagerie_message.ID_message", "AND", true)
123
				->where("_messagerie_boite_reception.ID_identite", "=", "identite.ID_identite", "", true)
124
				->get();
125
126
			if ((is_array($query)) && (count($query) > 0)) {
127
				foreach ($query as $obj) {
128
					$arr = [
129
						"id_message" => $obj->ID_message,
130
						"objet" => $obj->objet,
131
						"date_message" => $obj->date,
132
						"id_expediteur" => $obj->ID_expediteur,
133
						"pseudo_receveur" => $obj->pseudo,
134
						"url" => $obj->url
135
					];
136
137
					$this->values[] = $arr;
138
				}
139
			}
140
		}
141
142
		/**
143
		 * fonction qui récupère tous les messages supprimés
144
		 */
145 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...
146
			$dbc = App::getDb();
147
148
			$query = $dbc->select()
149
				->from("_messagerie_boite_reception")
150
				->from("_messagerie_message")
151
				->from("identite")
152
				->where("_messagerie_boite_reception.ID_identite", "=", 1, "AND")
153
				->where("_messagerie_boite_reception.supprimer", "=", 1, "AND")
154
				->where("_messagerie_boite_reception.ID_message", "=", "_messagerie_message.ID_message", "AND", true)
155
				->where("_messagerie_message.ID_expediteur", "=", "identite.ID_identite", "", true)
156
				->get();
157
158
			if ((is_array($query)) && (count($query) > 0)) {
159
				foreach ($query as $obj) {
160
					$arr = [
161
						"id_message" => $obj->ID_message,
162
						"objet" => $obj->objet,
163
						"date_message" => $obj->date,
164
						"id_expediteur" => $obj->ID_expediteur,
165
						"pseudo_expediteur" => $obj->pseudo,
166
						"url" => $obj->url
167
					];
168
169
					$this->values[] = $arr;
170
				}
171
			}
172
		}
173
174
		/*
175
		 * fonction qui permetlors de l'envoit d'un message d'être sur que le membre existe
176
		 */
177
		private function getIdIdentiteExist($pseudo) {
178
			$dbc = App::getDb();
179
180
			$pseudo = trim($pseudo);
181
182
			$query = $dbc->select("ID_identite")->from("identite")->where("pseudo", "=", $pseudo)->get();
183
184
			if ((count($query) == 1) && (is_array($query))) {
185
				foreach ($query as $obj) {
186
					return $obj->ID_identite;
187
				}
188
			}
189
190
			return false;
191
		}
192
193
		/**
194
		 * @param $url_message
195
		 * fonction qui récupère un message suivant une url
196
		 */
197 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...
198
			$dbc = App::getDb();
199
200
			$query = $dbc->select()
201
				->from("_messagerie_message")
202
				->from("_messagerie_boite_reception")
203
				->from("identite")
204
				->where("_messagerie_message.url", "=", $url_message, "AND")
205
				->where("_messagerie_message.ID_expediteur", "=", "identite.ID_identite", "AND", true)
206
				->where("_messagerie_boite_reception.ID_message", "=", "_messagerie_message.ID_message", "", true)
207
				->get();
208
209
			if ((is_array($query)) && (count($query) > 0)) {
210
				foreach ($query as $obj) {
211
					$this->values = [
212
						"id_message" => $obj->ID_message,
213
						"objet" => $obj->objet,
214
						"date_message" => $obj->date,
215
						"id_expediteur" => $obj->ID_expediteur,
216
						"pseudo_expediteur" => $obj->pseudo,
217
						"url" => $obj->url,
218
						"supprimer" => $obj->supprimer
219
					];
220
				}
221
			}
222
			else {
223
				return false;
224
			}
225
		}
226
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
227
		
228
		
229
		
230
		//-------------------------- SETTER ----------------------------------------------------------------------------//
231
		/**
232
		 * @param $id_message
233
		 * pour passer le message en supprimé, il sera alors consultable dans la table messages supprimés
234
		 */
235 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...
236
			$dbc = App::getDb();
237
238
			$dbc->update("supprimer", 1)->from("_messagerie_boite_reception")
239
				->where("ID_message", "=", $id_message, "AND")
240
				->where("ID_identite", "=", 1)
241
				->set();
242
		}
243
244
		/**
245
		 * @param $id_message
246
		 */
247 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...
248
			$dbc = App::getDb();
249
250
			$dbc->delete()->from("_messagerie_boite_reception")
251
				->where("ID_message", "=", $id_message, "AND")
252
				->where("ID_identite", "=", $_SESSION['idlongin'])
253
				->del();
254
		}
255
256
		/**
257
		 * @param $objet
258
		 * @param $destinataire
259
		 * @param $message
260
		 * @return bool
261
		 *
262
		 * fonction qui sert à envoyer un message à un ou plusieurs destinataires
263
		 */
264
		public function setEnvoyerMessage($objet, $destinataire, $message) {
0 ignored issues
show
Coding Style introduced by
setEnvoyerMessage 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...
265
			$dbc = App::getDb();
266
267
			//on test si un ou plusieurs destinataires ++ si ils existent
268
			if (ChaineCaractere::FindInString($destinataire, ",")) {
269
				$destinataires = explode(",", $destinataire);
270
				$c = count($destinataires);
271
272
				for ($i=0 ; $i<$c ; $i++) {
273
					if ($this->getIdIdentiteExist($destinataires[$i]) !== false) {
274
						$destinataires[] = $this->getIdIdentiteExist($destinataires[$i]);
275
						$expediteur = $_SESSION['idlongin'.CLEF_SITE];
276
					}
277
					else {
278
						return false;
279
					}
280
				}
281
			}
282
			else {
283
				if ($this->getIdIdentiteExist($destinataire) !== false) {
284
					$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...
285
					$expediteur = $_SESSION['idlongin'.CLEF_SITE];
286
				}
287
				else if (is_int($destinataire)) {
288
					$destinataires[] = $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...
289
					$expediteur = 1;
290
				}
291
				else {
292
					return false;
293
				}
294
			}
295
296
			//cela veut dire qu'on a au moin 1 membre à qui envoyer le message
297
			if (count($destinataires) > 0) {
298
				$dbc->insert("message", $message)
299
					->insert("objet", $objet)
300
					->insert("url", ChaineCaractere::setUrl($objet))
301
					->insert("date", date("Y-m-d H:i:s"))
302
					->insert("ID_expediteur", $expediteur)
0 ignored issues
show
Bug introduced by
The variable $expediteur does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
303
					->into("_messagerie_message")
304
					->set();
305
306
				$id_message = $dbc->lastInsertId();
307
308
				foreach ($destinataires as $destinataire) {
309
					$dbc->insert("ID_identite", $destinataire)
310
						->insert("ID_message", $id_message)
311
						->into("_messagerie_boite_reception")
312
						->set();
313
				}
314
315
				return true;
316
			}
317
318
			return false;
319
		}
320
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
321
	}