Skip to content

Commit f1dad54

Browse files
committed
Introduce CacheAwareContext
1 parent 45a2aa6 commit f1dad54

File tree

7 files changed

+39
-18
lines changed

7 files changed

+39
-18
lines changed

src/Converter/Cache/Cache.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* @template TSource of object
99
* @template TTarget of object
10-
* @template TContext of object|null
10+
* @template TContext of CacheAwareContext|null
1111
*/
1212
interface Cache
1313
{
@@ -17,12 +17,12 @@ interface Cache
1717
*
1818
* @return TTarget|null
1919
*/
20-
public function get(object $source, ?object $ctx = null): ?object;
20+
public function get(object $source, ?CacheAwareContext $ctx = null): ?object;
2121

2222
/**
2323
* @param TSource $source
2424
* @param TTarget $target
2525
* @param TContext $ctx
2626
*/
27-
public function set(object $source, object $target, ?object $ctx = null): void;
27+
public function set(object $source, object $target, ?CacheAwareContext $ctx = null): void;
2828
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Neusta\ConverterBundle\Converter\Cache;
6+
7+
interface CacheAwareContext
8+
{
9+
/**
10+
* @return non-empty-string
11+
*/
12+
public function getHash(): string;
13+
}

src/Converter/Cache/CacheKeyFactory.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@
77

88
/**
99
* @template TSource of object
10-
* @template TContext of object|null
1110
*/
1211
interface CacheKeyFactory
1312
{
1413
/**
1514
* @param TSource $source
16-
* @param TContext $ctx
1715
*
1816
* @return non-empty-string
1917
*/
20-
public function createFor(object $source, ?object $ctx = null): string;
18+
public function createFor(object $source): string;
2119
}

src/Converter/Cache/InMemoryCache.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* @template TSource of object
99
* @template TTarget of object
10-
* @template TContext of object|null
10+
* @template TContext of CacheAwareContext|null
1111
*
1212
* @implements Cache<TSource, TTarget, TContext>
1313
*/
@@ -19,20 +19,28 @@ final class InMemoryCache implements Cache
1919
private array $targets = [];
2020

2121
/**
22-
* @param CacheKeyFactory<TSource, TContext> $keyFactory
22+
* @param CacheKeyFactory<TSource> $keyFactory
2323
*/
2424
public function __construct(
2525
private CacheKeyFactory $keyFactory,
2626
) {
2727
}
2828

29-
public function get(object $source, ?object $ctx = null): ?object
29+
public function get(object $source, ?CacheAwareContext $ctx = null): ?object
3030
{
31-
return $this->targets[$this->keyFactory->createFor($source, $ctx)] ?? null;
31+
return $this->targets[$this->createKey($source, $ctx)] ?? null;
3232
}
3333

34-
public function set(object $source, object $target, ?object $ctx = null): void
34+
public function set(object $source, object $target, ?CacheAwareContext $ctx = null): void
3535
{
36-
$this->targets[$this->keyFactory->createFor($source, $ctx)] = $target;
36+
$this->targets[$this->createKey($source, $ctx)] = $target;
37+
}
38+
39+
/**
40+
* @param TSource $source
41+
*/
42+
private function createKey(object $source, ?CacheAwareContext $ctx = null): string
43+
{
44+
return $this->keyFactory->createFor($source) . $ctx?->getHash();
3745
}
3846
}

src/Converter/CachedConverter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
namespace Neusta\ConverterBundle\Converter;
66

77
use Neusta\ConverterBundle\Converter\Cache\Cache;
8+
use Neusta\ConverterBundle\Converter\Cache\CacheAwareContext;
89
use Neusta\ConverterBundle\Converter;
910

1011
/**
1112
* @template TSource of object
1213
* @template TTarget of object
13-
* @template TContext of object|null
14+
* @template TContext of CacheAwareContext|null
1415
*
1516
* @implements Converter<TSource, TTarget, TContext>
1617
*/

src/Converter/Context/GenericContext.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace Neusta\ConverterBundle\Converter\Context;
66

7-
class GenericContext
7+
use Neusta\ConverterBundle\Converter\Cache\CacheAwareContext;
8+
9+
class GenericContext implements CacheAwareContext
810
{
911
/** @var array<string, mixed> */
1012
protected array $values;

tests/Fixtures/Converter/Cache/UserKeyFactory.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@
55
namespace Neusta\ConverterBundle\Tests\Fixtures\Converter\Cache;
66

77
use Neusta\ConverterBundle\Converter\Cache\CacheKeyFactory;
8-
use Neusta\ConverterBundle\Converter\DefaultConverterContext;
98
use Neusta\ConverterBundle\Tests\Fixtures\Model\User;
109

1110
/**
12-
* @implements CacheKeyFactory<User, DefaultConverterContext>
11+
* @implements CacheKeyFactory<User>
1312
*/
1413
class UserKeyFactory implements CacheKeyFactory
1514
{
16-
public function createFor(object $source, ?object $ctx = null): string
15+
public function createFor(object $source): string
1716
{
18-
return $source->getUuid() . $ctx?->getHash();
17+
return (string) $source->getUuid();
1918
}
2019
}

0 commit comments

Comments
 (0)