Files
citrus/app/Observers/BlogObserver.php
T

54 lines
1.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Observers;
use App\Models\Blog;
use App\Models\Setting;
use App\Services\LinkedInService;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
class BlogObserver
{
/**
* Handle the Blog "saved" event.
*/
public function saved(Blog $blog): void
{
// Only trigger if blog status is 'published'
if ($blog->status !== 'published') {
return;
}
// Check if auto-publish setting is enabled
if (!Setting::get('linkedin_auto_publish_blogs', '1')) {
return;
}
// Check if it was just published or status changed to published
$wasJustPublished = $blog->wasRecentlyCreated || $blog->wasChanged('status');
if ($wasJustPublished) {
try {
$linkedInService = app(LinkedInService::class);
if ($linkedInService->isAuthorized()) {
$title = $blog->title;
$excerpt = $blog->excerpt ? strip_tags($blog->excerpt) : Str::limit(strip_tags($blog->content), 200);
$url = url('/blog/' . $blog->slug);
$result = $linkedInService->sharePost($title, $excerpt, $url);
if ($result['success']) {
Log::info("Blog [#{$blog->id}] LinkedIn'de otomatik paylaşıldı.", ['post_id' => $result['post_id'] ?? null]);
} else {
Log::warning("Blog [#{$blog->id}] LinkedIn paylaşımı başarısız: " . $result['message']);
}
}
} catch (\Throwable $e) {
Log::error("BlogObserver LinkedIn auto-post error: " . $e->getMessage());
}
}
}
}