2025-06-29 21:33:18 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services\Factories;
|
|
|
|
|
|
|
|
|
|
use App\Contracts\HomepageParserInterface;
|
2025-07-05 18:26:04 +02:00
|
|
|
use App\Models\Feed;
|
2025-06-29 21:33:18 +02:00
|
|
|
use App\Services\Parsers\VrtHomepageParserAdapter;
|
2025-06-29 21:39:28 +02:00
|
|
|
use App\Services\Parsers\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
|
|
|
}
|