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

Atom::canHandle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

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