Skip to content

Commit 1423678

Browse files
committed
Issue 23 dotkernel.com - Add updated date and image to RSS feed
1 parent 69a3db2 commit 1423678

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

src/App/src/Factory/FeedGeneratorFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function __invoke(ContainerInterface $container): FeedGenerator
2626
rtrim($config['application']['url'] ?? '', '/') . '/',
2727
$config['app']['meta']['title'] ?? '',
2828
$config['app']['meta']['description'] ?? '',
29+
$config['app']['meta']['image'] ?? '',
2930
);
3031
}
3132
}

src/App/src/Service/FeedGenerator.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Light\App\Service;
66

7+
use DateTimeImmutable;
78
use DateTimeInterface;
89
use DOMDocument;
910
use DOMElement;
@@ -17,12 +18,15 @@ class FeedGenerator
1718
{
1819
public const CONTENT_TYPE = 'application/rss+xml; charset=UTF-8';
1920

21+
private const MEDIA_NAMESPACE = 'http://search.yahoo.com/mrss/';
22+
2023
public function __construct(
2124
private readonly PostRepository $postRepository,
2225
private readonly string $feedFile,
2326
private readonly string $baseUrl,
2427
private readonly string $title,
2528
private readonly string $description,
29+
private readonly string $image,
2630
) {
2731
}
2832

@@ -43,6 +47,7 @@ public function write(): int
4347

4448
$rss = $dom->createElement('rss');
4549
$rss->setAttribute('version', '2.0');
50+
$rss->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:media', self::MEDIA_NAMESPACE);
4651
$dom->appendChild($rss);
4752

4853
$channel = $dom->createElement('channel');
@@ -51,6 +56,7 @@ public function write(): int
5156
$this->appendText($dom, $channel, 'title', $this->title);
5257
$this->appendText($dom, $channel, 'link', $this->baseUrl);
5358
$this->appendText($dom, $channel, 'description', $this->description);
59+
$this->appendText($dom, $channel, 'lastBuildDate', (new DateTimeImmutable())->format(DateTimeInterface::RSS));
5460

5561
foreach ($posts as $post) {
5662
$link = $this->baseUrl . $post->getCategory()->getSlug() . '/' . $post->getSlug() . '/';
@@ -62,7 +68,15 @@ public function write(): int
6268
$this->appendText($dom, $item, 'link', $link);
6369
$this->appendText($dom, $item, 'description', $post->getTldr() ?? $post->getExcerpt());
6470
$this->appendText($dom, $item, 'pubDate', $post->getPostDate()->format(DateTimeInterface::RSS));
71+
$this->appendText($dom, $item, 'updatedAt', $post->getUpdatedFormatted(DateTimeInterface::RSS) ?? $post->getPostDate()->format(DateTimeInterface::RSS));
6572
$this->appendText($dom, $item, 'guid', $link);
73+
74+
if ($this->image !== '') {
75+
$media = $dom->createElementNS(self::MEDIA_NAMESPACE, 'media:content');
76+
$media->setAttribute('url', $this->image);
77+
$media->setAttribute('medium', 'image');
78+
$item->appendChild($media);
79+
}
6680
}
6781

6882
if ($dom->save($this->feedFile) === false) {

0 commit comments

Comments
 (0)