54 lines
1.7 KiB
PHP
54 lines
1.7 KiB
PHP
<?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());
|
||
}
|
||
}
|
||
}
|
||
}
|