Skip to content

Commit 9bc9975

Browse files
committed
Clean up comments, move code around by topic
1 parent b554d4c commit 9bc9975

File tree

1 file changed

+40
-34
lines changed

1 file changed

+40
-34
lines changed

app/AbstractRequest.php

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,33 @@ protected function responseFromCache(): ?Response
351351
return null;
352352
}
353353

354+
/**
355+
* Return cache key string based on this class, URL, and request body
356+
* @return string
357+
*/
358+
public function cacheKey(): string
359+
{
360+
return hash(
361+
'sha256',
362+
json_encode([
363+
self::class,
364+
$this->getURL(),
365+
$this->encodeBody(),
366+
config('api-request.cache_key_seed', 'v2024.8.6'),
367+
]),
368+
);
369+
}
370+
371+
public function canBeFulfilledByCache(): bool
372+
{
373+
return Cache::tags($this->cacheTags)->has($this->cacheKey());
374+
}
375+
376+
public function isFromCache(): bool
377+
{
378+
return $this->responseIsFromCache;
379+
}
380+
354381
/**
355382
* For caching the response, how long should the cache be valid?
356383
* By default, this is one day. To customize, override this method.
@@ -398,6 +425,19 @@ public function getLastLogContents(): string
398425
return LogFile::disk()->get($this->getLastLogFile());
399426
}
400427

428+
/**
429+
* Get the filename for the last run of this instance of this request.
430+
* If the last sync or async was a cache hit, this will return the original log of the request that was cached
431+
* @throws DomainException if this instance has never logged (could mean never run, or $shouldLog is false)
432+
*/
433+
public function getLastLogFile(): string
434+
{
435+
if (!$this->sentLogs) {
436+
throw new DomainException('No log files have been saved by this instance.');
437+
}
438+
return Str::finish($this->getLogFolder(), '/') . Arr::last($this->sentLogs);
439+
}
440+
401441
/**
402442
* Can be overridden by children or trait to parse the body in a known format (e.g. JSON or XML)
403443
* @param string $responseString
@@ -442,36 +482,6 @@ public function getResponse(): ?Response
442482
return $this->response;
443483
}
444484

445-
/**
446-
* Return cache key string based on this class, URL, and request body
447-
* @return string
448-
*/
449-
public function cacheKey(): string
450-
{
451-
return hash(
452-
'sha256',
453-
json_encode([
454-
self::class,
455-
$this->getURL(),
456-
$this->encodeBody(),
457-
config('api-request.cache_key_seed', 'v2024.8.6'),
458-
]),
459-
);
460-
}
461-
462-
public function canBeFulfilledByCache(): bool
463-
{
464-
return Cache::tags($this->cacheTags)->has($this->cacheKey());
465-
}
466-
467-
public function getLastLogFile(): string
468-
{
469-
if (!$this->sentLogs) {
470-
throw new DomainException('No log files have been saved by this instance.');
471-
}
472-
return Str::finish($this->getLogFolder(), '/') . Arr::last($this->sentLogs);
473-
}
474-
475485
/**
476486
* Change the timeout of this request. This is the preferred way to override the default,
477487
* even in the constructor of a custom class I think this is much more expressive than a numeric constant
@@ -482,8 +492,4 @@ public function setTimeout(CarbonInterval $interval): void
482492
$this->guzzleOptions[RequestOptions::TIMEOUT] = $interval->totalSeconds;
483493
}
484494

485-
public function isFromCache(): bool
486-
{
487-
return $this->responseIsFromCache;
488-
}
489495
}

0 commit comments

Comments
 (0)