Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
63.64% |
7 / 11 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| LaravelCache | |
63.64% |
7 / 11 |
|
50.00% |
2 / 4 |
7.73 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
62.50% |
5 / 8 |
|
0.00% |
0 / 1 |
3.47 | |||
| delete | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| createItem | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Apie\LaravelApie\Wrappers\Cache; |
| 4 | |
| 5 | use Illuminate\Contracts\Cache\Repository as Cache; |
| 6 | use Symfony\Contracts\Cache\CacheInterface; |
| 7 | use Symfony\Contracts\Cache\ItemInterface; |
| 8 | |
| 9 | class LaravelCache implements CacheInterface |
| 10 | { |
| 11 | public function __construct( |
| 12 | private readonly Cache $cache, |
| 13 | ) { |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * @param array<array-key, mixed>|null $metadata |
| 18 | */ |
| 19 | public function get( |
| 20 | string $key, |
| 21 | callable $callback, |
| 22 | ?float $beta = null, |
| 23 | ?array &$metadata = null, |
| 24 | ): mixed { |
| 25 | if ($beta === INF) { |
| 26 | $this->cache->forget($key); |
| 27 | } |
| 28 | // Laravel's cache can restrict unserialize() to allowed classes (cache.serializable_classes), |
| 29 | // which turns objects like Symfony\UX\Icons\Icon into __PHP_Incomplete_Class. Store a string instead. |
| 30 | $generate = fn () => serialize($callback($this->createItem($key), true)); |
| 31 | /** @var mixed $serialized */ |
| 32 | $serialized = $this->cache->remember($key, now()->addMinutes(60), $generate); |
| 33 | if (!is_string($serialized)) { |
| 34 | // entry written by an older version of this class |
| 35 | $this->cache->forget($key); |
| 36 | $serialized = $this->cache->remember($key, now()->addMinutes(60), $generate); |
| 37 | } |
| 38 | |
| 39 | return unserialize($serialized); |
| 40 | } |
| 41 | |
| 42 | public function delete(string $key): bool |
| 43 | { |
| 44 | return $this->cache->forget($key); |
| 45 | } |
| 46 | |
| 47 | private function createItem(string $key): ItemInterface |
| 48 | { |
| 49 | return new LaravelCacheItem($key); |
| 50 | } |
| 51 | } |