Completed
Push — master ( 4c7da2...64af60 )
by Alex
02:34 queued 46s
created

LinkNode::createElement()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 9
cp 0.8889
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 2
nop 2
crap 3.0123

1 Method

Rating   Name   Duplication   Size   Complexity  
A LinkNode::addElement() 0 8 3
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\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 9
    public function __construct(string $nodeName = null)
31
    {
32 9
        parent::__construct($nodeName);
33 9
        $mediaRule = new Media();
34 9
        $mediaRule->setUrlAttributeName('href');
35 9
        $this->ruleSet = new RuleSet(new Link('related'));
36 9
        $this->ruleSet->add($mediaRule, ['media', 'enclosure']);
37 9
    }
38
39
    /**
40
     * @param  NodeInterface $node
41
     * @param  \DOMElement   $element
42
     * @return mixed
43
     */
44 5
    public function setProperty(NodeInterface $node, \DOMElement $element) : void
45
    {
46 5
        if ($element->hasAttribute('rel')) {
47 3
            $this->ruleSet->get($element->getAttribute('rel'))->setProperty($node, $element);
48
        } else {
49 4
            $this->ruleSet->getDefault()->setProperty($node, $element);
50
        }
51 5
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56 2
    protected function hasValue(NodeInterface $node) : bool
57
    {
58 2
        return true;
59
    }
60
61
62
    /**
63
     * @inheritDoc
64
     */
65 2
    protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void
66
    {
67 2
        if ($node instanceof ItemInterface && $node->hasMedia()) {
68
            $this->ruleSet->get('media')->apply($document, $rootElement, $node);
69
        }
70
71 2
        $this->ruleSet->getDefault()->apply($document, $rootElement, $node);
72 2
    }
73
}
74