AclCacheProvider::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 4
ccs 4
cts 4
cp 1
crap 2
rs 10
c 1
b 0
f 1
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
use Ubiquity\security\acl\cache\traits\AclCacheTrait;
11
12
/**
13
 * Ubiquity\security\acl\persistence$AclCacheProvider
14
 * This class is part of Ubiquity
15
 *
16
 * @author jc
17
 * @version 1.0.0
18
 *
19
 */
20
class AclCacheProvider extends AclArrayProvider {
21
	use AclCacheTrait;
22
23 6
	public function __construct() {
24 6
		$cacheKeys = $this->getCacheKeys();
25 6
		foreach ($cacheKeys as $key) {
26 6
			$this->createCache($key);
27
		}
28
	}
29
30 6
	protected function getCacheKeys(): array {
31 6
		return [
32 6
			$this->getRootKey('acls'),
33 6
			$this->getRootKey(Role::class),
34 6
			$this->getRootKey(Resource::class),
35 6
			$this->getRootKey(Permission::class)
36 6
		];
37
	}
38
39 6
	protected function loadAllPart($class): array {
40 6
		$this->parts[$class] = CacheManager::$cache->fetch($this->getRootKey($class));
41 6
		return parent::loadAllPart($class);
42
	}
43
44 6
	public function loadAllAcls(): array {
45 6
		$this->aclsArray = CacheManager::$cache->fetch($this->getRootKey('acls'));
46 6
		return parent::loadAllAcls();
47
	}
48
49 3
	public function isAutosave(): bool {
50 3
		return false;
51
	}
52
53 3
	public function saveAll(): void {
54 3
		CacheManager::$cache->store($this->getRootKey('acls'), $this->aclsArray);
55 3
		$classes = [
56 3
			Role::class,
57 3
			Resource::class,
58 3
			Permission::class
59 3
		];
60 3
		foreach ($classes as $class) {
61 3
			CacheManager::$cache->store($this->getRootKey($class), $this->parts[$class] ?? []);
62
		}
63
	}
64
	
65
	public function cacheUpdated(): bool {
66
		$old=CacheManager::$cache->fetch($this->getRootKey('acls'));
67
		if ($old!=$this->aclsArray) {
68
			return true;
69
		}
70
		$classes = [
71
			Role::class,
72
			Resource::class,
73
			Permission::class
74
		];
75
		foreach ($classes as $class) {
76
			$old=CacheManager::$cache->fetch($this->getRootKey($class));
77
			if ($old!=($this->parts[$class]??[])) {
78
				return true;
79
			}
80
		}
81
		return false;
82
	}
83
84
	public function getDetails(): array {
85
		return [
86
			'lock' => 'In memory or annotations cache'
87
		];
88
	}
89
}
90
91