Test Failed
Push — main ( af0bbd...b98fce )
by Jean-Christophe
02:19
created

AclCacheProvider::saveAcl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
namespace Ubiquity\security\acl\persistence;
3
4
use Ubiquity\cache\CacheManager;
5
use Ubiquity\security\acl\models\AbstractAclPart;
6
use Ubiquity\security\acl\models\AclElement;
7
use Ubiquity\security\acl\models\Role;
8
use Ubiquity\security\acl\models\Resource;
9
use Ubiquity\security\acl\models\Permission;
10
11
class AclCacheProvider extends AclArrayProvider {
12
13
	private $key = 'acls/';
14
15
	private function getRootKey($element) {
16
		return $this->key . $element;
17
	}
18
19
	public function __construct() {
20
		$cacheKeys = $this->getCacheKeys();
21
		foreach ($cacheKeys as $key) {
22
			$this->createCache($key);
23
		}
24
	}
25
26
	protected function getCacheKeys(): array {
27
		return [
28
			$this->getRootKey('acls'),
29
			$this->getRootKey(Role::class),
30
			$this->getRootKey(Resource::class),
31
			$this->getRootKey(Permission::class)
32
		];
33
	}
34
35
	protected function createCache($part) {
36
		if (! CacheManager::$cache->exists($part)) {
37
			CacheManager::$cache->store($part, []);
38
		}
39
	}
40
41
	protected function loadAllPart($class): array {
42
		$this->parts[$class] = CacheManager::$cache->fetch($this->getRootKey($class));
43
		return parent::loadAllPart($class);
44
	}
45
46
	public function loadAllAcls(): array {
47
		$this->aclsArray = CacheManager::$cache->fetch($this->getRootKey('acls'));
48
		return parent::loadAllAcls();
49
	}
50
51
	public function savePart(AbstractAclPart $part) {
52
		$this->_savePart($part);
53
		$class = get_class($part);
54
		CacheManager::$cache->store($this->getRootKey($class), $this->parts[$class]);
55
	}
56
57
	public function saveAcl(AclElement $aclElement) {
58
		$this->_saveAcl($aclElement);
59
		CacheManager::$cache->store($this->getRootKey('acls'), $this->aclsArray);
60
	}
61
62
	public function isAutosave(): bool {
63
		return false;
64
	}
65
66
	public function saveAll(): void {
67
		CacheManager::$cache->store($this->getRootKey('acls'), $this->aclsArray);
68
		$classes = [
69
			Role::class,
70
			Resource::class,
71
			Permission::class
72
		];
73
		foreach ($classes as $class) {
74
			CacheManager::$cache->store($this->getRootKey($class), $this->parts[$class]);
75
		}
76
	}
77
}
78
79