Completed
Push — issue/299 ( f1e88f )
by Alex
09:47
created

Link::selectAlternateLink()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 4
nc 2
nop 2
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\Rule\Atom;
12
13
use FeedIo\Feed\NodeInterface;
14
use FeedIo\Rule\Link as BaseLink;
15
16
class Link extends BaseLink
17
{
18
    const NODE_NAME = 'link';
19
20
    /**
21
     * @param  NodeInterface $node
22
     * @param  \DOMElement   $element
23
     */
24
    public function setProperty(NodeInterface $node, \DOMElement $element) : void
25
    {
26
        if ($element->hasAttribute('href')) {
27
            $this->selectAlternateLink($node, $element);
28
        }
29
    }
30
31
    protected function selectAlternateLink(NodeInterface $node, \DOMElement $element): void
32
    {
33
        if (
34
        ($element->hasAttribute('rel') && $element->getAttribute('rel') == 'alternate')
35
        || is_null($node->getLink())
36
        ) {
37
            $node->setLink($element->getAttribute('href'));
38
        }
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void
45
    {
46
        $element = $document->createElement(static::NODE_NAME);
47
        $element->setAttribute('href', $node->getLink());
48
49
        $rootElement->appendChild($element);
50
    }
51
}
52