Completed
Pull Request — master (#230)
by
unknown
02:43 queued 01:26
created

Rss   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 75.86%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 11
dl 0
loc 103
ccs 22
cts 29
cp 0.7586
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 11 1
A canHandle() 0 4 1
A getMainElement() 0 4 1
A buildFeedRuleSet() 0 7 1
A buildItemRuleSet() 0 11 1
A buildBaseRuleSet() 0 12 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\Rule\Image;
23
use FeedIo\RuleSet;
24
25
class Rss extends XmlAbstract
26
{
27
28
    /**
29
     * Format version
30
     */
31
    const VERSION = '2.0';
32
33
    /**
34
     * RSS document must have a <rss> root node
35
     */
36
    const ROOT_NODE_TAGNAME = 'rss';
37
38
    /**
39
     * <channel> node contains feed's metadata
40
     */
41
    const CHANNEL_NODE_TAGNAME = 'channel';
42
43
    /**
44
     * publication date
45
     */
46
    const DATE_NODE_TAGNAME = 'pubDate';
47
48
    protected $mandatoryFields = ['channel'];
49
50
    /**
51
     * Formats the document according to the standard's specification
52
     * @param  \DOMDocument $document
53
     * @return \DOMDocument
54
     */
55
    public function format(\DOMDocument $document) : \DOMDocument
56
    {
57
        $rss = $document->createElement(static::ROOT_NODE_TAGNAME);
58
        $rss->setAttribute('version', static::VERSION);
59
60
        $channel = $document->createElement(static::CHANNEL_NODE_TAGNAME);
61
        $rss->appendChild($channel);
62
        $document->appendChild($rss);
63
64
        return $document;
65
    }
66
67
    /**
68
     * Tells if the parser can handle the feed or not
69
     * @param  Document $document
70
     * @return boolean
71
     */
72 7
    public function canHandle(Document $document) : bool
73
    {
74 7
        return static::ROOT_NODE_TAGNAME === $document->getDOMDocument()->documentElement->tagName;
75
    }
76
77
    /**
78
     * @param  DOMDocument $document
79
     * @return \DomElement
80
     */
81 6
    public function getMainElement(\DOMDocument $document) : \DOMElement
82
    {
83 6
        return $document->documentElement->getElementsByTagName(static::CHANNEL_NODE_TAGNAME)->item(0);
84
    }
85
86
    /**
87
     * @return \FeedIo\RuleSet
88
     */
89 6
    public function buildFeedRuleSet() : RuleSet
90
    {
91 6
        $ruleSet = $this->buildBaseRuleSet();
92 6
        $ruleSet->add(new Language());
93
94 6
        return $ruleSet;
95
    }
96
97
    /**
98
     * @return \FeedIo\RuleSet
99
     */
100 9
    public function buildItemRuleSet() : RuleSet
101
    {
102 9
        $ruleSet = $this->buildBaseRuleSet();
103
        $ruleSet
104 9
            ->add(new Author(), ['dc:creator'])
105 9
            ->add(new PublicId())
106 9
            ->add(new Media(), ['media:thumbnail'])
107
            ;
108
109 9
        return $ruleSet;
110
    }
111
112
    /**
113
     * @return \FeedIo\RuleSet
114
     */
115 10
    protected function buildBaseRuleSet() : RuleSet
116
    {
117 10
        $ruleSet = parent::buildBaseRuleSet();
118
        $ruleSet
119 10
            ->add(new Link())
120 10
            ->add(new Description(), ['content:encoded'])
121 10
            ->add($this->getModifiedSinceRule(static::DATE_NODE_TAGNAME), ['lastBuildDate', 'lastPubDate'])
122 10
            ->add(new Category())
123 10
            ->add(new Image());
124
125 10
        return $ruleSet;
126
    }
127
}
128