57 lines
1.4 KiB
PHP
57 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Observers;
|
|
|
|
use App\Models\Artwork;
|
|
use App\Services\ArtworkSearchIndexer;
|
|
|
|
/**
|
|
* Syncs artwork documents to Meilisearch on every relevant model event.
|
|
*
|
|
* All operations are dispatched to the queue — no blocking calls.
|
|
*/
|
|
class ArtworkObserver
|
|
{
|
|
public function __construct(
|
|
private readonly ArtworkSearchIndexer $indexer
|
|
) {}
|
|
|
|
/** New artwork created — index once published and approved. */
|
|
public function created(Artwork $artwork): void
|
|
{
|
|
$this->indexer->index($artwork);
|
|
}
|
|
|
|
/** Artwork updated — covers publish, approval, metadata changes. */
|
|
public function updated(Artwork $artwork): void
|
|
{
|
|
// When soft-deleted, remove from index immediately.
|
|
if ($artwork->isDirty('deleted_at') && $artwork->deleted_at !== null) {
|
|
$this->indexer->delete($artwork->id);
|
|
return;
|
|
}
|
|
|
|
$this->indexer->update($artwork);
|
|
}
|
|
|
|
/** Soft delete — remove from search. */
|
|
public function deleted(Artwork $artwork): void
|
|
{
|
|
$this->indexer->delete($artwork->id);
|
|
}
|
|
|
|
/** Force delete — ensure removal from index. */
|
|
public function forceDeleted(Artwork $artwork): void
|
|
{
|
|
$this->indexer->delete($artwork->id);
|
|
}
|
|
|
|
/** Restored from soft-delete — re-index. */
|
|
public function restored(Artwork $artwork): void
|
|
{
|
|
$this->indexer->index($artwork);
|
|
}
|
|
}
|