Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/Extension/Cryptography/Store/Psr16CacheStoreDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Extension\Cryptography\Store;

use DateInterval;
use Patchlevel\Hydrator\Extension\Cryptography\Cipher\CipherKey;
use Psr\SimpleCache\CacheInterface;

final readonly class Psr16CacheStoreDecorator implements CipherKeyStore
{
public function __construct(
private CipherKeyStore $cipherKeyStore,
private CacheInterface $cache,
private DateInterval|int|null $ttl = null,
) {
}

public function currentKeyFor(string $subjectId): CipherKey
{
$key = 'subjectId:' . $subjectId;
$entry = $this->cache->get($key);

if ($entry instanceof CipherKey) {
return $entry;
}

$entry = $this->cipherKeyStore->currentKeyFor($subjectId);

$this->cache->set($key, $entry, $this->ttl);

return $entry;
}

public function get(string $id): CipherKey
{
$key = 'id:' . $id;
$entry = $this->cache->get($key);

if ($entry instanceof CipherKey) {
return $entry;
}

$entry = $this->cipherKeyStore->get($id);

$this->cache->set($key, $entry, $this->ttl);

return $entry;
}

public function store(CipherKey $key): void
{
$this->cipherKeyStore->store($key);

$this->cache->set('id:' . $key->id, $key, $this->ttl);
$this->cache->set('subjectId:' . $key->subjectId, $key, $this->ttl);
}

public function remove(string $id): void
{
$this->cipherKeyStore->remove($id);

$this->cache->delete('id:' . $id);
}

public function removeWithSubjectId(string $subjectId): void
{
$this->cipherKeyStore->removeWithSubjectId($subjectId);

$this->cache->delete('subjectId:' . $subjectId);
}
}
76 changes: 76 additions & 0 deletions src/Extension/Cryptography/Store/Psr6CacheStoreDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Extension\Cryptography\Store;

use DateInterval;
use Patchlevel\Hydrator\Extension\Cryptography\Cipher\CipherKey;
use Psr\Cache\CacheItemPoolInterface;

