Completed
Push — issue/232 ( a55812 )
by Alex
01:44
created

Rss::getMainElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FeedIo\Standard;
12
13
use DOMDocument;
14
use FeedIo\Reader\Document;
15
use FeedIo\Rule\Author;
16
use FeedIo\Rule\Description;
17
use FeedIo\Rule\Language;
18
use FeedIo\Rule\Link;
19
use FeedIo\Rule\PublicId;
20
use FeedIo\Rule\Media;
21
use FeedIo\Rule\Category;
22
use FeedIo\RuleSet;
23
24
class Rss extends XmlAbstract
25
{
26
27
    /**
28
     * Format version
29
     */
30
    const VERSION = '2.0';
31
32
    /**
33
     * RSS document must have a <rss> root node
34
     */
35
    const ROOT_NODE_TAGNAME = 'rss';
36
37
    /**
38
     * <channel> node contains feed's metadata
39
     */
40
    const CHANNEL_NODE_TAGNAME = 'channel';
41
42
    /**
43
     * publication date
44
     */
45
    const DATE_NODE_TAGNAME = 'pubDate';
46
47
    protected $mandatoryFields = ['channel'];
48
49
    /**
50
     * Formats the document according to the standard's specification
51
     * @param  \DOMDocument $document
52
     * @return \DOMDocument
53
     */
54 3
    public function format(\DOMDocument $document) : \DOMDocument
55
    {
56 3
        $rss = $document->createElement(static::ROOT_NODE_TAGNAME);
57 3
        $rss->setAttribute('version', static::VERSION);
58
59 3
        $channel = $document->createElement(static::CHANNEL_NODE_TAGNAME);
60 3
        $rss->appendChild($channel);
61 3
        $document->appendChild($rss);
62
63 3
        return $document;
64
    }
65
66
    /**
67
     * Tells if the parser can handle the feed or not
68
     * @param  Document $document
69
     * @return boolean
70
     */
71 7 View Code Duplication
    public function canHandle(Document $document) : bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73 7
        if (!isset($document->getDOMDocument()->documentElement->tagName)) {
74
            return false;
75
        }
76 7
        return static::ROOT_NODE_TAGNAME === $document->getDOMDocument()->documentElement->tagName;
77
    }
78
79
    /**
80
     * @param  DOMDocument $document
81
     * @return \DomElement
82
     */
83 7
    public function getMainElement(\DOMDocument $document) : \DOMElement
84
    {
85 7
        return $document->documentElement->getElementsByTagName(static::CHANNEL_NODE_TAGNAME)->item(0);
86
    }
87
88
    /**
89
     * @return \FeedIo\RuleSet
90
     */
91 7
    public function buildFeedRuleSet() : RuleSet
92
    {
93 7
        $ruleSet = $this->buildBaseRuleSet();
94 7
        $ruleSet->add(new Language());
95
96 7
        return $ruleSet;
97
    }
98
99
    /**
100
     * @return \FeedIo\RuleSet
101
     */
102 10
    public function buildItemRuleSet() : RuleSet
103
    {
104 10
        $ruleSet = $this->buildBaseRuleSet();
105
        $ruleSet
106 10
            ->add(new Author(), ['dc:creator'])
107 10
            ->add(new PublicId())
108 10
            ->add(new Media(), ['media:thumbnail'])
109
            ;
110
111 10
        return $ruleSet;
112
    }
113
114
    /**
115
     * @return \FeedIo\RuleSet
116
     */
117 11
    protected function buildBaseRuleSet() : RuleSet
118
    {
119 11
        $ruleSet = parent::buildBaseRuleSet();
120
        $ruleSet
121 11
            ->add(new Link())
122 11
            ->add(new Description())
123 11
            ->add($this->getModifiedSinceRule(static::DATE_NODE_TAGNAME), ['lastBuildDate', 'lastPubDate'])
124 11
            ->add(new Category());
125
126 11
        return $ruleSet;
127
    }
128
}
129