Faction::getIdFaction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
	namespace modules\bataille\app\controller;
3
	
4
	
5
	use core\App;
6
	use core\HTML\flashmessage\FlashMessage;
7
	use Intervention\Image\Point;
8
	use modules\messagerie\app\controller\Messagerie;
9
	
10
	class Faction extends PermissionsFaction {
11
		protected $id_faction;
12
		protected $id_autre_faction;
13
		protected $nom_faction;
14
		protected $points_faction;
15
		
16
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
17
		public function __construct() {
18
			
19
		}
20
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
21
		
22
		
23
		//-------------------------- GETTER ----------------------------------------------------------------------------//
24
		public function getIdFaction(){
25
		    return $this->id_faction;
26
		}
27
		
28
		/**
29
		 * @param $id_faction
30
		 * @return bool
31
		 * permet de tester si le joueur est dans la faction affichée
32
		 */
33
		private function getTestFactionPlayer($id_faction) {
34
			$dbc = App::getDb();
35
			$id_ma_faction = 0;
36
			
37
			$query = $dbc->select("ID_faction")->from("_bataille_infos_player")->where("ID_identite", "=", Bataille::getIdIdentite())->get();
38
			
39
			foreach ($query as $obj) {
40
				$id_ma_faction = $obj->ID_faction;
41
			}
42
			
43
			if ($id_ma_faction == $id_faction) {
44
				Bataille::setValues([
45
					"ma_faction" => true,
46
					"id_identite_player" => Bataille::getIdIdentite()
47
				]);
48
				return true;
49
			}
50
			
51
			return false;
52
		}
53
		
54
		/**
55
		 * @return mixed
56
		 * fonction qui renvoi l'ID de la faction du joueur
57
		 */
58
		public function getFactionPlayer($id_identite = null) {
59
			$dbc = App::getDb();
60
			
61
			if ($id_identite === null) {
62
				$id_identite = Bataille::getIdIdentite();
63
			}
64
			
65
			$query = $dbc->select("ID_faction")->from("_bataille_infos_player")
66
				->where("ID_identite", "=", $id_identite, "AND")
67
				->where("ID_faction", ">", 0)
68
				->get();
69
			
70
			if (count($query) > 0) {
71
				foreach ($query as $obj) {
72
					$this->id_faction = $obj->ID_faction;
73
					$this->getInfosFaction();
74
				}
75
				
76
				return true;
77
			}
78
			
79
			$this->id_faction = "";
80
			return false;
81
		}
82
		
83
		/**
84
		 * @param null $id_faction
85
		 * fonction qui récupère les infos de la faction
86
		 */
87
		public function getInfosFaction($id_faction = null) {
88
			$dbc = App::getDb();
89
			
90
			if ($id_faction === null) {
91
				$id_faction = $this->id_faction;
92
			}
93
			
94
			$this->getTestFactionPlayer($id_faction);
95
			
96
			$query = $dbc->select("identite.pseudo")
97
				->select("_bataille_faction.ID_faction")
98
				->select("_bataille_faction.nom_faction")
99
				->select("_bataille_faction.points_faction")
100
				->select("_bataille_faction.img_profil")
101
				->select("_bataille_faction.description")
102
				->from("_bataille_faction")
103
				->from("identite")
104
				->where("_bataille_faction.ID_faction", "=", $id_faction, "AND")
105
				->where("_bataille_faction.ID_identite", "=", "identite.ID_identite", "", true)
106
				->get();
107
			
108
			if ((count($query) == 1)) {
109
				foreach ($query as $obj) {
110
					Bataille::setValues(["faction" => [
111
						"id_faction" => $obj->ID_faction,
112
						"nom" => $obj->nom_faction,
113
						"points_faction" => $obj->points_faction,
114
						"description" => $obj->description,
115
						"url_img" => $obj->img_profil,
116
						"pseudo_chef" => $obj->pseudo
117
					]]);
118
					
119
					$this->nom_faction = $obj->nom_faction;
120
				}
121
			}
122
		}
123
		
124
		/**
125
		 * @return array
126
		 * fonction qui récupère les membres d'un faction
127
		 */
128
		public function getMembreFaction() {
129
			$dbc = App::getDb();
130
			
131
			$query = $dbc->select()
132
				->from("_bataille_infos_player")
133
				->from("identite")
134
				->where("_bataille_infos_player.ID_faction", "=", $this->id_faction, "AND")
135
				->where("_bataille_infos_player.ID_identite", "=", "identite.ID_identite", "", true)
136
				->orderBy("_bataille_infos_player.points", "DESC")
137
				->get();
138
			
139
			$membre = [];
140
			$liste_membre = [];
141
			foreach ($query as $obj) {
142
				$membre[] = [
143
					"id_identite" => $obj->ID_identite,
144
					"pseudo" => $obj->pseudo,
145
					"points" => $obj->points,
146
					"rang_faction" => $obj->rang_faction,
147
					"chef" => $this->getTestChefFaction($obj->ID_identite, $this->id_faction),
148
					"permissions" => $this->getMembrePermissions($obj->ID_identite, $this->id_faction)
149
				];
150
				
151
				$liste_membre[] = $obj->pseudo;
152
			}
153
			
154
			Bataille::setValues(["membres_faction" => $membre]);
155
			
156
			return $liste_membre;
157
		}
158
		
159
		/**
160
		 * @param $nom_faction
161
		 * @return bool
162
		 * ajout d'une fonction pour tester si une faction existe ou non renvoi true si elle existe
163
		 */
164
		protected function getFactionExist($nom_faction) {
165
			$dbc = App::getDb();
166
			
167
			$query = $dbc->select("ID_faction")->from("_bataille_faction")->where("nom_faction", "=", $nom_faction)->get();
168
			
169
			if (count($query) > 0) {
170
				foreach ($query as $obj) {
171
					$this->id_autre_faction = $obj->ID_faction;
172
				}
173
				
174
				return true;
175
			}
176
			
177
			return false;
178
		}
179
		
180
		/**
181
		 * @return int
182
		 * fonciton qui renvoi le nombre d'invitations possible
183
		 * sachant que dès le niveau 3 on peut en envoyer 5 et ensuite une seule par niveau jusqu'au niveau 30
184
		 */
185
		private function getNbInvitationPossible() {
186
			$dbc = App::getDb();
187
			$nb_inv = 5;
188
			
189
			$nb_inv = (Bataille::getBatiment()->getNiveauBatiment("ambassade")+$nb_inv)-3;
190
			
191
			$query = $dbc->select("ID_identite")->from("_bataille_faction_invitation")->where("ID_faction", "=", $this->id_faction)->get();
192
			$nb_invitation_envoyees = count($query);
193
			
194
			$nb_inv = $nb_inv-$nb_invitation_envoyees-count($this->getMembreFaction());
195
			
196
			if ($nb_inv <= 0) {
197
				return 0;
198
			}
199
			
200
			return $nb_inv;
201
		}
202
		
203
		/**
204
		 * @return array
205
		 * foncitons qui renvoit les informations sur les joueurs invités à rejoindre la faction
206
		 */
207
		public function getInvitationsEnvoyees() {
208
			$dbc = App::getDb();
209
			$permissions_membre = $this->getPermissionsMembre($this->id_faction);
210
			$invitations = [];
211
			$pseudos = [];
212
			
213
			if ($permissions_membre == "chef" || in_array("INVITER_MEMBRE", $permissions_membre)) {
214
				$query = $dbc->select()->from("_bataille_faction_invitation, identite, _bataille_infos_player")
215
					->where("_bataille_faction_invitation.ID_faction", "=", $this->id_faction, "AND")
216
					->where("_bataille_faction_invitation.ID_identite", "=", "identite.ID_identite", "AND", true)
217
					->where("_bataille_faction_invitation.ID_identite", "=", "_bataille_infos_player.ID_identite", "", true)->get();
218
			}
219
			
220
			if (count($query) > 0) {
221
				foreach ($query as $obj) {
0 ignored issues
show
Bug introduced by
The variable $query 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...
222
					$invitations[] = [
223
						"id_identite" => $obj->ID_identite,
224
						"points" => $obj->points,
225
						"pseudo" => $obj->pseudo,
226
						"vacances" => $obj->mode_vacances,
227
					];
228
					
229
					$pseudos[] = $obj->pseudo;
230
				}
231
			}
232
			
233
			Bataille::setValues(["invitations" => $invitations, "nb_invitation_possible" => $this->getNbInvitationPossible()]);
234
			
235
			return $pseudos;
236
		}
237
		
238
		/**
239
		 * fonction qui renvoi les invitations recues par un joueur
240
		 */
241
		public function getInvitationsMembre() {
242
			$dbc = App::getDb();
243
			
244
			$query = $dbc->select()->from("_bataille_faction_invitation, _bataille_faction")
245
				->where("_bataille_faction_invitation.ID_identite", "=", Bataille::getIdIdentite(), "AND")
246
				->where("_bataille_faction_invitation.ID_faction", "=", "_bataille_faction.ID_faction", "", true)->get();
247
			
248
			if (count($query) > 0) {
249
				foreach ($query as $obj) {
250
					$invitations[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$invitations was never initialized. Although not strictly required by PHP, it is generally a good practice to add $invitations = 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...
251
						"id_faction" => $obj->ID_faction,
252
						"nom_faction" => $obj->nom_faction,
253
						"points_faction" => $obj->points_faction,
254
					];
255
					
256
					Bataille::setValues(["invitations" => $invitations]);
257
				}
258
			}
259
		}
260
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
261
		
262
		
263
		//-------------------------- SETTER ----------------------------------------------------------------------------//
264
		/**
265
		 * @param $nom_faction
266
		 * @param $description
267
		 * @return bool
268
		 * fonction qui permet de créer une faction quand l'ambassade est au lvl 3
269
		 */
270
		public function setCreerFaction($nom_faction, $description) {
271
			$dbc = App::getDb();
272
			
273
			if (Bataille::getBatiment()->getNiveauBatiment("ambassade") >= 3) {
274
				if ($this->getFactionExist($nom_faction) == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
275
					FlashMessage::setFlash("Une faction");
276
					return false;
277
				}
278
				if (strlen($nom_faction) < 2) {
279
					FlashMessage::setFlash("Le nom de votre faction est trop court");
280
					return false;
281
				}
282
				
283
				$points_chef = Points::getPointsJoueur();
284
				
285
				$dbc->insert("nom_faction", $nom_faction)->insert("description", $description)
286
					->insert("ID_identite", Bataille::getIdIdentite())->insert("points_faction", $points_chef)
287
					->into("_bataille_faction")->set();
288
				
289
				$id = $dbc->lastInsertId();
290
				
291
				$dbc->update("ID_faction", $id)->from("_bataille_infos_player")->where("ID_identite", "=", Bataille::getIdIdentite())->set();
292
				
293
				FlashMessage::setFlash("Votre faction a bien été créée", "success");
294
				return true;
295
			}
296
			
297
			FlashMessage::setFlash("Votre ambassade n'est pas au niveau 3");
298
			return false;
299
		}
300
		
301
		/**
302
		 * @param $id_identite
303
		 * @return bool
304
		 * fonction qui permet de renvoyer un membre d'un faction
305
		 */
306
		public function setRenvoyerMembre($id_identite) {
307
			$dbc = App::getDb();
308
			$permissions_membre = $this->getPermissionsMembre($this->id_faction);
309
			
310
			if ($permissions_membre == "chef" || in_array("RENVOYER_MEMBRE", $permissions_membre)) {
311
				$dbc->update("ID_faction", 0)
312
					->update("rang_faction", "")
313
					->from("_bataille_infos_player")
314
					->where("ID_identite", "=", $id_identite, "AND")
315
					->where("ID_faction", "=", $this->id_faction, "", true)
316
					->set();
317
				
318
				$this->setSupprilerAllPermissions($id_identite);
319
				
320
				FlashMessage::setFlash("Le membre a bien été renvoyé de la faction", "success");
321
				return true;
322
			}
323
			
324
			FlashMessage::setFlash("Vous n'avez pas l'autorisation de renvoyer un membre");
325
			return false;
326
		}
327
		
328
		/**
329
		 * @param $pseudo
330
		 * @return bool
331
		 * fonction qui permet d'inviter un membre à rejoindre la faction
332
		 */
333
		public function setInviterMembre($pseudo) {
334
			$dbc = App::getDb();
335
			$permissions_membre = $this->getPermissionsMembre($this->id_faction);
336
			
337
			if ($permissions_membre == "chef" || in_array("INVITER_MEMBRE", $permissions_membre)) {
338
				$id_identite = Bataille::getPlayerExist($pseudo);
339
				if ($id_identite== false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
340
					FlashMessage::setFlash("Ce joueur n'existe pas");
341
					return false;
342
				}
343
				if (in_array($pseudo, $this->getMembreFaction())) {
344
					FlashMessage::setFlash("Ce joueur est déjà dans votre faction ou est en attente d'invitation, vous ne pouvez pas l'inviter à nouveau");
345
					return false;
346
				}
347
				if (in_array($pseudo, $this->getInvitationsEnvoyees())) {
348
					FlashMessage::setFlash("Ce joueur est déjà dans votre faction ou est en attente d'invitation, vous ne pouvez pas l'inviter à nouveau");
349
					return false;
350
				}
351
				if ($this->getNbInvitationPossible() < 1) {
352
					FlashMessage::setFlash("Plus d'invitations possible, votre leader doit augmenter son ambassade");
353
					return false;
354
				}
355
				
356
				$infos = [
357
					"nom_faction" => $this->nom_faction,
358
					"id_faction" => $this->id_faction
359
				];
360
				
361
				require(MODULEROOT."bataille/app/controller/rapports/invitation-faction.php");
362
				
363
				$messagerie = new Messagerie();
364
				$messagerie->setEnvoyerMessage("Invitation rejoindre faction", $id_identite, $message);
0 ignored issues
show
Bug introduced by
The variable $message does not exist. Did you mean $messagerie?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
365
				
366
				$dbc->insert("ID_faction", $this->id_faction)->insert("ID_identite", $id_identite)
367
					->into("_bataille_faction_invitation")->set();
368
				FlashMessage::setFlash("L'invitation a bien été envoyée", "success");
369
				return true;
370
			}
371
			
372
			FlashMessage::setFlash("Vous n'avez pas l'autorisation d'inviter un membre");
373
			return false;
374
		}
375
		
376
		/**
377
		 * @param $id_identite
378
		 */
379
		public function setChangerChef($id_identite) {
380
			$dbc = App::getDb();
381
			$permissions_membre = $this->getPermissionsMembre($this->id_faction);
382
			
383
			if ($permissions_membre == "chef") {
384
				$dbc->update("ID_identite", $id_identite)->from("_bataille_faction")
385
					->where("ID_faction", "=", $this->id_faction)->set();
386
				
387
				FlashMessage::setFlash("Le chef de la faction a bien été changé", "success");
388
				
389
				return true;
390
			}
391
			
392
			FlashMessage::setFlash("Vous n'êtes pas le chef de la faction vous ne pouvez donc pas le changer");
393
			return false;
394
		}
395
		
396
		/**
397
		 * @param $id_identite
398
		 * @return bool
399
		 * fonction qui permet de supprimer une invitation a rejoindre la faction
400
		 */
401 View Code Duplication
		public function setSupprimerInvitation($id_identite) {
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...
402
			$dbc = App::getDb();
403
			$permissions_membre = $this->getPermissionsMembre($this->id_faction);
404
			
405
			if ($permissions_membre == "chef" || in_array("INVITER_MEMBRE", $permissions_membre)) {
406
				$dbc->delete()->from("_bataille_faction_invitation")->where("ID_faction", "=", $this->id_faction, "AND")
407
					->where("ID_identite", "=", $id_identite)->del();
408
				
409
				FlashMessage::setFlash("L'invitation a bien été supprimée", "success");
410
				return true;
411
			}
412
			
413
			FlashMessage::setFlash("Vous n'avez pas l'autorisation de supprimer une invitation");
414
			return false;
415
		}
416
		
417
		/**
418
		 * @return bool
419
		 * fonction qui permet à un joueur de quitter sa faction
420
		 */
421
		public function setQuitterFaction() {
422
			$dbc = App::getDb();
423
			$permissions_membre = $this->getPermissionsMembre($this->id_faction);
424
			
425
			if ($permissions_membre == "chef") {
426
				FlashMessage::setFlash("Merci de définir un nouveau chef avant de quitter votre faction");
427
				return false;
428
			}
429
			
430
			Points::setRejoindreQuitterFaction("del");
431
			
432
			$dbc->update("ID_faction", 0)
433
				->update("rang_faction", "")->from("_bataille_infos_player")->where("ID_identite", "=", Bataille::getIdIdentite())->set();
434
			
435
			$this->setSupprilerAllPermissions(Bataille::getIdIdentite());
436
			
437
			FlashMessage::setFlash("Vous avez quitter votre faction", "success");
438
			return true;
439
		}
440
		
441
		/**
442
		 * @param $id_faction
443
		 * @return bool
444
		 * fonction qui permet à un joueur de rejoindre une faction
445
		 */
446 View Code Duplication
		public function setAccepterInvitationPlayer($id_faction) {
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...
447
			$dbc = App::getDb();
448
			
449
			$dbc->update("ID_faction", $id_faction)
450
				->update("rang_faction", "")->from("_bataille_infos_player")->where("ID_identite", "=", Bataille::getIdIdentite())->set();
451
			
452
			$this->setSupprimerInvitationPlayer($id_faction);
453
			
454
			Points::setRejoindreQuitterFaction();
455
			
456
			FlashMessage::setFlash("Vous avez rejoint une faction", "success");
457
			return true;
458
		}
459
		
460
		/**
461
		 * @param $id_faction
462
		 * @return bool
463
		 * permet à un joueur de supprimer une invitation qu'il a reçu
464
		 */
465 View Code Duplication
		public function setSupprimerInvitationPlayer($id_faction) {
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...
466
			$dbc = App::getDb();
467
			
468
			$dbc->delete()->from("_bataille_faction_invitation")->where("ID_faction", "=", $id_faction, "AND")
469
				->where("ID_identite", "=", Bataille::getIdIdentite())->del();
470
			
471
			FlashMessage::setFlash("L'invitation a bien été supprimée", "success");
472
			return true;
473
		}
474
		
475
		/**
476
		 * @param $id_identite
477
		 * @param $rang
478
		 * @return bool
479
		 * permet de definir le rang d'un joueur au sein de la faction (juste le nom pas les permissions
480
		 */
481 View Code Duplication
		public function setRang($id_identite, $rang) {
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...
482
			$dbc = App::getDb();
483
			$permissions_membre = $this->getPermissionsMembre($this->id_faction);
484
			
485
			if ($permissions_membre == "chef" || in_array("GERER_RANG_MEMBRE", $permissions_membre)) {
486
				$dbc->update("rang_faction", $rang)->from("_bataille_infos_player")->where("ID_identite", "=", $id_identite, "AND")
487
					->where("ID_faction", "=", $this->id_faction)->set();
488
				
489
				FlashMessage::setFlash("Le rang du joueur a bien été mis à jour", "success");
490
				return true;
491
			}
492
			
493
			FlashMessage::setFlash("Vous n'avez pas l'autorisation de définir le rang des joueurs");
494
			return false;
495
		}
496
		//-------------------------- END SETTER ----------------------------------------------------------------------------//    
497
	}