@@ -13,7 +13,7 @@ final class InMemoryCipherKeyStore implements CipherKeyStore
1313 /** @var array<string, CipherKey> */
1414 private array $ indexById = [];
1515
16- /** @var array<string, list< CipherKey>> */
16+ /** @var array<string, array<string, CipherKey>> */
1717 private array $ indexBySubjectId = [];
1818
1919 public function currentKeyFor (string $ subjectId ): CipherKey
@@ -31,43 +31,31 @@ public function currentKeyFor(string $subjectId): CipherKey
3131 return $ this ->indexBySubjectId [$ subjectId ][$ lastKey ];
3232 }
3333
34- public function get (string $ keyId ): CipherKey
34+ public function get (string $ id ): CipherKey
3535 {
36- return $ this ->indexById [$ keyId ] ?? throw CipherKeyNotExists::forKeyId ($ keyId );
36+ return $ this ->indexById [$ id ] ?? throw CipherKeyNotExists::forKeyId ($ id );
3737 }
3838
39- public function store (string $ id , CipherKey $ key ): void
39+ public function store (CipherKey $ key ): void
4040 {
41- $ this ->indexById [ $ id ] = $ key ;
41+ $ this ->remove ( $ key-> id ) ;
4242
43- if (!isset ($ this ->indexBySubjectId [$ key ->subjectId ])) {
44- $ this ->indexBySubjectId [$ key ->subjectId ] = [];
45- }
46-
47- $ this ->indexBySubjectId [$ key ->subjectId ][] = $ key ;
43+ $ this ->indexById [$ key ->id ] = $ key ;
44+ $ this ->indexBySubjectId [$ key ->subjectId ][$ key ->id ] = $ key ;
4845 }
4946
5047 public function remove (string $ id ): void
5148 {
52- unset($ this ->indexById [$ id ]);
53-
54- foreach ($ this ->indexBySubjectId as $ subjectId => $ keys ) {
55- $ filtered = [];
56-
57- foreach ($ keys as $ key ) {
58- if ($ key ->id === $ id ) {
59- continue ;
60- }
49+ $ key = $ this ->indexById [$ id ] ?? null ;
6150
62- $ filtered [] = $ key ;
63- }
64-
65- if ($ filtered === []) {
66- unset($ this ->indexBySubjectId [$ subjectId ]);
67- } else {
68- $ this ->indexBySubjectId [$ subjectId ] = $ filtered ;
69- }
51+ if (!$ key ) {
52+ return ;
7053 }
54+
55+ unset(
56+ $ this ->indexBySubjectId [$ key ->subjectId ][$ id ],
57+ $ this ->indexById [$ id ],
58+ );
7159 }
7260
7361 public function clear (): void
0 commit comments