Centralize provenance/broadcast/PostHog triggers so CreatePost and DuplicatePost no longer fire the event by hand. Co-authored-by: Cursor <cursoragent@cursor.com>
39 lines
979 B
PHP
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);
|
|
}
|
|
}
|