Passed
Push — main ( 624588...ca628f )
by Jean-Christophe
02:56
created

AclArrayProvider::removeAcl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
namespace Ubiquity\security\acl\persistence;
3
4
use Ubiquity\security\acl\models\AclElement;
5
use Ubiquity\security\acl\models\Permission;
6
use Ubiquity\security\acl\models\Resource;
7
use Ubiquity\security\acl\models\Role;
8
use Ubiquity\exceptions\AclException;
9
use Ubiquity\security\acl\models\AbstractAclPart;
10
11
/**
12
 * Ubiquity\security\acl\persistence$AclArrayProvider
13
 * This class is part of Ubiquity
14
 *
15
 * @author jc
16
 * @version 1.0.0
17
 *
18
 */
19
abstract class AclArrayProvider implements AclProviderInterface {
20
21
	protected $aclsArray;
22
23
	protected $parts;
24
25
	public function __construct() {}
26
27 6
	protected function loadAllPart($class): array {
28 6
		$elements = [];
29 6
		foreach ($this->parts[$class] as $partArray) {
30 6
			$elm = new $class();
31 6
			$elm->fromArray($partArray);
32 6
			$elements[$partArray['name']] = $elm;
33
		}
34 6
		return $elements;
35
	}
36
37
	/**
38
	 *
39
	 * {@inheritdoc}
40
	 * @see \Ubiquity\security\acl\persistence\AclProviderInterface::loadAllAcls()
41
	 */
42 6
	public function loadAllAcls(): array {
43 6
		$acls = [];
44 6
		foreach ($this->aclsArray as $aclArray) {
45 6
			$aclElement = new AclElement();
46 6
			$aclElement->fromArray($aclArray);
47 6
			$acls[$aclElement->getId_()] = $aclElement;
48
		}
49 6
		return $acls;
50
	}
51
52 4
	public function saveAcl(AclElement $aclElement) {
53 4
		$this->aclsArray[$aclElement->getId_()] = $aclElement->toArray();
54 4
	}
55
56 3
	public function removeAcl(AclElement $aclElement) {
57 3
		unset($this->aclsArray[$aclElement->getId_()]);
58 3
	}
59
60
	/**
61
	 *
62
	 * {@inheritdoc}
63
	 * @see \Ubiquity\security\acl\persistence\AclProviderInterface::loadAllPermissions()
64
	 */
65 6
	public function loadAllPermissions(): array {
66 6
		return $this->loadAllPart(Permission::class);
67
	}
68
69
	/**
70
	 *
71
	 * {@inheritdoc}
72
	 * @see \Ubiquity\security\acl\persistence\AclProviderInterface::loadAllResources()
73
	 */
74 6
	public function loadAllResources(): array {
75 6
		return $this->loadAllPart(Resource::class);
76
	}
77
78
	/**
79
	 *
80
	 * {@inheritdoc}
81
	 * @see \Ubiquity\security\acl\persistence\AclProviderInterface::loadAllRoles()
82
	 */
83 6
	public function loadAllRoles(): array {
84 6
		return $this->loadAllPart(Role::class);
85
	}
86
87 5
	public function savePart(\Ubiquity\security\acl\models\AbstractAclPart $part) {
88 5
		$class = \get_class($part);
89 5
		$this->parts[$class][$part->getName()] = $part->toArray();
90 5
	}
91
92 1
	public function updatePart(\Ubiquity\security\acl\models\AbstractAclPart $part) {
93 1
		$class = \get_class($part);
94 1
		$this->parts[$class][$part->getName()] = $part->toArray();
95 1
	}
96
97 4
	public function removePart(\Ubiquity\security\acl\models\AbstractAclPart $part) {
98 4
		$name = $part->getName();
99 4
		if ($part instanceof Resource) {
100 2
			$field = 'resource';
101 4
		} elseif ($part instanceof Role) {
102 3
			$field = 'role';
103
		} else {
104 3
			$field = 'permission';
105
		}
106 4
		foreach ($this->aclsArray as $acl) {
107 4
			if ($acl[$field]['name'] === $name) {
108
				throw new AclException("$name is in use in ACLs and can't be removed!");
109
			}
110
		}
111 4
		unset($this->parts[\get_class($part)][$name]);
112 4
	}
113
114 1
	public function existPart(AbstractAclPart $part): bool {
115 1
		$name = $part->getName();
116 1
		return isset($this->parts[\get_class($part)][$name]);
117
	}
118
119 1
	public function existAcl(AclElement $aclElement): bool {
120 1
		return isset($this->aclsArray[$aclElement->getId_()]);
121
	}
122
123
	public function getModelClassesSwap(): array {
124
		return [];
125
	}
126
}
127
128