fedi-feed-router/app/Services/Factories/HomepageParserFactory.php

45 lines
1.1 KiB
PHP

<?php
namespace App\Services\Factories;
use App\Contracts\HomepageParserInterface;
use App\Models\Feed;
use App\Services\Parsers\VrtHomepageParserAdapter;
use App\Services\Parsers\BelgaHomepageParserAdapter;
use Exception;
class HomepageParserFactory
{
/**
* @var array<int, class-string<HomepageParserInterface>>
*/
private static array $parsers = [
VrtHomepageParserAdapter::class,
BelgaHomepageParserAdapter::class,
];
/**
* @throws Exception
*/
public static function getParser(string $url): HomepageParserInterface
{
foreach (self::$parsers as $parserClass) {
$parser = new $parserClass();
if ($parser->canParse($url)) {
return $parser;
}
}
throw new Exception("No homepage parser found for URL: {$url}");
}
public static function getParserForFeed(Feed $feed): ?HomepageParserInterface
{
try {
return self::getParser($feed->url);
} catch (Exception) {
return null;
}
}
}