Completed
Push — master ( 33bbaa...8a8db9 )
by Alex
13s queued 11s
created

Atom::buildItemRuleSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Atom\Author;
16
use FeedIo\Rule\Atom\LinkNode;
17
use FeedIo\Rule\Atom\Logo;
18
use FeedIo\Rule\Description;
19
use FeedIo\Rule\Language;
20
use FeedIo\Rule\PublicId;
21
use FeedIo\Rule\Atom\Category;
22
use FeedIo\RuleSet;
23
24
class Atom extends XmlAbstract
25
{
26
    /**
27
     * Atom document must have a <feed> root node
28
     */
29
    const ROOT_NODE_TAGNAME = 'feed';
30
31
    const ITEM_NODE = 'entry';
32
33
    const DATETIME_FORMAT = \DateTime::ATOM;
34
35
    /**
36
     * Formats the document according to the standard's specification
37
     * @param  \DOMDocument $document
38
     * @return \DOMDocument
39
     */
40 4
    public function format(\DOMDocument $document) : \DOMDocument
41
    {
42 4
        $element = $document->createElement('feed');
43 4
        $element->setAttribute('xmlns', 'http://www.w3.org/2005/Atom');
44 4
        $document->appendChild($element);
45
46 4
        return $document;
47
    }
48
49
    /**
50
     * Tells if the parser can handle the feed or not
51
     * @param  Document $document
52
     * @return mixed
53
     */
54 4 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...
55
    {
56 4
        if (!isset($document->getDOMDocument()->documentElement->tagName)) {
57
            return false;
58
        }
59 4
        return self::ROOT_NODE_TAGNAME === $document->getDOMDocument()->documentElement->tagName;
60
    }
61
62
    /**
63
     * @param  DOMDocument $document
64
     * @return \DomElement
65
     */
66 7
    public function getMainElement(\DOMDocument $document) : \DOMElement
67
    {
68 7
        return $document->documentElement;
69
    }
70
71
    /**
72
     * Builds and returns a rule set to parse the root node
73
     * @return \FeedIo\RuleSet
74
     */
75 8
    public function buildFeedRuleSet() : RuleSet
76
    {
77 8
        $ruleSet = $this->buildBaseRuleSet();
78
        $ruleSet
79 8
            ->add(new LinkNode())
80 8
            ->add(new PublicId('id'))
81 8
            ->add(new Language('lang'))
82 8
            ->add($this->getModifiedSinceRule('updated'), ['published'])
83
        ;
84
85 8
        return $ruleSet;
86
    }
87
88
    /**
89
     * Builds and returns a rule set to parse an item
90
     * @return \FeedIo\RuleSet
91
     */
92 7
    public function buildItemRuleSet() : RuleSet
93
    {
94 7
        $ruleSet = $this->buildFeedRuleSet();
95
        $ruleSet
96 7
            ->add(new Author())
97 7
            ->add(new Description('content'), ['summary']);
98
99 7
        return $ruleSet;
100
    }
101
102
    /**
103
     * @return \FeedIo\RuleSet
104
     */
105 8
    protected function buildBaseRuleSet() : RuleSet
106
    {
107 8
        $ruleSet = parent::buildBaseRuleSet();
108 8
        $ruleSet->add(new Category())
109 8
            ->add(new Logo());
110
111 8
        return $ruleSet;
112
    }
113
}
114