map(fn (string $url) => self::saveArticle($url)); } private static function fetchArticles(): Collection { try { $response = Http::get('https://www.vrt.be/vrtnws/en/'); $html = $response->body(); // Extract article links using regex preg_match_all('/href="(\/vrtnws\/en\/\d{4}\/\d{2}\/\d{2}\/[^"]+)"/', $html, $matches); $urls = collect($matches[1] ?? []) ->unique() ->map(fn ($path) => 'https://www.vrt.be' . $path) ->toArray(); $responses = Http::pool(function ($pool) use ($urls) { foreach ($urls as $url) { $pool->get($url); } }); return collect($responses) ->map(function ($response, $index) use ($urls) { $url = $urls[$index]; try { if ($response->successful()) { return $url; } else { return null; } } catch (Exception) { return null; } }) ->filter(fn($article) => !empty($article)); } catch (Exception $e) { logger('article_fetcher')->error("Failed to fetch VRT homepage", ['error' => $e->getMessage()]); return new Collection([]); } } protected static function saveArticle(string $url): Article { return Article::firstOrCreate(['url' => $url]); } }