Test Failed
Push — main ( 4b33bb...ef3f31 )
by Jean-Christophe
02:12
created

AclListOperationsTrait::updatePart()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 2
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 3
rs 10
1
<?php
2
namespace Ubiquity\security\acl\models\traits;
3
4
use Ubiquity\security\acl\models\AclElement;
5
use Ubiquity\security\acl\models\AbstractAclPart;
6
7
/**
8
 * Ubiquity\security\acl\models\traits$AclListOperations
9
 * This class is part of Ubiquity
10
 *
11
 * @author jc
12
 * @version 1.0.0
13
 *
14
 */
15
trait AclListOperationsTrait {
16
17
	abstract public function getRoleByName(string $name);
18
19
	abstract public function getResourceByName(string $name);
20
21
	abstract public function getPermissionByName(string $name);
22
23
	public function saveAclElement(AclElement $aclElement) {
24
		foreach ($this->providers as $provider) {
25
			$provider->saveAcl($aclElement);
26
		}
27
	}
28
29
	public function removeAclElement(AclElement $aclElement) {
30
		foreach ($this->providers as $provider) {
31
			$provider->removeAcl($aclElement);
32
		}
33
	}
34
35
	public function savePart(AbstractAclPart $aclPart) {
36
		foreach ($this->providers as $provider) {
37
			$provider->savePart($aclPart);
38
		}
39
	}
40
41
	public function updatePart(AbstractAclPart $aclPart) {
42
		foreach ($this->providers as $provider) {
43
			$provider->updatePart($aclPart);
44
		}
45
	}
46
47
	public function removePart(AbstractAclPart $aclPart) {
48
		foreach ($this->providers as $provider) {
49
			$provider->removePart($aclPart);
50
		}
51
	}
52
53
	public function removeRole(string $roleName) {
54
		$role = $this->getRoleByName($roleName);
55
		unset($this->roles["role_$roleName"]);
56
		$this->unsetCache("role_$roleName");
57
		$this->removeAcl($roleName);
58
		$this->removePart($role);
59
	}
60
61
	protected function unsetCache($name) {
62
		if (isset($this->elementsCache[$name])) {
63
			unset($this->elementsCache[$name]);
64
		}
65
	}
66
67
	public function removePermission(string $permissionName) {
68
		$permission = $this->getPermissionByName($permissionName);
69
		unset($this->permissions["perm_$permissionName"]);
70
		$this->unsetCache("perm_$permissionName");
71
		$this->removeAcl(null, null, $permissionName);
72
		$this->removePart($permission);
73
	}
74
75
	public function removeResource(string $resourceName) {
76
		$resource = $this->getResourceByName($resourceName);
77
		unset($this->resources["res_$resourceName"]);
78
		$this->unsetCache("res_$resourceName");
79
		$this->removeAcl(null, $resourceName);
80
		$this->removePart($resource);
81
	}
82
83
	public function removeAcl(string $roleName = null, string $resourceName = null, string $permissionName = null) {
84
		$toRemove = [];
85
		foreach ($this->acls as $index => $acl) {
86
			if (($resourceName == null || $acl->getResource()->getName() === $resourceName) && ($roleName == null || $acl->getRole()->getName() === $roleName) && ($permissionName == null || $acl->getPermission()->getName() === $permissionName)) {
3 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $resourceName of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
Bug introduced by
It seems like you are loosely comparing $permissionName of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
Bug introduced by
It seems like you are loosely comparing $roleName of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
87
				foreach ($this->providers as $provider) {
88
					$provider->removeAcl($acl);
89
				}
90
				$toRemove[] = $index;
91
			}
92
		}
93
		foreach ($toRemove as $remove) {
94
			unset($this->acls[$remove]);
95
		}
96
	}
97
98
	public function saveAll() {
99
		foreach ($this->providers as $provider) {
100
			if (! $provider->isAutosave()) {
101
				$provider->saveAll();
102
			}
103
		}
104
	}
105
}
106
107