<?php
 namespace App\Observers; use App\Models\Post; class PostObserver { public function saved(Post $post): void { $searchRelevant = ['title', 'slug', 'content', 'excerpt', 'status', 'published_at', 'is_featured', 'subtype']; if ($post->wasRecentlyCreated || $post->wasChanged($searchRelevant)) { $this->reindex($post); } } public function deleted(Post $post): void { $this->indexerForType($post->type)?->delete($post->id); } public function reindex(Post $post): void { $this->indexerForType($post->type)?->reindex($post); } private function indexerForType(string $type): mixed { $class = match ($type) { 'announcement' => 'App\Services\Search\AnnouncementSearchIndexer', 'kb_article' => 'App\Services\Search\KnowledgebaseSearchIndexer', 'blog_post' => 'App\Services\Search\BlogSearchIndexer', default => null, }; if ($class === null) { return null; } try { return class_exists($class) ? app($class) : null; } catch (\Throwable) { return null; } } } 