Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 8 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| HomepageParserFactory | |
0.00% |
0 / 8 |
|
0.00% |
0 / 2 |
30 | |
0.00% |
0 / 1 |
| getParser | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| getParserForFeed | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Services\Factories; |
| 4 | |
| 5 | use App\Contracts\HomepageParserInterface; |
| 6 | use App\Models\Feed; |
| 7 | use App\Services\Parsers\VrtHomepageParserAdapter; |
| 8 | use App\Services\Parsers\BelgaHomepageParserAdapter; |
| 9 | use Exception; |
| 10 | |
| 11 | class HomepageParserFactory |
| 12 | { |
| 13 | /** |
| 14 | * @var array<int, class-string<HomepageParserInterface>> |
| 15 | */ |
| 16 | private static array $parsers = [ |
| 17 | VrtHomepageParserAdapter::class, |
| 18 | BelgaHomepageParserAdapter::class, |
| 19 | ]; |
| 20 | |
| 21 | /** |
| 22 | * @throws Exception |
| 23 | */ |
| 24 | public static function getParser(string $url): HomepageParserInterface |
| 25 | { |
| 26 | foreach (self::$parsers as $parserClass) { |
| 27 | $parser = new $parserClass(); |
| 28 | |
| 29 | if ($parser->canParse($url)) { |
| 30 | return $parser; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | throw new Exception("No homepage parser found for URL: {$url}"); |
| 35 | } |
| 36 | |
| 37 | public static function getParserForFeed(Feed $feed): ?HomepageParserInterface |
| 38 | { |
| 39 | try { |
| 40 | return self::getParser($feed->url); |
| 41 | } catch (Exception) { |
| 42 | return null; |
| 43 | } |
| 44 | } |
| 45 | } |