Files
SkinbaseNova/app/Observers/ArtworkFeatureObserver.php
T

92 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Observers;
use App\Jobs\GenerateFeaturedArtworkThumbnailsJob;
use App\Models\Artwork;
use App\Models\ArtworkFeature;
use App\Services\HomepageService;
use App\Services\Profile\CreatorJourneyService;
final class ArtworkFeatureObserver
{
public function __construct(
private readonly HomepageService $homepage,
private readonly CreatorJourneyService $journeys,
)
{
}
public function created(ArtworkFeature $feature): void
{
$this->homepage->clearFeaturedAndMedalCaches();
$this->queueFeaturedThumbnailRefresh($feature);
$this->queueCreatorRebuild($feature);
}
public function updated(ArtworkFeature $feature): void
{
$this->homepage->clearFeaturedAndMedalCaches();
$this->queueFeaturedThumbnailRefresh($feature);
$this->queueCreatorRebuild($feature);
}
public function deleted(ArtworkFeature $feature): void
{
$this->homepage->clearFeaturedAndMedalCaches();
$this->queueCreatorRebuild($feature);
}
public function restored(ArtworkFeature $feature): void
{
$this->homepage->clearFeaturedAndMedalCaches();
$this->queueFeaturedThumbnailRefresh($feature);
$this->queueCreatorRebuild($feature);
}
public function forceDeleted(ArtworkFeature $feature): void
{
$this->homepage->clearFeaturedAndMedalCaches();
$this->queueCreatorRebuild($feature);
}
/**
* Ensure the featured hero variants (and their DB-persisted existence state)
* are ready before the next homepage guest-cache warm cycle picks this
* artwork up as the hero winner, so the public request never has to check
* the remote disk itself.
*/
private function queueFeaturedThumbnailRefresh(ArtworkFeature $feature): void
{
$artworkId = (int) $feature->artwork_id;
if ($artworkId <= 0) {
return;
}
$artwork = $feature->relationLoaded('artwork')
? $feature->artwork
: Artwork::withTrashed()->find($artworkId);
if (! $artwork instanceof Artwork || empty($artwork->hash) || empty($artwork->file_ext)) {
return;
}
GenerateFeaturedArtworkThumbnailsJob::dispatch($artworkId);
}
private function queueCreatorRebuild(ArtworkFeature $feature): void
{
$artwork = $feature->relationLoaded('artwork')
? $feature->artwork
: Artwork::withTrashed()->find($feature->artwork_id);
if (! $artwork) {
return;
}
$this->journeys->requestRebuild((int) $artwork->user_id);
}
}