Passed
Push — master ( 4db736...130235 )
by Anthony
03:00
created

DroitAcces   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 11
Bugs 1 Features 0
Metric Value
wmc 40
c 11
b 1
f 0
lcom 1
cbo 2
dl 0
loc 210
rs 8.2608

12 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 21 5
A getLogged() 0 3 1
A getSuperAdmin() 0 3 1
A getIdListeDroitAcces() 0 3 1
A getModifSeo() 0 3 1
A getModifContenu() 0 3 1
A getModifNavigation() 0 3 1
A getSupprimerPage() 0 3 1
A getListeDroitAcces() 0 17 4
D getDroitAccesPage() 0 27 10
C getDroitAccesContenu() 0 49 10
A getDroitAccesAction() 0 15 4

How to fix   Complexity   

Complex Class

Complex classes like DroitAcces often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use DroitAcces, and based on these observations, apply Extract Interface, too.

1
<?php
2
	namespace core\admin\droitsacces;
3
4
	use core\App;
5
6
	class DroitAcces {
7
		private $logged;
8
9
		//pour la table identite
10
		private $id_identite;
11
		private $super_admin;
12
13
		//pour la table liste_droit_acces
14
		private $id_liste_droit_acces;
15
16
		//pour des droits pour la gestion des contenus
17
		private $modif_seo;
18
		private $modif_contenu;
19
		private $modif_navigation;
20
		private $supprimer_page;
21
22
23
		//-------------------------- CONSTRUCTEUR ----------------------------------------------------------------------------//
24
		public function __construct() {
25
			$dbc = \core\App::getDb();
26
27
			if (isset($_SESSION["idlogin".CLEF_SITE])) {
28
				$this->logged = true;
29
				$this->id_identite = $_SESSION["idlogin".CLEF_SITE];
30
31
				//on test voir si super admin
32
				$query = $dbc->query("SELECT super_admin,liste_droit FROM identite WHERE ID_identite=".$this->id_identite);
33
34
				if ((is_array($query)) && (count($query) > 0)) {
35
					foreach ($query as $obj) {
36
						$this->super_admin = $obj->super_admin;
37
						$this->id_liste_droit_acces = $obj->liste_droit;
38
					}
39
				}
40
			}
41
			else {
42
				$this->logged = false;
43
			}
44
		}
45
		//-------------------------- FIN CONSTRUCTEUR ----------------------------------------------------------------------------//
46
    
47
    
48
    
49
		//-------------------------- GETTER ----------------------------------------------------------------------------//
50
		public function getLogged(){
51
			return $this->logged;
52
		}
53
		public function getSuperAdmin(){
54
			return $this->super_admin;
55
		}
56
		public function getIdListeDroitAcces(){
57
			return $this->id_liste_droit_acces;
58
		}
59
		public function getModifSeo(){
60
			return $this->modif_seo;
61
		}
62
		public function getModifContenu(){
63
			return $this->modif_contenu;
64
		}
65
		public function getModifNavigation(){
66
			return $this->modif_navigation;
67
		}
68
		public function getSupprimerPage(){
69
			return $this->supprimer_page;
70
		}
71
72
		/**
73
		 * @return array
74
		 */
75
		private function getListeDroitAcces() {
76
			$dbc = App::getDb();
77
78
			$liste_droit_acces = [];
79
80
			$query = $dbc->query("SELECT * FROM droit_acces, liste_droit_acces, liaison_liste_droit WHERE
81
								droit_acces.ID_droit_acces = liaison_liste_droit.ID_droit_acces AND
82
								liste_droit_acces.ID_liste_droit_acces = liaison_liste_droit.ID_liste_droit_acces AND
83
								liste_droit_acces.ID_liste_droit_acces = $this->id_liste_droit_acces
84
				");
85
86
			if ((is_array($query)) && (count($query) > 0)) {
87
				foreach ($query as $obj) $liste_droit_acces[] = $obj->droit_acces;
88
			}
89
90
			return $liste_droit_acces;
91
		}
92
93
		//autres getter
94
		/**
95
		 * pour savoir si en fonction des droits d'accès de l'utilisateur il peu ou non accéder à cete page
96
		 * on passe outre les test si on est super admin
97
		 * @param string $page
98
		 * @return bool
99
		 */
100
		public function getDroitAccesPage($page) {
101
			$dbc = \core\App::getDb();
102
103
			if ($this->super_admin != 1) {
104
				//page sans droit dans admin
105
				$all_access = array("gestion-comptes/mon-compte", "index");
106
				$droit_acces = [];
107
108
				$query = $dbc->query("SELECT droit_acces FROM droit_acces WHERE page LIKE '%$page%'");
109
				if ((is_array($query)) && (count($query) > 0)) {
110
					foreach ($query as $obj) $droit_acces = $obj->droit_acces;
111
				}
112
113
				//récupération de la liste des droits du userr
114
				$liste_droit_acces = $this->getListeDroitAcces();
115
116
				if (($this->super_admin == 1) || (in_array($droit_acces, $liste_droit_acces)) || (($page == "") || ($page == null)) || (in_array($page, $all_access))) {
117
					return true;
118
				}
119
				else {
120
					return false;
121
				}
122
			}
123
			else {
124
				return true;
125
			}
126
		}
127
128
		/**
129
		 * fonction qui permet de gérer les droits d'accès sur les contenus :
130
		 * - creer une page
131
		 * - modifier du contenu (SEO, navigation, contenu)
132
		 * - supprimer une page
133
		 * si on est super admin on passe outre tous les tests
134
		 * @param $droit
135
		 * @param $id_page
136
		 * @return bool|null
137
		 */
138
		public function getDroitAccesContenu($droit, $id_page) {
139
			$dbc = \core\App::getDb();
140
141
			//récupération de la liste des droits de l'utilisateur si aps super admin
142
			if ($this->super_admin != 1) {
143
				$liste_droit_acces = $this->getListeDroitAcces();
144
145
				if (in_array($droit, $liste_droit_acces)) {
146
					//on check si il a le droit de modifier ou supprimer cette page
147
					$query = $dbc->query("SELECT * FROM droit_acces_page, liste_droit_acces WHERE
148
									droit_acces_page.ID_liste_droit_acces = liste_droit_acces.ID_liste_droit_acces AND
149
									droit_acces_page.ID_page = $id_page AND
150
									liste_droit_acces.ID_liste_droit_acces = $this->id_liste_droit_acces
151
					");
152
153
					//si on a un resultat
154
					if ((is_array($query)) && (count($query) > 0)) {
155
						foreach ($query as $obj) {
156
							$this->modif_seo = $obj->seo;
157
							$this->modif_contenu = $obj->contenu;
158
							$this->modif_navigation = $obj->navigation;
159
							$this->supprimer_page = $obj->supprimer;
160
						}
161
162
						//si les trois sont différent de 0 on renvoit true soinon false
163
						if (($this->modif_seo != 0) || ($this->modif_contenu != 0) || ($this->modif_navigation != 0) || ($this->supprimer_page != 0)) {
164
							return true;
165
						}
166
						else {
167
							return false;
168
						}
169
					}
170
					else {
171
						return false;
172
					}
173
				}
174
				else {
175
					return false;
176
				}
177
			}
178
			else {
179
				$this->modif_seo = 1;
180
				$this->modif_contenu = 1;
181
				$this->modif_navigation = 1;
182
				$this->supprimer_page = 1;
183
184
				return true;
185
			}
186
		}
187
188
		/**
189
		 * pour savoir si un utilisateur à le droit de supprimer, modifier ou ajouter des trucs
190
		 * @param $droit_acces
191
		 * @return bool
192
		 */
193
		public function getDroitAccesAction($droit_acces) {
194
			if ($this->super_admin != 1) {
195
				$liste_droit_acces = $this->getListeDroitAcces();
196
197
				if (($this->super_admin == 1) || (in_array($droit_acces, $liste_droit_acces))) {
198
					return true;
199
				}
200
				else {
201
					return false;
202
				}
203
			}
204
			else {
205
				return true;
206
			}
207
		}
208
		//-------------------------- FIN GETTER ----------------------------------------------------------------------------//
209
    
210
    
211
    
212
		//-------------------------- SETTER ----------------------------------------------------------------------------//
213
214
		//-------------------------- FIN SETTER ----------------------------------------------------------------------------//
215
	}