trypost/app/Observers/PostObserver.php
Paulo Castellano d1d38865ef Dispatch PostCreated from PostObserver on every create.
Centralize provenance/broadcast/PostHog triggers so CreatePost and DuplicatePost no longer fire the event by hand.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-24 12:01:01 -03:00

39 lines
979 B
PHP

<?php
declare(strict_types=1);
namespace App\Observers;
use App\Enums\Automation\Trigger\Type as TriggerType;
use App\Enums\Post\Status as PostStatus;
use App\Events\PostCreated;
use App\Jobs\Automation\DispatchPostTriggerAutomationsJob;
use App\Models\Post;
use Illuminate\Contracts\Events\ShouldHandleEventsAfterCommit;
class PostObserver implements ShouldHandleEventsAfterCommit
{
public function created(Post $post): void
{
PostCreated::dispatch($post);
}
public function saved(Post $post): void
{
if (! $post->wasChanged('status')) {
return;
}
$triggerType = match ($post->status) {
PostStatus::Published => TriggerType::PostPublished,
PostStatus::Scheduled => TriggerType::PostScheduled,
default => null,
};
if ($triggerType === null) {
return;
}
DispatchPostTriggerAutomationsJob::dispatch($post, $triggerType);
}
}