|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of Packagist. |
| 5 | + * |
| 6 | + * (c) Jordi Boggiano <j.boggiano@seld.be> |
| 7 | + * Nils Adermann <naderman@naderman.de> |
| 8 | + * |
| 9 | + * For the full copyright and license information, please view the LICENSE |
| 10 | + * file that was distributed with this source code. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace App\Service; |
| 14 | + |
| 15 | +use Composer\Pcre\Preg; |
| 16 | +use Psr\Log\LoggerInterface; |
| 17 | +use Symfony\Component\HtmlSanitizer\HtmlSanitizer; |
| 18 | +use Symfony\Component\HtmlSanitizer\HtmlSanitizerAction; |
| 19 | +use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig; |
| 20 | +use Symfony\Contracts\Cache\CacheInterface; |
| 21 | +use Symfony\Contracts\Cache\ItemInterface; |
| 22 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 23 | + |
| 24 | +class BlogRssFetcher |
| 25 | +{ |
| 26 | + private const string RSS_URL = 'https://blog.packagist.com/rss/'; |
| 27 | + private const int CACHE_TTL = 3600; |
| 28 | + private const int MAX_ITEMS = 5; |
| 29 | + |
| 30 | + public function __construct( |
| 31 | + private readonly HttpClientInterface $httpClient, |
| 32 | + private readonly CacheInterface $cache, |
| 33 | + private readonly LoggerInterface $logger, |
| 34 | + ) { |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @return list<array{title: string, link: string, description: string, datePublished: \DateTimeInterface|null}> |
| 39 | + */ |
| 40 | + public function getNewsItems(): array |
| 41 | + { |
| 42 | + try { |
| 43 | + return $this->cache->get('blog_news_items', function (ItemInterface $item): array { |
| 44 | + $item->expiresAfter(self::CACHE_TTL); |
| 45 | + |
| 46 | + return $this->fetchAndParseRss(); |
| 47 | + }); |
| 48 | + } catch (\Exception $e) { |
| 49 | + $this->logger->error('Failed to fetch blog RSS feed', [ |
| 50 | + 'error' => $e->getMessage(), |
| 51 | + ]); |
| 52 | + return []; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @return list<array{title: string, link: string, description: string, datePublished: \DateTimeInterface|null}> |
| 58 | + */ |
| 59 | + private function fetchAndParseRss(): array |
| 60 | + { |
| 61 | + $response = $this->httpClient->request('GET', self::RSS_URL); |
| 62 | + $content = $response->getContent(); |
| 63 | + |
| 64 | + $xml = new \SimpleXMLElement($content); |
| 65 | + $items = []; |
| 66 | + |
| 67 | + foreach ($xml->channel->item as $entry) { |
| 68 | + // Extract categories |
| 69 | + $categories = []; |
| 70 | + foreach ($entry->category as $category) { |
| 71 | + $categories[] = strtolower((string) $category); |
| 72 | + } |
| 73 | + |
| 74 | + // Only include items with composer or packagist.org category |
| 75 | + if (!in_array('composer', $categories, true) && !in_array('packagist.org', $categories, true)) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + $pubDate = null; |
| 80 | + if (isset($entry->pubDate)) { |
| 81 | + try { |
| 82 | + $pubDate = new \DateTimeImmutable((string) $entry->pubDate); |
| 83 | + } catch (\Exception) { |
| 84 | + // Ignore invalid dates |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + $desc = $entry->description; |
| 89 | + $desc = str_replace('><', '> <', (string) $desc); |
| 90 | + $desc = trim(html_entity_decode(strip_tags($desc), ENT_QUOTES | ENT_HTML5, 'UTF-8')); |
| 91 | + |
| 92 | + $items[] = [ |
| 93 | + 'title' => (string) $entry->title, |
| 94 | + 'link' => (string) $entry->link, |
| 95 | + 'description' => $desc, |
| 96 | + 'datePublished' => $pubDate, |
| 97 | + ]; |
| 98 | + |
| 99 | + if (count($items) >= self::MAX_ITEMS) { |
| 100 | + break; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return $items; |
| 105 | + } |
| 106 | +} |
0 commit comments