Test Failed
Push — main ( d55b52...b5ed50 )
by Jean-Christophe
03:55
created

AclList   B

Complexity

Total Complexity 48

Size/Duplication

Total Lines 247
Duplicated Lines 0 %

Test Coverage

Coverage 96.67%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 82
c 3
b 0
f 1
dl 0
loc 247
ccs 116
cts 120
cp 0.9667
rs 8.5599
wmc 48

24 Methods

Rating   Name   Duplication   Size   Complexity  
A setProviders() 0 2 1
A addProvider() 0 2 1
A init() 0 5 1
A loadAcls() 0 5 2
A getRoles() 0 2 1
A getResources() 0 2 1
A loadPermissions() 0 5 2
A elementExistByName() 0 7 3
A getProviders() 0 2 1
A getResourceByName() 0 2 1
A getAcls() 0 2 1
A getPermissionByName() 0 2 1
A loadRoles() 0 5 2
A getElementByName() 0 7 3
A getPermissions() 0 2 1
A getRoleByName() 0 2 1
A loadResources() 0 5 2
A existAclIn() 0 6 2
A getProviderClasses() 0 6 2
B getRolePermissionsOn() 0 17 7
A isAllowed() 0 12 4
A getAclById_() 0 7 3
A existPartIn() 0 6 2
A getProvider() 0 7 3

How to fix   Complexity   

Complex Class

Complex classes like AclList 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.

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 AclList, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Ubiquity\security\acl\models;
3
4
use Ubiquity\security\acl\persistence\AclProviderInterface;
5
use Ubiquity\exceptions\AclException;
6
use Ubiquity\security\acl\models\traits\AclListOperationsTrait;
7
use Ubiquity\security\acl\models\traits\AclListQueryTrait;
8
9
/**
10
 * Ubiquity\security\acl\models$AclList
11
 * This class is part of Ubiquity
12
 *
13
 * @author jc
14
 * @version 1.0.0
15
 *
16
 */