final readonly class Psr6CacheStoreDecorator implements CipherKeyStore
{
public function __construct(
private CipherKeyStore $cipherKeyStore,
private CacheItemPoolInterface $cache,
private DateInterval|int|null $expiresAfter = null,
) {
}

public function currentKeyFor(string $subjectId): CipherKey
{
$key = 'subjectId:' . $subjectId;
$item = $this->cache->getItem($key);
$entry = $item->get();

if ($item->isHit() && $entry instanceof CipherKey) {
return $entry;
}

$entry = $this->cipherKeyStore->currentKeyFor($subjectId);

$item->set($entry);
$item->expiresAfter($this->expiresAfter);
$this->cache->save($item);

return $entry;
}

public function get(string $id): CipherKey
{
$key = 'id:' . $id;
$item = $this->cache->getItem($key);
$entry = $item->get();

if ($item->isHit() && $entry instanceof CipherKey) {

Check warning on line 45 in src/Extension/Cryptography/Store/Psr6CacheStoreDecorator.php

View workflow job for this annotation

GitHub Actions / Mutation tests on diff (locked, 8.5, ubuntu-latest)

Escaped Mutant for Mutator "LogicalAndSingleSubExprNegation": @@ @@ $item = $this->cache->getItem($key); $entry = $item->get(); - if ($item->isHit() && $entry instanceof CipherKey) { + if (!$item->isHit() && $entry instanceof CipherKey) { return $entry; }
return $entry;
}

$entry = $this->cipherKeyStore->get($id);

$item->set($entry);
$item->expiresAfter($this->expiresAfter);
$this->cache->save($item);

return $entry;
}

public function store(CipherKey $key): void
{
$this->cipherKeyStore->store($key);
}

public function remove(string $id): void
{
$this->cipherKeyStore->remove($id);

$this->cache->deleteItem('id:' . $id);
}

public function removeWithSubjectId(string $subjectId): void
{
$this->cipherKeyStore->removeWithSubjectId($subjectId);

$this->cache->deleteItem('subjectId:' . $subjectId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Tests\Unit\Extension\Cryptography\Store;

use DateTimeImmutable;
use Patchlevel\Hydrator\Extension\Cryptography\Cipher\CipherKey;
use Patchlevel\Hydrator\Extension\Cryptography\Store\CipherKeyStore;
use Patchlevel\Hydrator\Extension\Cryptography\Store\Psr16CacheStoreDecorator;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Psr\SimpleCache\CacheInterface;

#[CoversClass(Psr16CacheStoreDecorator::class)]
final class Psr16CacheStoreDecoratorTest extends TestCase
{
public function testCurrentKeyForWithCacheHit(): void
{
$key = $this->createKey();

$cache = $this->createMock(CacheInterface::class);
$cache->expects(self::once())->method('get')->with('subjectId:subject-1')->willReturn($key);
$cache->expects(self::never())->method('set');

$innerStore = $this->createMock(CipherKeyStore::class);
$innerStore->expects(self::never())->method('currentKeyFor');

$store = new Psr16CacheStoreDecorator($innerStore, $cache);

self::assertSame($key, $store->currentKeyFor('subject-1'));
}

public function testCurrentKeyForWithCacheMiss(): void
{
$key = $this->createKey();

$cache = $this->createMock(CacheInterface::class);
$cache->expects(self::once())->method('get')->with('subjectId:subject-1')->willReturn(null);
$cache->expects(self::once())->method('set')->with('subjectId:subject-1', $key, 42);

$innerStore = $this->createMock(CipherKeyStore::class);
$innerStore->expects(self::once())->method('currentKeyFor')->with('subject-1')->willReturn($key);

$store = new Psr16CacheStoreDecorator($innerStore, $cache, 42);

self::assertSame($key, $store->currentKeyFor('subject-1'));
}

public function testGetWithCacheMiss(): void
{
$key = $this->createKey();

$cache = $this->createMock(CacheInterface::class);
$cache->expects(self::once())->method('get')->with('id:key-1')->willReturn(false);
$cache->expects(self::once())->method('set')->with('id:key-1', $key, null);

$innerStore = $this->createMock(CipherKeyStore::class);
$innerStore->expects(self::once())->method('get')->with('key-1')->willReturn($key);

$store = new Psr16CacheStoreDecorator($innerStore, $cache);

self::assertSame($key, $store->get('key-1'));
}

public function testStoreWritesInnerStoreAndBothCacheEntries(): void
{
$key = $this->createKey();

$cache = $this->createMock(CacheInterface::class);
$cache->expects(self::exactly(2))->method('set')->willReturnMap([
['id:key-1', $key, 17, true],
['subjectId:subject-1', $key, 17, true],
]);

$innerStore = $this->createMock(CipherKeyStore::class);
$innerStore->expects(self::once())->method('store')->with($key);

$store = new Psr16CacheStoreDecorator($innerStore, $cache, 17);
$store->store($key);
}

public function testRemoveDeletesIdCacheEntry(): void
{
$cache = $this->createMock(CacheInterface::class);
$cache->expects(self::once())->method('delete')->with('id:key-1');

$innerStore = $this->createMock(CipherKeyStore::class);
$innerStore->expects(self::once())->method('remove')->with('key-1');

$store = new Psr16CacheStoreDecorator($innerStore, $cache);
$store->remove('key-1');
}

public function testRemoveWithSubjectIdDeletesSubjectCacheEntry(): void
{
$cache = $this->createMock(CacheInterface::class);
$cache->expects(self::once())->method('delete')->with('subjectId:subject-1');

$innerStore = $this->createMock(CipherKeyStore::class);
$innerStore->expects(self::once())->method('removeWithSubjectId')->with('subject-1');

$store = new Psr16CacheStoreDecorator($innerStore, $cache);
$store->removeWithSubjectId('subject-1');
}

private function createKey(): CipherKey
{
return new CipherKey(
'key-1',
'subject-1',
'secret',
'aes-256-gcm',
new DateTimeImmutable(),
);
}
}
Loading
Loading