Skip to content

Commit d3aa49b

Browse files
committed
refactor(cache): allows enum tags for RestrictedCache
1 parent 82a6bc3 commit d3aa49b

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

packages/cache/src/Testing/RestrictedCache.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,67 +10,73 @@
1010
use Tempest\Cache\Lock;
1111
use Tempest\DateTime\DateTimeInterface;
1212
use Tempest\DateTime\Duration;
13+
use UnitEnum;
1314

1415
final class RestrictedCache implements Cache
1516
{
1617
public bool $enabled;
1718

1819
public function __construct(
19-
private ?string $tag = null,
20+
private null|string|UnitEnum $tag = null,
2021
) {}
2122

23+
private function resolveTag(): ?string
24+
{
25+
return $this->tag instanceof UnitEnum ? $this->tag->name : $this->tag;
26+
}
27+
2228
public function lock(Stringable|string $key, null|Duration|DateTimeInterface $expiration = null, null|Stringable|string $owner = null): Lock
2329
{
24-
throw new CacheUsageWasForbidden($this->tag);
30+
throw new CacheUsageWasForbidden($this->resolveTag());
2531
}
2632

2733
public function has(Stringable|string $key): bool
2834
{
29-
throw new CacheUsageWasForbidden($this->tag);
35+
throw new CacheUsageWasForbidden($this->resolveTag());
3036
}
3137

3238
public function put(Stringable|string $key, mixed $value, null|Duration|DateTimeInterface $expiration = null): CacheItemInterface
3339
{
34-
throw new CacheUsageWasForbidden($this->tag);
40+
throw new CacheUsageWasForbidden($this->resolveTag());
3541
}
3642

3743
public function putMany(iterable $values, null|Duration|DateTimeInterface $expiration = null): array
3844
{
39-
throw new CacheUsageWasForbidden($this->tag);
45+
throw new CacheUsageWasForbidden($this->resolveTag());
4046
}
4147

4248
public function increment(Stringable|string $key, int $by = 1): int
4349
{
44-
throw new CacheUsageWasForbidden($this->tag);
50+
throw new CacheUsageWasForbidden($this->resolveTag());
4551
}
4652

4753
public function decrement(Stringable|string $key, int $by = 1): int
4854
{
49-
throw new CacheUsageWasForbidden($this->tag);
55+
throw new CacheUsageWasForbidden($this->resolveTag());
5056
}
5157

5258
public function get(Stringable|string $key): mixed
5359
{
54-
throw new CacheUsageWasForbidden($this->tag);
60+
throw new CacheUsageWasForbidden($this->resolveTag());
5561
}
5662

5763
public function getMany(iterable $key): array
5864
{
59-
throw new CacheUsageWasForbidden($this->tag);
65+
throw new CacheUsageWasForbidden($this->resolveTag());
6066
}
6167

6268
public function resolve(Stringable|string $key, Closure $callback, null|Duration|DateTimeInterface $expiration = null, ?Duration $stale = null): mixed
6369
{
64-
throw new CacheUsageWasForbidden($this->tag);
70+
throw new CacheUsageWasForbidden($this->resolveTag());
6571
}
6672

6773
public function remove(Stringable|string $key): void
6874
{
69-
throw new CacheUsageWasForbidden($this->tag);
75+
throw new CacheUsageWasForbidden($this->resolveTag());
7076
}
7177

7278
public function clear(): void
7379
{
74-
throw new CacheUsageWasForbidden($this->tag);
80+
throw new CacheUsageWasForbidden($this->resolveTag());
7581
}
7682
}

0 commit comments

Comments
 (0)