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

LinkNode   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 60
ccs 19
cts 20
cp 0.95
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A setProperty() 0 10 2
A hasValue() 0 4 1
A addElement() 0 10 3
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