44
55namespace Light \App \Service ;
66
7+ use DateTimeImmutable ;
78use DateTimeInterface ;
89use DOMDocument ;
910use 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