2025-06-29 09:37:49 +02:00
|
|
|
<?php
|
|
|
|
|
|
2025-06-29 17:46:06 +02:00
|
|
|
namespace App\Services\Article;
|
2025-06-29 09:37:49 +02:00
|
|
|
|
2026-08-13 23:36:07 +02:00
|
|
|
use App\Actions\FetchRssArticlesAction;
|
|
|
|
|
use App\Actions\FetchWebsiteArticlesAction;
|
2025-06-29 09:37:49 +02:00
|
|
|
use App\Models\Article;
|
2025-07-05 18:26:04 +02:00
|
|
|
use App\Models\Feed;
|
2025-06-29 21:33:18 +02:00
|
|
|
use App\Services\Factories\ArticleParserFactory;
|
2026-03-08 14:18:28 +01:00
|
|
|
use App\Services\Http\HttpFetcher;
|
2025-07-05 18:26:04 +02:00
|
|
|
use App\Services\Log\LogSaver;
|
2025-06-29 09:37:49 +02:00
|
|
|
use Exception;
|
2025-06-29 17:13:18 +02:00
|
|
|
use Illuminate\Support\Collection;
|
2025-06-29 09:37:49 +02:00
|
|
|
|
|
|
|
|
class ArticleFetcher
|
|
|
|
|
{
|
2025-08-15 02:50:42 +02:00
|
|
|
public function __construct(
|
2026-08-13 21:59:56 +02:00
|
|
|
private LogSaver $logSaver,
|
2026-08-13 23:36:07 +02:00
|
|
|
private FetchRssArticlesAction $fetchRssArticles,
|
|
|
|
|
private FetchWebsiteArticlesAction $fetchWebsiteArticles,
|
2025-08-15 02:50:42 +02:00
|
|
|
) {}
|
|
|
|
|
|
2025-07-07 00:51:32 +02:00
|
|
|
/**
|
|
|
|
|
* @return Collection<int, Article>
|
|
|
|
|
*/
|
2025-08-15 02:50:42 +02:00
|
|
|
public function getArticlesFromFeed(Feed $feed): Collection
|
2025-06-29 21:20:45 +02:00
|
|
|
{
|
2025-07-05 18:26:04 +02:00
|
|
|
if ($feed->type === 'rss') {
|
2026-08-13 23:36:07 +02:00
|
|
|
return $this->fetchRssArticles->execute($feed);
|
2025-07-05 18:26:04 +02:00
|
|
|
} elseif ($feed->type === 'website') {
|
2026-08-13 23:36:07 +02:00
|
|
|
return $this->fetchWebsiteArticles->execute($feed);
|
2025-07-05 18:26:04 +02:00
|
|
|
}
|
|
|
|
|
|
2026-03-08 14:18:28 +01:00
|
|
|
$this->logSaver->warning('Unsupported feed type', null, [
|
2025-07-05 18:26:04 +02:00
|
|
|
'feed_id' => $feed->id,
|
2026-03-08 14:18:28 +01:00
|
|
|
'feed_type' => $feed->type,
|
2025-07-05 18:26:04 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return collect();
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 00:51:32 +02:00
|
|
|
/**
|
|
|
|
|
* @return array<string, mixed>
|
|
|
|
|
*/
|
2025-08-15 02:50:42 +02:00
|
|
|
public function fetchArticleData(Article $article): array
|
2025-06-29 09:37:49 +02:00
|
|
|
{
|
|
|
|
|
try {
|
2025-06-29 21:33:18 +02:00
|
|
|
$html = HttpFetcher::fetchHtml($article->url);
|
|
|
|
|
$parser = ArticleParserFactory::getParser($article->url);
|
2025-06-29 09:37:49 +02:00
|
|
|
|
2025-06-29 21:33:18 +02:00
|
|
|
return $parser->extractData($html);
|
2025-06-29 09:37:49 +02:00
|
|
|
} catch (Exception $e) {
|
2025-08-15 02:50:42 +02:00
|
|
|
$this->logSaver->error('Exception while fetching article data', null, [
|
2025-06-29 21:33:18 +02:00
|
|
|
'url' => $article->url,
|
2026-03-08 14:18:28 +01:00
|
|
|
'error' => $e->getMessage(),
|
2025-06-29 21:33:18 +02:00
|
|
|
]);
|
2025-07-03 21:34:39 +02:00
|
|
|
|
2025-06-29 21:33:18 +02:00
|
|
|
return [];
|
2025-06-29 09:37:49 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|