Completed
Pull Request — project/php-7 (#133)
by Alex
04:04 queued 01:49
created

LinkNode::setProperty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
crap 2
1
<?php
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\ItemInterface;
14
use FeedIo\Feed\NodeInterface;
15
use FeedIo\RuleAbstract;
16
use FeedIo\RuleSet;
17
18
class LinkNode extends RuleAbstract
19
{
20
    const NODE_NAME = 'link';
21
22
    /**
23
     * @var \FeedIo\RuleSet
24
     */
25
    protected $ruleSet;
26
27
    /**
28
     * @param string $nodeName
29
     */
30 8
    public function __construct($nodeName = null)
31
    {
32 8
        parent::__construct($nodeName);
33 8
        $mediaRule = new Media();
34 8
        $mediaRule->setUrlAttributeName('href');
35 8
        $this->ruleSet = new RuleSet(new Link('related'));
36 8
        $this->ruleSet->add($mediaRule, ['media', 'enclosure']);
37 8
    }
38
39
    /**
40
     * @param  NodeInterface $node
41
     * @param  \DOMElement   $element
42
     * @return mixed
43
     */
44 4
    public function setProperty(NodeInterface $node, \DOMElement $element)
45
    {
46 4
        if ($element->hasAttribute('rel')) {
47 3
            $this->ruleSet->get($element->getAttribute('rel'))->setProperty($node, $element);
48
        } else {
49 3
            $this->ruleSet->getDefault()->setProperty($node, $element);
50
        }
51
52 4
        return $this;
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58 2
    protected function hasValue(NodeInterface $node) : bool
59
    {
60 2
        return true;
61
    }
62
63
64
    /**
65
     * @inheritDoc
66
     */
67 2
    protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void
68
    {
69 2
        if ($node instanceof ItemInterface) {
70 2
            foreach ($node->getMedias() as $media) {
71
                $this->ruleSet->get('media')->apply($document, $rootElement, $node);
72
            }
73
        }
74
75 2
        $this->ruleSet->getDefault()->apply($document, $rootElement, $node);
76 2
    }
77
}
78