-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathInMemoryCipherKeyStore.php
More file actions
80 lines (60 loc) · 1.94 KB
/
Copy pathInMemoryCipherKeyStore.php
File metadata and controls
80 lines (60 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
declare(strict_types=1);
namespace Patchlevel\Hydrator\Extension\Cryptography\Store;
use Patchlevel\Hydrator\Extension\Cryptography\Cipher\CipherKey;
use function array_key_last;
/** @experimental */
final class InMemoryCipherKeyStore implements CipherKeyStore
{
/** @var array<string, CipherKey> */
private array $indexById = [];
/** @var array<string, array<string, CipherKey>> */
private array $indexBySubjectId = [];
public function currentKeyFor(string $subjectId): CipherKey
{
if (!isset($this->indexBySubjectId[$subjectId])) {
throw CipherKeyNotExists::forSubjectId($subjectId);
}
$lastKey = array_key_last($this->indexBySubjectId[$subjectId]);
if ($lastKey === null) {
throw CipherKeyNotExists::forSubjectId($subjectId);
}
return $this->indexBySubjectId[$subjectId][$lastKey];
}
public function get(string $id): CipherKey
{
return $this->indexById[$id] ?? throw CipherKeyNotExists::forKeyId($id);
}
public function store(CipherKey $key): void
{
$this->remove($key->id);
$this->indexById[$key->id] = $key;
$this->indexBySubjectId[$key->subjectId][$key->id] = $key;
}
public function remove(string $id): void
{
$key = $this->indexById[$id] ?? null;
if (!$key) {
return;
}
unset(
$this->indexBySubjectId[$key->subjectId][$id],
$this->indexById[$id],
);
}
public function clear(): void
{
$this->indexById = [];
$this->indexBySubjectId = [];
}
public function removeWithSubjectId(string $subjectId): void
{
if (!isset($this->indexBySubjectId[$subjectId])) {
return;
}
foreach ($this->indexBySubjectId[$subjectId] as $key) {
unset($this->indexById[$key->id]);
}
unset($this->indexBySubjectId[$subjectId]);
}
}