fedi-feed-router/app/Services/Article/ValidationService.php

76 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2025-06-29 19:46:50 +02:00
<?php
namespace App\Services\Article;
use App\Models\Article;
class ValidationService
{
2025-08-15 02:50:42 +02:00
public function __construct(
private ArticleFetcher $articleFetcher
) {}
public function validate(Article $article): Article
2025-06-29 19:46:50 +02:00
{
logger('Checking keywords for article: ' . $article->id);
2025-08-15 02:50:42 +02:00
$articleData = $this->articleFetcher->fetchArticleData($article);
2025-06-30 21:28:15 +02:00
2025-08-09 21:32:46 +02:00
// Update article with fetched metadata (title, description)
2025-08-10 21:18:20 +02:00
$updateData = [];
2025-08-09 18:34:19 +02:00
if (!empty($articleData)) {
2025-08-10 21:18:20 +02:00
$updateData['title'] = $articleData['title'] ?? $article->title;
$updateData['description'] = $articleData['description'] ?? $article->description;
$updateData['content'] = $articleData['full_article'] ?? null;
2025-08-09 18:34:19 +02:00
}
if (!isset($articleData['full_article']) || empty($articleData['full_article'])) {
2025-08-09 21:32:46 +02:00
logger()->warning('Article data missing full_article content', [
2025-06-30 21:28:15 +02:00
'article_id' => $article->id,
'url' => $article->url
]);
2025-08-10 21:18:20 +02:00
$updateData['approval_status'] = 'rejected';
2025-08-09 18:34:19 +02:00
$article->update($updateData);
2025-06-30 21:28:15 +02:00
return $article->refresh();
}
2025-08-09 21:32:46 +02:00
// Validate using extracted content (not stored)
2025-08-15 02:50:42 +02:00
$validationResult = $this->validateByKeywords($articleData['full_article']);
2025-08-10 21:18:20 +02:00
$updateData['approval_status'] = $validationResult ? 'approved' : 'pending';
2025-06-29 21:33:18 +02:00
2025-08-09 18:34:19 +02:00
$article->update($updateData);
2025-06-29 19:46:50 +02:00
return $article->refresh();
}
2025-06-29 21:33:18 +02:00
2025-08-15 02:50:42 +02:00
private function validateByKeywords(string $full_article): bool
2025-06-29 21:33:18 +02:00
{
2025-08-09 21:32:46 +02:00
// Belgian news content keywords - broader set for Belgian news relevance
2025-06-29 21:33:18 +02:00
$keywords = [
2025-08-09 21:32:46 +02:00
// Political parties and leaders
'N-VA', 'Bart De Wever', 'Frank Vandenbroucke', 'Alexander De Croo',
'Vooruit', 'Open Vld', 'CD&V', 'Vlaams Belang', 'PTB', 'PVDA',
// Belgian locations and institutions
'Belgium', 'Belgian', 'Flanders', 'Flemish', 'Wallonia', 'Brussels',
'Antwerp', 'Ghent', 'Bruges', 'Leuven', 'Mechelen', 'Namur', 'Liège', 'Charleroi',
'parliament', 'government', 'minister', 'policy', 'law', 'legislation',
// Common Belgian news topics
'economy', 'economic', 'education', 'healthcare', 'transport', 'climate', 'energy',
'European', 'EU', 'migration', 'security', 'justice', 'culture', 'police'
2025-06-29 21:33:18 +02:00
];
foreach ($keywords as $keyword) {
if (stripos($full_article, $keyword) !== false) {
return true;
}
}
return false;
}
2025-06-29 19:46:50 +02:00
}