fedi-feed-router/backend/src/Domains/Article/Parsers/Factories/HomepageParserFactory.php

56 lines
1.4 KiB
PHP
Raw Normal View History

2025-06-29 21:33:18 +02:00
<?php
2025-08-15 16:39:18 +02:00
namespace Domains\Article\Parsers\Factories;
2025-06-29 21:33:18 +02:00
2025-08-15 16:39:18 +02:00
use Domains\Article\Contracts\HomepageParserInterface;
use Domains\Feed\Models\Feed;
use Domains\Article\Parsers\Vrt\VrtHomepageParserAdapter;
use Domains\Article\Parsers\Belga\BelgaHomepageParserAdapter;
2025-06-29 21:33:18 +02:00
use Exception;
class HomepageParserFactory
{
2025-07-07 00:51:32 +02:00
/**
* @var array<int, class-string<HomepageParserInterface>>
*/
2025-06-29 21:33:18 +02:00
private static array $parsers = [
VrtHomepageParserAdapter::class,
2025-06-29 21:39:28 +02:00
BelgaHomepageParserAdapter::class,
2025-06-29 21:33:18 +02:00
];
2025-07-05 18:26:04 +02:00
/**
* @throws Exception
*/
2025-06-29 21:33:18 +02:00
public static function getParser(string $url): HomepageParserInterface
{
foreach (self::$parsers as $parserClass) {
$parser = new $parserClass();
2025-07-05 18:26:04 +02:00
2025-06-29 21:33:18 +02:00
if ($parser->canParse($url)) {
return $parser;
}
}
throw new Exception("No homepage parser found for URL: {$url}");
}
2025-07-05 18:26:04 +02:00
public static function getParserForFeed(Feed $feed): ?HomepageParserInterface
2025-06-29 21:33:18 +02:00
{
2025-08-10 21:47:10 +02:00
if (!$feed->provider) {
2025-07-05 18:26:04 +02:00
return null;
2025-06-29 21:33:18 +02:00
}
2025-08-10 21:47:10 +02:00
$providerConfig = config("feed.providers.{$feed->provider}");
if (!$providerConfig || !isset($providerConfig['parsers']['homepage'])) {
return null;
}
$parserClass = $providerConfig['parsers']['homepage'];
if (!class_exists($parserClass)) {
return null;
}
return new $parserClass();
2025-06-29 21:33:18 +02:00
}
2025-07-05 18:26:04 +02:00
}