first commit
This commit is contained in:
139
src/Services/PublisherService.php
Normal file
139
src/Services/PublisherService.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Site;
|
||||
use App\Models\Topic;
|
||||
use App\Models\Article;
|
||||
use App\Helpers\Logger;
|
||||
|
||||
class PublisherService
|
||||
{
|
||||
private TopicBalancer $topicBalancer;
|
||||
private OpenAIService $openAI;
|
||||
private ImageService $imageService;
|
||||
private WordPressService $wordpress;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->topicBalancer = new TopicBalancer();
|
||||
$this->openAI = new OpenAIService();
|
||||
$this->imageService = new ImageService();
|
||||
$this->wordpress = new WordPressService();
|
||||
}
|
||||
|
||||
public function publishNext(): array
|
||||
{
|
||||
Logger::info('Rozpoczynam automatyczną publikację', 'publish');
|
||||
|
||||
$sites = Site::findDueForPublishing();
|
||||
|
||||
if (empty($sites)) {
|
||||
Logger::info('Brak stron do publikacji', 'publish');
|
||||
return ['success' => false, 'message' => 'Brak stron wymagających publikacji.'];
|
||||
}
|
||||
|
||||
$site = $sites[0];
|
||||
return $this->publishForSite($site);
|
||||
}
|
||||
|
||||
public function publishForSite(array $site): array
|
||||
{
|
||||
Logger::info("Publikacja dla strony: {$site['name']} (ID: {$site['id']})", 'publish');
|
||||
|
||||
// 1. Select topic
|
||||
$topic = $this->topicBalancer->getNextTopic($site['id']);
|
||||
if (!$topic) {
|
||||
Logger::error("Brak aktywnych tematów dla strony {$site['name']}", 'publish');
|
||||
return ['success' => false, 'message' => "Brak aktywnych tematów dla strony {$site['name']}."];
|
||||
}
|
||||
|
||||
Logger::info("Wybrany temat: {$topic['name']} (ID: {$topic['id']})", 'publish');
|
||||
|
||||
// 2. Get existing titles to avoid repetition
|
||||
$existingTitles = Article::getRecentTitlesByTopic($topic['id'], 20);
|
||||
|
||||
// 3. Generate article
|
||||
$article = $this->openAI->generateArticle(
|
||||
$topic['name'],
|
||||
$topic['description'] ?? '',
|
||||
$existingTitles
|
||||
);
|
||||
|
||||
if (!$article) {
|
||||
$this->saveFailedArticle($site, $topic, 'Nie udało się wygenerować artykułu przez OpenAI.');
|
||||
return ['success' => false, 'message' => 'Błąd generowania artykułu przez AI.'];
|
||||
}
|
||||
|
||||
Logger::info("Wygenerowano artykuł: {$article['title']}", 'publish');
|
||||
|
||||
// 4. Generate/fetch image
|
||||
$imageUrl = null;
|
||||
$mediaId = null;
|
||||
$image = $this->imageService->generate($article['title'], $topic['name']);
|
||||
|
||||
if ($image) {
|
||||
$mediaId = $this->wordpress->uploadMedia($site, $image['data'], $image['filename']);
|
||||
if ($mediaId) {
|
||||
Logger::info("Upload obrazka: media_id={$mediaId}", 'publish');
|
||||
}
|
||||
} else {
|
||||
Logger::warning('Nie udało się wygenerować obrazka, publikacja bez obrazka', 'publish');
|
||||
}
|
||||
|
||||
// 5. Publish to WordPress
|
||||
$wpPostId = $this->wordpress->createPost(
|
||||
$site,
|
||||
$article['title'],
|
||||
$article['content'],
|
||||
$topic['wp_category_id'],
|
||||
$mediaId
|
||||
);
|
||||
|
||||
if (!$wpPostId) {
|
||||
$this->saveFailedArticle($site, $topic, 'Nie udało się opublikować posta na WordPress.', $article);
|
||||
return ['success' => false, 'message' => 'Błąd publikacji na WordPress.'];
|
||||
}
|
||||
|
||||
Logger::info("Opublikowano post: wp_post_id={$wpPostId}", 'publish');
|
||||
|
||||
// 6. Save article in database
|
||||
Article::create([
|
||||
'site_id' => $site['id'],
|
||||
'topic_id' => $topic['id'],
|
||||
'title' => $article['title'],
|
||||
'content' => $article['content'],
|
||||
'wp_post_id' => $wpPostId,
|
||||
'image_url' => $imageUrl,
|
||||
'status' => 'published',
|
||||
'ai_model' => $article['model'],
|
||||
'prompt_used' => $article['prompt'],
|
||||
'published_at' => date('Y-m-d H:i:s'),
|
||||
]);
|
||||
|
||||
// 7. Update counters
|
||||
Topic::incrementArticleCount($topic['id']);
|
||||
Site::updateLastPublished($site['id']);
|
||||
|
||||
$message = "Opublikowano: \"{$article['title']}\" na {$site['name']}";
|
||||
Logger::info($message, 'publish');
|
||||
|
||||
return ['success' => true, 'message' => $message];
|
||||
}
|
||||
|
||||
private function saveFailedArticle(array $site, array $topic, string $error, ?array $article = null): void
|
||||
{
|
||||
Article::create([
|
||||
'site_id' => $site['id'],
|
||||
'topic_id' => $topic['id'],
|
||||
'title' => $article['title'] ?? 'FAILED - nie wygenerowano',
|
||||
'content' => $article['content'] ?? '',
|
||||
'status' => 'failed',
|
||||
'ai_model' => $article['model'] ?? null,
|
||||
'prompt_used' => $article['prompt'] ?? null,
|
||||
'error_message' => $error,
|
||||
]);
|
||||
|
||||
Logger::error("Publikacja nieudana: {$error}", 'publish');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user