Passed
Push — master ( 49cb71...80c1f1 )
by Anthony
03:05
created

GestionDroitAcces::getAllDroitAcces()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 3
eloc 10
nc 3
nop 0
1
<?php
2
	namespace core\admin\droitsacces;
3
4
	use core\App;
5
	use core\HTML\flashmessage\FlashMessage;
6
	
7
	class GestionDroitAcces extends DroitAcces {
8
		use GetDetailListeAcces;
9
		
10
		
11
12
		//-------------------------- CONSTRUCTEUR ----------------------------------------------------------------------------//
13
		public function __construct() {
14
				$this->getListeDroitAccesAdmin();
15
		}
16
		//-------------------------- FIN CONSTRUCTEUR ----------------------------------------------------------------------------//
17
18
19
20
		//-------------------------- GETTER ----------------------------------------------------------------------------//
21
		/**
22
		 * appellee dans le constructeur pour afficher les listes de droit d'acces
23
		 */
24
		private function getListeDroitAccesAdmin() {
25
			$dbc = App::getDb();
26
			
27
			$query = $dbc->select()->from("liste_droit_acces")->get();
28
29
			if ((is_array($query)) && (count($query) > 0)) {
30
				$values = [];
31
				foreach ($query as $obj) {
32
					$values[] = [
33
						"id_liste" => $obj->ID_liste_droit_acces,
34
						"nom_liste" => $obj->nom_liste,
35
						"nb_droit_acces" => $this->getNombreDroitAccesListe($obj->ID_liste_droit_acces),
36
						"nb_droit_acces_page" => $this->getNombrePageListe($obj->ID_liste_droit_acces),
37
						"nb_user" => $this->getNombreUtilisateurListe($obj->ID_liste_droit_acces),
38
					];
39
				}
40
				App::setValues(["liste_droit_acces" => $values]);
41
			}
42
		}
43
		
44
		/**
45
		 * @param $id_droit_acces
46
		 * @return string
47
		 * fonction qui renvoi le nom d'un droit d'accès en fonction de son id
48
		 */
49
		private function getNomDroitAcces($id_droit_acces) {
50
			$dbc = App::getDb();
51
			
52
			$query = $dbc->select("droit_acces")->from("droit_acces")->where("ID_droit_acces", "=", $id_droit_acces)->get();
53
		
54
			if (count($query) > 0) {
55
				foreach ($query as $obj) {
56
					return $obj->droit_acces;
57
				}
58
			}
59
			
60
			return "";
61
		}
62
		
63
		/**
64
		 * fonction qui récupère tous les droits d'accès
65
		 */
66
		private function getAllDroitAcces() {
67
			$dbc = App::getDb();
68
			
69
			$query = $dbc->select()->from("droit_acces")->get();
70
			
71
			if (count($query) > 0) {
72
				foreach ($query as $obj) {
73
					$values[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$values was never initialized. Although not strictly required by PHP, it is generally a good practice to add $values = 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...
74
						"id_droit_acces" => $obj->ID_droit_acces,
75
						"droit_acces" => $obj->droit_acces,
76
						"nom_module" => $obj->nom_module
77
					];
78
				}
79
				
80
				App::setValues(["droit_acces" => $values]);
0 ignored issues
show
Bug introduced by
The variable $values 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...
81
			}
82
		}
83
		
84
		/**
85
		 * @param $id_liste
86
		 * recupere tous les id des droits dc'acces d'une liste
87
		 */
88
		public function getDroiAccesListe($id_liste) {
89
			$dbc = App::getDb();
90
			
91
			$query = $dbc->select()->from("liste_droit_acces, liaison_liste_droit")
92
				->where("liste_droit_acces.ID_liste_droit_acces", "=", $id_liste, "AND")
93
				->where("liste_droit_acces.ID_liste_droit_acces", "=", "liaison_liste_droit.ID_liste_droit_acces", "", true)
94
				->get();
95
			
96
			if (count($query) > 0) {
97
				foreach ($query as $obj) {
98
					$values[] = $this->getNomDroitAcces($obj->ID_droit_acces);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$values was never initialized. Although not strictly required by PHP, it is generally a good practice to add $values = 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...
99
					$nom_liste = $obj->nom_liste;
100
					$id_liste = $obj->ID_liste_droit_acces;
101
				}
102
				
103
				App::setValues(["droit_acces_liste" => $values, "nom_liste" => $nom_liste, "id_liste" => $id_liste]);
0 ignored issues
show
Bug introduced by
The variable $values 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...
Bug introduced by
The variable $nom_liste 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...
104
				$this->getAllDroitAcces();
105
			}
106
		}
107
		//-------------------------- FIN GETTER ----------------------------------------------------------------------------//
108
109
110
111
		//-------------------------- SETTER ----------------------------------------------------------------------------//
112
		public function setGestionDroitAccesListe($id_droit_acces, $id_liste, $activer) {
113
			$dbc = App::getDb();
114
			
115
			if ($activer == 1) {
116
				$dbc->insert("ID_droit_acces", $id_droit_acces)->insert("ID_liste_droit_acces", $id_liste)
117
					->into("liaison_liste_droit")->set();
118
				FlashMessage::setFlash("Le droit d'acces a bien été ajouté", "success");
119
				return true;
120
			}
121
			
122
			$dbc->delete()->from("liaison_liste_droit")->where("ID_liste_droit_acces", "=", $id_liste, "AND")
123
				->where("ID_droit_acces", "=", $id_droit_acces)->del();
124
			FlashMessage::setFlash("Le droit d'acces a bien été supprimé", "success");
125
			return true;
126
		}
127
		//-------------------------- FIN SETTER ----------------------------------------------------------------------------//
128
	}