Passed
Push — main ( 25e437...1b4ef4 )
by Jean-Christophe
02:19
created

AclListOperationsTrait::savePart()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

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
ccs 3
cts 3
cp 1
crap 2
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 10
	public function saveAclElement(AclElement $aclElement) {
24 10
		foreach ($this->providers as $provider) {
25 2
			$provider->saveAcl($aclElement);
26
		}
27 10
	}
28
29
	public function removeAclElement(AclElement $aclElement) {
30
		foreach ($this->providers as $provider) {
31
			$provider->removeAcl($aclElement);
32
		}
33
	}
34
35 13
	public function savePart(AbstractAclPart $aclPart) {
36 13
		foreach ($this->providers as $provider) {
37 5
			$provider->savePart($aclPart);
38
		}
39 13
	}
40
41 2
	public function updatePart(AbstractAclPart $aclPart) {
42 2
		foreach ($this->providers as $provider) {
43 2
			$provider->updatePart($aclPart);
44
		}
45 2
	}
46
47 5
	public function removePart(AbstractAclPart $aclPart) {
48 5
		foreach ($this->providers as $provider) {
49 5
			$provider->removePart($aclPart);
50
		}
51 5
	}
52
53 2
	public function removeRole(string $roleName) {
54 2
		$role = $this->getRoleByName($roleName);
55 2
		unset($this->roles[$roleName]);
56 2
		$this->unsetCache("role_$roleName");
57 2
		$this->removeAcl($roleName);
58 2
		$this->removePart($role);
59 2
	}
60
61 5
	protected function unsetCache($name) {
62 5
		if (isset($this->elementsCache[$name])) {
63 5
			unset($this->elementsCache[$name]);
64
		}
65 5
	}
66
67 2
	public function removePermission(string $permissionName) {
68 2
		$permission = $this->getPermissionByName($permissionName);
69 2
		unset($this->permissions[$permissionName]);
70 2
		$this->unsetCache("perm_$permissionName");
71 2
		$this->removeAcl(null, null, $permissionName);
72 2
		$this->removePart($permission);
73 2
	}
74
75 1
	public function removeResource(string $resourceName) {
76 1
		$resource = $this->getResourceByName($resourceName);
77 1
		unset($this->resources[$resourceName]);
78 1
		$this->unsetCache("res_$resourceName");
79 1
		$this->removeAcl(null, $resourceName);
80 1
		$this->removePart($resource);
81 1
	}
82
83 7
	public function removeAcl(string $roleName = null, string $resourceName = null, string $permissionName = null) {
84 7
		$toRemove = [];
85 7
		foreach ($this->acls as $index => $acl) {
86 7
			if (($resourceName == null || $acl->getResource()->getName() === $resourceName) && ($roleName == null || $acl->getRole()->getName() === $roleName) && ($permissionName == null || $acl->getPermission()->getName() === $permissionName)) {
87 2
				foreach ($this->providers as $provider) {
88 2
					$provider->removeAcl($acl);
89
				}
90 2
				$toRemove[] = $index;
91
			}
92
		}
93 7
		foreach ($toRemove as $remove) {
94 2
			unset($this->acls[$remove]);
95
		}
96 7
	}
97
98 3
	public function saveAll() {
99 3
		foreach ($this->providers as $provider) {
100 3
			if (! $provider->isAutosave()) {
101 2
				$provider->saveAll();
102
			}
103
		}
104 3
	}
105
}
106
107