17
class AclList {
18
	use AclListOperationsTrait,AclListQueryTrait;
19
20
	/**
21
	 *
22
	 * @var AclElement[]
23
	 */
24
	protected $acls;
25
26
	/**
27
	 *
28
	 * @var Role[]
29
	 */
30
	protected $roles;
31
32
	/**
33
	 *
34
	 * @var \Ubiquity\security\acl\models\Resource[]
35
	 */
36
	protected $resources;
37
38
	/**
39
	 *
40
	 * @var Permission[]
41
	 */
42
	protected $permissions;
43
44
	/**
45
	 *
46
	 * @var AclProviderInterface[]
47
	 */
48
	protected $providers = [];
49
50
	protected $elementsCache = [];
51
52 21
	protected function getElementByName(string $name, array $inArray, string $type) {
53 21
		foreach ($inArray as $elm) {
54 21
			if ($elm->getName() == $name) {
55 20
				return $elm;
56
			}
57
		}
58 7
		throw new AclException("$name does not exist in $type ACL");
59
	}
60
61 4
	protected function elementExistByName(string $name, array $inArray): bool {
62 4
		foreach ($inArray as $elm) {
63 4
			if ($elm->getName() == $name) {
64 3
				return true;
65
			}
66
		}
67 4
		return false;
68
	}
69
70 22
	public function init() {
71 22
		$this->roles['role_@ALL'] = new Role('@ALL');
72 22
		$this->resources['res_*'] = new Resource('*');
73 22
		$this->permissions['perm_ALL'] = new Permission('ALL', 1000);
74 22
		$this->acls = [];
75 22
	}
76
77 21
	public function getRoleByName(string $name) {
78 21
		return $this->elementsCache["role_$name"] ??= $this->getElementByName($name, $this->roles, 'roles');
79
	}
80
81 13
	public function getResourceByName(string $name) {
82 13
		return $this->elementsCache["res_$name"] ??= $this->getElementByName($name, $this->resources, 'resources');
83
	}
84
85 17
	public function getPermissionByName(string $name) {
86 17
		return $this->elementsCache["perm_$name"] ??= $this->getElementByName($name, $this->permissions, 'permissions');
87
	}
88
89 10
	public function loadAcls(): array {
90 10
		foreach ($this->providers as $provider) {
91 10
			$this->acls += $provider->loadAllAcls();
92
		}
93 10
		return $this->acls;
94
	}
95
96 10
	public function loadRoles(): array {
97 10
		foreach ($this->providers as $provider) {
98 10
			$this->roles += $provider->loadAllRoles();
99
		}
100 10
		return $this->roles;
101
	}
102
103 10
	public function loadResources(): array {
104 10
		foreach ($this->providers as $provider) {
105 10
			$this->resources += $provider->loadAllResources();
106
		}
107 10
		return $this->resources;
108
	}
109
110 10
	public function loadPermissions(): array {
111 10
		foreach ($this->providers as $provider) {
112 10
			$this->permissions += $provider->loadAllPermissions();
113
		}
114 10
		return $this->permissions;
115
	}
116
117
	public function addProvider(AclProviderInterface $provider) {
118
		$this->providers[] = $provider;
119
	}
120
121
	/**
122
	 *
123
	 * @return AclElement[]
124
	 */
125 7
	public function getAcls() {
126 7
		return $this->acls;
127
	}
128
129
	/**
130
	 *
131
	 * @return Role[]
132
	 */
133 10
	public function getRoles() {
134 10
		return $this->roles;
135
	}
136
137
	/**
138
	 *
139
	 * @return \Ubiquity\security\acl\models\Resource[]
140
	 */
141 9
	public function getResources() {
142 9
		return $this->resources;
143
	}
144
145
	/**
146
	 *
147
	 * @return Permission[]
148
	 */
149 13
	public function getPermissions() {
150 13
		return $this->permissions;
151
	}
152
153
	/**
154
	 *
155
	 * @return AclProviderInterface[]
156
	 */
157 1
	public function getProviders() {
158 1
		return $this->providers;
159
	}
160
161
	/**
162
	 *
163
	 * @param AclProviderInterface[] $providers
164
	 */
165 11
	public function setProviders($providers) {
166 11
		$this->providers = $providers;
167 11
	}
168
169 7
	public function getRolePermissionsOn(string $roleName, $resourceName = '*'): array {
170 7
		$role = $this->getRoleByName($roleName);
171 7
		$parents = $role->getParentsArray();
172 7
		$result = [];
173 7
		foreach ($this->acls as $aclElement) {
174 7
			$aclRoleName = $aclElement->getRole()->getName();
175 7
			if ($aclRoleName === '@ALL' || $aclRoleName === $roleName) {
176
				$aclResourceName = $aclElement->getResource()->getName();
177 11
				if ($aclResourceName === '*' || $aclResourceName === $resourceName) {
178 11
					$result[] = $aclElement;
179 11
				}
180 11
			}
181
		}
182 10
		foreach ($parents as $parentElm) {
183 10
			$result += $this->getRolePermissionsOn($parentElm, $resourceName);
184 10
		}
185 10
		return $result;
186
	}
187 12
188 12
	public function isAllowed(string $roleName, string $resourceName, string $permissionName) {
189 12
		$acls = $this->getRolePermissionsOn($roleName, $resourceName);
190 12
		if (\count($acls) > 0) {
191
			$permissionLevel = $this->getPermissionByName($permissionName)->getLevel();
192 3
			foreach ($acls as $aclElm) {
193 3
				$level = $aclElm->getPermission()->getLevel();
194 2
				if ($level >= $permissionLevel) {
195 2
					return true;
196 2
				}
197
			}
198 13
		}
199 13
		return false;
200 13
	}
201 13
202 13
	/**
203 13
	 *
204
	 * @param string $providerClass
205 4
	 * @return \Ubiquity\security\acl\persistence\AclProviderInterface|NULL
206 4
	 */
207 4
	public function getProvider(string $providerClass) {
208
		foreach ($this->providers as $prov) {
209 4
			if ($prov instanceof $providerClass) {
210 4
				return $prov;
211
			}
212 4
		}
213 4
		return null;
214
	}
215 4
216 4
	/**
217
	 *
218 20
	 * @param AbstractAclPart $part
219 20
	 * @param string $providerClass
220 19
	 * @return boolean
221 19
	 */
222 19
	public function existPartIn(AbstractAclPart $part, string $providerClass) {
223 16
		$prov = $this->getProvider($providerClass);
224 16
		if (isset($prov)) {
225 16
			return $prov->existPart($part);
226 16
		}
227 16
		return false;
228
	}
229
230
	/**
231 19
	 *
232 4
	 * @param AclElement $elm
233
	 * @param string $providerClass
234 19
	 * @return boolean
235
	 */
236
	public function existAclIn(AclElement $elm, string $providerClass) {
237 20
		$prov = $this->getProvider($providerClass);
238 20
		if (isset($prov)) {
239 19
			return $prov->existAcl($elm);
240 16
		}
241 15
		return false;
242 15
	}
243 15
244 15
	/**
245
	 *
246
	 * @param string $id_
247
	 * @return ?AclElement
248 14
	 */
249
	public function getAclById_(string $id_): ?AclElement {
250
		foreach ($this->acls as $acl) {
251
			if ($acl->getId_() === $id_) {
252
				return $acl;
253
			}
254
		}
255
		return null;
256 2
	}
257 2
258 2
	public function getProviderClasses() {
259 2
		$result = [];
260
		foreach ($this->providers as $prov) {
261
			$result[] = \get_class($prov);
262
		}
263
		return $result;
264
	}
265
}
266
267