Passed
Push — master ( 274e82...4dfe60 )
by Caen
03:54 queued 14s
created

RssFeedGenerator::getImageLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/** @noinspection PhpComposerExtensionStubsInspection */
4
/** @noinspection XmlUnusedNamespaceDeclaration */
5
6
declare(strict_types=1);
7
8
namespace Hyde\Framework\Features\XmlGenerators;
9
10
use Hyde\Hyde;
11
use SimpleXMLElement;
12
use Hyde\Facades\Site;
13
use Hyde\Facades\Config;
14
use Hyde\Pages\MarkdownPost;
15
use Hyde\Support\Filesystem\MediaFile;
16
use Hyde\Framework\Features\Blogging\Models\FeaturedImage;
17
18
use function date;
19
20
/**
21
 * @see https://validator.w3.org/feed/docs/rss2.html
22
 */
23
class RssFeedGenerator extends BaseXmlGenerator
24
{
25
    public function generate(): static
26
    {
27
        MarkdownPost::getLatestPosts()->each(function (MarkdownPost $post): void {
28
            $this->addItem($post);
29
        });
30
31
        return $this;
32
    }
33
34
    protected function constructBaseElement(): void
35
    {
36
        $this->xmlElement = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?>
37
            <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" />');
38
        $this->xmlElement->addChild('channel');
39
40
        $this->addBaseChannelItems();
41
        $this->addAtomLinkItem();
42
    }
43
44
    protected function addItem(MarkdownPost $post): void
45
    {
46
        $item = $this->getChannel()->addChild('item');
47
        $this->addChild($item, 'title', $post->title);
48
        $this->addChild($item, 'description', $post->description);
0 ignored issues
show
Bug introduced by
It seems like $post->description can also be of type null; however, parameter $value of Hyde\Framework\Features\...mlGenerator::addChild() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
        $this->addChild($item, 'description', /** @scrutinizer ignore-type */ $post->description);
Loading history...
49
50
        $this->addDynamicItemData($item, $post);
51
    }
52
53
    protected function addDynamicItemData(SimpleXMLElement $item, MarkdownPost $post): void
54
    {
55
        if ($post->getCanonicalUrl() !== null) {
56
            $this->addChild($item, 'link', $post->getCanonicalUrl());
57
            $this->addChild($item, 'guid', $post->getCanonicalUrl());
58
        }
59
60
        if (isset($post->date)) {
61
            $this->addChild($item, 'pubDate', $post->date->dateTimeObject->format(DATE_RSS));
62
        }
63
64
        if (isset($post->author)) {
65
            $item->addChild('dc:creator', $post->author->getName(), 'http://purl.org/dc/elements/1.1/');
0 ignored issues
show
Bug introduced by
The method getName() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
            $item->addChild('dc:creator', $post->author->/** @scrutinizer ignore-call */ getName(), 'http://purl.org/dc/elements/1.1/');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
        }
67
68
        if (isset($post->category)) {
69
            $this->addChild($item, 'category', $post->category);
0 ignored issues
show
Bug introduced by
It seems like $post->category can also be of type null; however, parameter $value of Hyde\Framework\Features\...mlGenerator::addChild() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
            $this->addChild($item, 'category', /** @scrutinizer ignore-type */ $post->category);
Loading history...
70
        }
71
72
        if (isset($post->image)) {
73
            $image = $item->addChild('enclosure');
74
            $image->addAttribute('url', Hyde::url($post->image->getSource()));
0 ignored issues
show
Bug introduced by
The method getSource() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
            $image->addAttribute('url', Hyde::url($post->image->/** @scrutinizer ignore-call */ getSource()));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
            $image->addAttribute('type', $this->getImageType($post->image));
0 ignored issues
show
Bug introduced by
It seems like $post->image can also be of type null; however, parameter $image of Hyde\Framework\Features\...nerator::getImageType() does only seem to accept Hyde\Framework\Features\...ng\Models\FeaturedImage, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
            $image->addAttribute('type', $this->getImageType(/** @scrutinizer ignore-type */ $post->image));
Loading history...
76
            $image->addAttribute('length', $this->getImageLength($post->image));
0 ignored issues
show
Bug introduced by
It seems like $post->image can also be of type null; however, parameter $image of Hyde\Framework\Features\...rator::getImageLength() does only seem to accept Hyde\Framework\Features\...ng\Models\FeaturedImage, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
            $image->addAttribute('length', $this->getImageLength(/** @scrutinizer ignore-type */ $post->image));
Loading history...
77
        }
78
    }
79
80
    protected function addBaseChannelItems(): void
81
    {
82
        $channel = $this->getChannel();
83
84
        $this->addChild($channel, 'title', Site::name());
0 ignored issues
show
Bug introduced by
It seems like Hyde\Facades\Site::name() can also be of type null; however, parameter $value of Hyde\Framework\Features\...mlGenerator::addChild() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
        $this->addChild($channel, 'title', /** @scrutinizer ignore-type */ Site::name());
Loading history...
85
        $this->addChild($channel, 'link', Site::url());
86
        $this->addChild($channel, 'description', $this->getDescription());
87
        $this->addChild($channel, 'language', Config::getString('hyde.language', 'en'));
88
        $this->addChild($channel, 'generator', 'HydePHP '.Hyde::version());
89
        $this->addChild($channel, 'lastBuildDate', date(DATE_RSS));
90
    }
91
92
    protected function addAtomLinkItem(): void
93
    {
94
        $atomLink = $this->getChannel()->addChild('atom:link', namespace: 'http://www.w3.org/2005/Atom');
95
        $atomLink->addAttribute('href', $this->escape(Hyde::url($this->getFilename())));
96
        $atomLink->addAttribute('rel', 'self');
97
        $atomLink->addAttribute('type', 'application/rss+xml');
98
    }
99
100
    protected function getImageType(FeaturedImage $image): string
101
    {
102
        return (new MediaFile($image->getSource()))->getMimeType();
103
    }
104
105
    /** @return numeric-string */
0 ignored issues
show
Documentation Bug introduced by
The doc comment numeric-string at position 0 could not be parsed: Unknown type name 'numeric-string' at position 0 in numeric-string.
Loading history...
106
    protected function getImageLength(FeaturedImage $image): string
107
    {
108
        return (string) $image->getContentLength();
109
    }
110
111
    public static function getFilename(): string
112
    {
113
        return Config::getString('hyde.rss.filename', 'feed.xml');
114
    }
115
116
    public static function getDescription(): string
117
    {
118
        return Config::getString('hyde.rss.description', Site::name().' RSS Feed');
119
    }
120
121
    protected function getChannel(): SimpleXMLElement
122
    {
123
        return $this->xmlElement->channel;
124
    }
125
}
126