From 1a6e2dda0fdfe87aacb05076b5d09e8deee69048 Mon Sep 17 00:00:00 2001 From: Jeremy Wadhams Date: Thu, 2 Apr 2026 12:04:36 -0500 Subject: [PATCH] feat: add getCacheTags() method to replace direct property access Implementers can now override getCacheTags() to derive tags from other properties rather than setting state in the constructor. The property remains as the default backing store but is no longer accessed directly. Co-Authored-By: Claude Sonnet 4.6 --- app/AbstractRequest.php | 13 +++++++++---- app/AbstractUseStaleRequest.php | 8 ++++---- app/Testing/RequestClassAssertions.php | 3 +-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/app/AbstractRequest.php b/app/AbstractRequest.php index cb83e41..cfeb09c 100644 --- a/app/AbstractRequest.php +++ b/app/AbstractRequest.php @@ -46,6 +46,11 @@ abstract class AbstractRequest /** @var array Tags to be used when inserting to cache */ protected array $cacheTags = []; + public function getCacheTags(): array + { + return $this->cacheTags; + } + /** @var string Request method, for use in toGuzzle */ protected string $method = 'POST'; @@ -317,7 +322,7 @@ protected function getGuzzleClient(): Client public function purgeCache(): self { - Cache::tags($this->cacheTags)->forget($this->cacheKey()); + Cache::tags($this->getCacheTags())->forget($this->cacheKey()); return $this; } @@ -338,7 +343,7 @@ protected function writeResponseToCache(): void // so we flatten the body to strings then rehydrate the Response class manually // Note, when the format of the cached value changes, you have to update CACHE_KEY_SEED // So that previous cache entries with incompatible cached data are not read by responseFromCache - Cache::tags($this->cacheTags)->put( + Cache::tags($this->getCacheTags())->put( $this->cacheKey(), [ 'logs' => $this->sentLogs, @@ -363,7 +368,7 @@ protected function responseFromCache(): ?Response // Note, when the format of $fromCache changes, you have to update CACHE_KEY_SEED // So that previous cache entries with incompatible cached data are not read by responseFromCache - $fromCache = Cache::tags($this->cacheTags)->get($this->cacheKey()); + $fromCache = Cache::tags($this->getCacheTags())->get($this->cacheKey()); if ($fromCache) { $this->sentLogs = array_map( fn ($filename) => Str::contains($filename, '/') @@ -396,7 +401,7 @@ public function cacheKey(): string public function canBeFulfilledByCache(): bool { - return Cache::tags($this->cacheTags)->has($this->cacheKey()); + return Cache::tags($this->getCacheTags())->has($this->cacheKey()); } public function isFromCache(): bool diff --git a/app/AbstractUseStaleRequest.php b/app/AbstractUseStaleRequest.php index 3a37172..279f665 100644 --- a/app/AbstractUseStaleRequest.php +++ b/app/AbstractUseStaleRequest.php @@ -21,7 +21,7 @@ protected function responseFromCache(): ?Response { $cachedResponse = parent::responseFromCache(); if ($cachedResponse && $this->needsRefresh()) { - Cache::tags($this->cacheTags)->put( + Cache::tags($this->getCacheTags())->put( $this->refreshCacheKey(), 'Wait between refreshes', $this->waitBetweenRefreshes(), @@ -44,7 +44,7 @@ protected function responseFromCache(): ?Response protected function writeResponseToCache(): void { if ($this->shouldWriteResponseToCache()) { - Cache::tags($this->cacheTags)->put($this->refreshCacheKey(), 'refresh after', $this->refreshAfter()); + Cache::tags($this->getCacheTags())->put($this->refreshCacheKey(), 'refresh after', $this->refreshAfter()); } parent::writeResponseToCache(); } @@ -63,12 +63,12 @@ public function refreshCacheKey(): string public function needsRefresh(): bool { - return !Cache::tags($this->cacheTags)->has($this->refreshCacheKey()); + return !Cache::tags($this->getCacheTags())->has($this->refreshCacheKey()); } public function refreshOnNextRequest(): self { - Cache::tags($this->cacheTags)->forget($this->refreshCacheKey()); + Cache::tags($this->getCacheTags())->forget($this->refreshCacheKey()); return $this; } diff --git a/app/Testing/RequestClassAssertions.php b/app/Testing/RequestClassAssertions.php index d350af5..921b518 100644 --- a/app/Testing/RequestClassAssertions.php +++ b/app/Testing/RequestClassAssertions.php @@ -21,8 +21,7 @@ protected static function mockRequestCachedResponse( array $headers = [], array $logs = [], ): void { - $tags = getProperty($request, 'cacheTags'); - Cache::tags($tags)->put($request->cacheKey(), [ + Cache::tags($request->getCacheTags())->put($request->cacheKey(), [ 'logs' => $logs, 'response' => [$status, $headers, $body], ]);