Skip to content

Commit 690c7fc

Browse files
committed
refactor: replace custom CacheEnum implementation with native PHP enums
1 parent 447e1f2 commit 690c7fc

19 files changed

+284
-272
lines changed

.phpunit.cache/test-results

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

src/Cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Mostafaznv\LaraCache;
44

55
use Mostafaznv\LaraCache\DTOs\CacheData;
6-
use Mostafaznv\LaraCache\DTOs\CacheEvent;
6+
use Mostafaznv\LaraCache\Enums\CacheEvent;
77
use Mostafaznv\LaraCache\Jobs\RefreshCache;
88
use Mostafaznv\LaraCache\Traits\InteractsWithCache;
99
use Mostafaznv\LaraCache\Utils\RefreshDebouncer;

src/DTOs/CacheData.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
use Carbon\Carbon;
66
use Illuminate\Support\Facades\Cache;
77
use Mostafaznv\LaraCache\CacheEntity;
8+
use Mostafaznv\LaraCache\Enums\CacheStatus;
9+
810

911
class CacheData
1012
{
1113
public function __construct(
1214
public CacheStatus $status,
1315
public ?int $expiration,
14-
public mixed $value
16+
public mixed $value,
1517
) {}
1618

19+
1720
public static function make(CacheStatus $status, int $ttl, mixed $value): self
1821
{
1922
$expiration = $ttl ? Carbon::now()->addSeconds($ttl)->unix() : null;
@@ -27,7 +30,7 @@ public static function fromCache(CacheEntity $entity, string $prefix, int $ttl =
2730
$value = Cache::store($entity->driver)->get($name, $entity->default);
2831

2932
if ($value === $entity->default) {
30-
return self::make(CacheStatus::NOT_CREATED(), $ttl, $entity->default);
33+
return self::make(CacheStatus::NOT_CREATED, $ttl, $entity->default);
3134
}
3235

3336
return $value;

src/DTOs/CacheEvent.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/DTOs/CacheStatus.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/Enums/CacheEvent.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Mostafaznv\LaraCache\Enums;
4+
5+
6+
enum CacheEvent
7+
{
8+
case RETRIEVED;
9+
case CREATED;
10+
case UPDATED;
11+
case DELETED;
12+
case RESTORED;
13+
}

src/Enums/CacheStatus.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Mostafaznv\LaraCache\Enums;
4+
5+
6+
enum CacheStatus
7+
{
8+
case NOT_CREATED;
9+
case CREATING;
10+
case CREATED;
11+
case DELETED;
12+
}

src/Jobs/RefreshCache.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
use Illuminate\Foundation\Bus\Dispatchable;
88
use Illuminate\Queue\InteractsWithQueue;
99
use Illuminate\Queue\SerializesModels;
10-
use Mostafaznv\LaraCache\DTOs\CacheEvent;
10+
use Mostafaznv\LaraCache\Enums\CacheEvent;
11+
1112

1213
class RefreshCache implements ShouldQueue
1314
{
@@ -16,9 +17,10 @@ class RefreshCache implements ShouldQueue
1617
public function __construct(
1718
private string $model,
1819
private string $name,
19-
private CacheEvent $event
20+
private CacheEvent $event,
2021
) {}
2122

23+
2224
public function handle(): void
2325
{
2426
$model = app($this->model);

src/Observers/LaraCacheObserver.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,31 @@
22

33
namespace Mostafaznv\LaraCache\Observers;
44

5-
use Mostafaznv\LaraCache\DTOs\CacheEvent;
5+
use Mostafaznv\LaraCache\Enums\CacheEvent;
6+
67

78
final class LaraCacheObserver
89
{
910
public function created(mixed $model): void
1011
{
11-
$model->cache()->refresh(CacheEvent::CREATED());
12+
$model->cache()->refresh(CacheEvent::CREATED);
1213
}
1314

1415
public function updated(mixed $model): void
1516
{
1617
if (!$this->isRestored($model)) {
17-
$model->cache()->refresh(CacheEvent::UPDATED());
18+
$model->cache()->refresh(CacheEvent::UPDATED);
1819
}
1920
}
2021

2122
public function deleted(mixed $model): void
2223
{
23-
$model->cache()->refresh(CacheEvent::DELETED());
24+
$model->cache()->refresh(CacheEvent::DELETED);
2425
}
2526

2627
public function restored(mixed $model): void
2728
{
28-
$model->cache()->refresh(CacheEvent::RESTORED());
29+
$model->cache()->refresh(CacheEvent::RESTORED);
2930
}
3031

3132
private function isRestored(mixed $model): bool

src/Traits/InteractsWithCache.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
use Illuminate\Support\Str;
77
use Mostafaznv\LaraCache\CacheEntity;
88
use Mostafaznv\LaraCache\DTOs\CacheData;
9-
use Mostafaznv\LaraCache\DTOs\CacheEvent;
10-
use Mostafaznv\LaraCache\DTOs\CacheStatus;
9+
use Mostafaznv\LaraCache\Enums\CacheEvent;
10+
use Mostafaznv\LaraCache\Enums\CacheStatus;
1111
use Mostafaznv\LaraCache\Exceptions\CacheEntityDoesNotExist;
1212
use Mostafaznv\LaraCache\Jobs\RefreshCache;
1313
use Mostafaznv\LaraCache\Jobs\UpdateLaraCacheModelsList;
1414

15+
1516
trait InteractsWithCache
1617
{
1718
private string $prefix;
18-
private mixed $model;
19+
private mixed $model;
1920
private string $laracacheListKey;
2021

2122
public function __construct(string $model)
@@ -49,22 +50,22 @@ private function findCacheEntity(string $name, ?CacheEntity $entity = null): Cac
4950
private function entityIsCallable(CacheEntity $entity, ?CacheEvent $event = null): bool
5051
{
5152
return is_null($event)
52-
or ($event->equals(CacheEvent::CREATED()) and $entity->refreshAfterCreate)
53-
or ($event->equals(CacheEvent::UPDATED()) and $entity->refreshAfterUpdate)
54-
or ($event->equals(CacheEvent::DELETED()) and $entity->refreshAfterDelete)
55-
or ($event->equals(CacheEvent::RESTORED()) and $entity->refreshAfterRestore);
53+
or ($event === CacheEvent::CREATED and $entity->refreshAfterCreate)
54+
or ($event === CacheEvent::UPDATED and $entity->refreshAfterUpdate)
55+
or ($event === CacheEvent::DELETED and $entity->refreshAfterDelete)
56+
or ($event === CacheEvent::RESTORED and $entity->refreshAfterRestore);
5657
}
5758

5859
private function callCacheClosure(CacheEntity $entity, int $ttl, bool $delete = false): CacheData
5960
{
6061
if ($delete) {
61-
return CacheData::make(CacheStatus::DELETED(), $ttl, $entity->default);
62+
return CacheData::make(CacheStatus::DELETED, $ttl, $entity->default);
6263
}
6364

6465
$value = $entity->cacheClosure ? call_user_func($entity->cacheClosure) : null;
6566

6667
return CacheData::make(
67-
status: CacheStatus::CREATED(),
68+
status: CacheStatus::CREATED,
6869
ttl: $ttl,
6970
value: $value ?: $entity->default
7071
);
@@ -108,11 +109,11 @@ private function initCache(CacheEntity $entity, int $ttl): void
108109
$name = $this->getEntityFullName($entity);
109110
$cache = CacheData::fromCache($entity, $this->prefix, $ttl);
110111

111-
if ($cache->status->equals(CacheStatus::NOT_CREATED())) {
112+
if ($cache->status === CacheStatus::NOT_CREATED) {
112113
$cache->value = $entity->default;
113114
}
114115

115-
$cache->status = CacheStatus::CREATING();
116+
$cache->status = CacheStatus::CREATING;
116117

117118
$this->putCacheIntoCacheStorage($cache, $entity->driver, $name, $ttl);
118119
}
@@ -162,11 +163,11 @@ private function retrieve(string $name): CacheData
162163
$entity = $this->findCacheEntity($name);
163164
$cache = CacheData::fromCache($entity, $this->prefix);
164165

165-
if ($cache->status->equals(CacheStatus::NOT_CREATED())) {
166+
if ($cache->status === CacheStatus::NOT_CREATED) {
166167
if ($entity->isQueueable) {
167168
$this->initCache($entity, $entity->getTtl());
168169

169-
RefreshCache::dispatch($this->model, $entity->name, CacheEvent::RETRIEVED())
170+
RefreshCache::dispatch($this->model, $entity->name, CacheEvent::RETRIEVED)
170171
->onConnection($entity->queueConnection)
171172
->onQueue($entity->queueName);
172173

0 commit comments

Comments
 (0)