-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeedGenerator.php
More file actions
100 lines (79 loc) · 3.32 KB
/
Copy pathFeedGenerator.php
File metadata and controls
100 lines (79 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
declare(strict_types=1);
namespace Light\App\Service;
use DateTimeImmutable;
use DateTimeInterface;
use DOMDocument;
use DOMElement;
use Light\Blog\Repository\PostRepository;
use RuntimeException;
use function count;
class FeedGenerator
{
public const CONTENT_TYPE = 'application/rss+xml; charset=UTF-8';
private const MEDIA_NAMESPACE = 'http://search.yahoo.com/mrss/';
public function __construct(
private readonly PostRepository $postRepository,
private readonly string $feedFile,
private readonly string $baseUrl,
private readonly string $title,
private readonly string $description,
private readonly string $image,
) {
}
public function getFeedFile(): string
{
return $this->feedFile;
}
public function write(): int
{
$posts = $this->postRepository->getPublishedPosts();
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$rss = $dom->createElement('rss');
$rss->setAttribute('version', '2.0');
$rss->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:media', self::MEDIA_NAMESPACE);
$dom->appendChild($rss);
$channel = $dom->createElement('channel');
$rss->appendChild($channel);
$this->appendText($dom, $channel, 'title', $this->title);
$this->appendText($dom, $channel, 'link', $this->baseUrl);
$this->appendText($dom, $channel, 'description', $this->description);
$this->appendText($dom, $channel, 'lastBuildDate', (new DateTimeImmutable())->format(DateTimeInterface::RSS));
foreach ($posts as $post) {
$link = $this->baseUrl . '/' . $post->getCategory()->getSlug() . '/' . $post->getSlug() . '/';
$item = $dom->createElement('item');
$channel->appendChild($item);
$this->appendText($dom, $item, 'title', $post->getTitle());
$this->appendText($dom, $item, 'link', $link);
$this->appendText($dom, $item, 'description', $post->getTlDr() ?? $post->getExcerpt());
$this->appendText($dom, $item, 'pubDate', $post->getPostDate()->format(DateTimeInterface::RSS));
$this->appendText(
$dom,
$item,
'updatedAt',
$post->getUpdatedFormatted(DateTimeInterface::RSS) ??
$post->getPostDate()->format(DateTimeInterface::RSS)
);
$this->appendText($dom, $item, 'guid', $link);
$image = $post->getOpenGraphImage();
$image = $image !== null && $image !== '' ? $this->baseUrl . $image : $this->image;
if ($image !== '') {
$media = $dom->createElementNS(self::MEDIA_NAMESPACE, 'media:content');
$media->setAttribute('url', $image);
$media->setAttribute('medium', 'image');
$item->appendChild($media);
}
}
if ($dom->save($this->feedFile) === false) {
throw new RuntimeException('Unable to write RSS feed.');
}
return count($posts);
}
private function appendText(DOMDocument $dom, DOMElement $parent, string $name, string $text): void
{
$el = $dom->createElement($name);
$el->appendChild($dom->createTextNode($text));
$parent->appendChild($el);
}
}