Skip to content

Commit

Permalink
add guard transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
EriBloo committed Oct 15, 2024
1 parent 21c18ee commit f1702c6
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/ValueObjects/Values/GuardTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace EriBloo\CacheObjects\ValueObjects\Values;

use EriBloo\CacheObjects\Contracts\CacheValueTransformer;
use Closure;

/**
* @template TValue
*
* @implements CacheValueTransformer<TValue>
*/
final readonly class GuardTransformer implements CacheValueTransformer
{
/**
* @param CacheValueTransformer<TValue> $transformer
* @param (Closure(TValue): void)|null $onSaveGuard
* @param (Closure(TValue): void)|null $onLoadGuard
*/
public function __construct(
private CacheValueTransformer $transformer,
private Closure|null $onSaveGuard = null,
private Closure|null $onLoadGuard = null,
) {}

public function onLoad(string $value): mixed
{
$transformed = $this->transformer->onLoad($value);

if ($this->onLoadGuard !== null) {
call_user_func($this->onLoadGuard, $transformed);
}

return $transformed;
}

public function onSave(mixed $value): string
{
if ($this->onSaveGuard !== null) {
call_user_func($this->onSaveGuard, $value);
}

return $this->transformer->onSave($value);
}
}
27 changes: 27 additions & 0 deletions tests/CacheObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use EriBloo\CacheObjects\Tests\Fixtures\BasicCacheObject;
use EriBloo\CacheObjects\Tests\Fixtures\EncryptedCacheObject;
use EriBloo\CacheObjects\Tests\Fixtures\GuardCacheObject;
use EriBloo\CacheObjects\Tests\Fixtures\HashedCacheObject;
use EriBloo\CacheObjects\Tests\Fixtures\ObjectWithTime;

use function PHPUnit\Framework\assertEquals;
use function PHPUnit\Framework\assertNotNull;
Expand Down Expand Up @@ -57,3 +59,28 @@
assertEquals(serialize('test'), Crypt::decryptString(app('store')->get((string) $obj->key())));
assertEquals('test', $obj->retrieve());
});

it('guards on save properly', function () {
// prepare
$obj = new GuardCacheObject(function (ObjectWithTime $value) {
if ($value->time->isFuture()) {
throw new InvalidArgumentException();
}
}, null);

// execute and assert
$obj->store(new ObjectWithTime(now()->addHour()));
})->throws(InvalidArgumentException::class);

it('guards on load properly', function () {
// prepare
$obj = new GuardCacheObject(null, function ($value) {
if ($value->time->isFuture()) {
throw new InvalidArgumentException();
}
});
$obj->store(value: new ObjectWithTime(now()->addHour()));

// execute and assert
$obj->retrieve();
})->throws(InvalidArgumentException::class);
46 changes: 46 additions & 0 deletions tests/Fixtures/GuardCacheObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace EriBloo\CacheObjects\Tests\Fixtures;

use Carbon\CarbonInterval;
use EriBloo\CacheObjects\Concerns\CacheObjectActions;
use EriBloo\CacheObjects\Contracts\CacheObject;
use EriBloo\CacheObjects\ValueObjects\Keys\StringKey;
use EriBloo\CacheObjects\ValueObjects\Values\GuardTransformer;
use EriBloo\CacheObjects\ValueObjects\Values\SerializeTransformer;
use Closure;

/**
* @template ObjectWithTime
*
* @implements CacheObject<ObjectWithTime>
*/
final class GuardCacheObject implements CacheObject
{
use CacheObjectActions;

public function __construct(
private Closure|null $onSaveGuard,
private Closure|null $onLoadGuard,
) {}

public function key(): StringKey
{
return new StringKey('guard-cache-object');
}

public function ttl(): CarbonInterval
{
return CarbonInterval::seconds(0);
}

/**
* @return GuardTransformer<ObjectWithTime>
*/
public function transformer(): GuardTransformer
{
return new GuardTransformer(new SerializeTransformer(), $this->onSaveGuard, $this->onLoadGuard);
}
}
14 changes: 14 additions & 0 deletions tests/Fixtures/ObjectWithTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace EriBloo\CacheObjects\Tests\Fixtures;

use Carbon\CarbonInterface;

final readonly class ObjectWithTime
{
public function __construct(
public CarbonInterface $time,
) {}
}

0 comments on commit f1702c6

Please sign in to comment.