Completed
Push — issue/134 ( d0dc5e...07c20b )
by Alex
01:47
created

Structure   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 54
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A setProperty() 0 11 3
A createElement() 0 9 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;
12
13
use FeedIo\Feed\NodeInterface;
14
use FeedIo\RuleAbstract;
15
use FeedIo\RuleSet;
16
17
class Structure extends RuleAbstract
18
{
19
    const NODE_NAME = 'structure';
20
21
    /**
22
     * @var \FeedIo\RuleSet
23
     */
24
    protected $ruleSet;
25
26
    /**
27
     * @param string  $nodeName
28
     * @param RuleSet $ruleSet
29
     */
30 6
    public function __construct($nodeName = null, $ruleSet = null)
31
    {
32 6
        parent::__construct($nodeName);
33
34 6
        $this->ruleSet = is_null($ruleSet) ? new RuleSet() : $ruleSet;
35 6
    }
36
37
    /**
38
     * @param  NodeInterface $node
39
     * @param  \DOMElement   $element
40
     * @return mixed
41
     */
42 2
    public function setProperty(NodeInterface $node, \DOMElement $element)
43
    {
44 2
        foreach ($element->childNodes as $domNode) {
45 2
            if ($domNode instanceof \DomElement) {
46 2
                $rule = $this->ruleSet->get($domNode->tagName);
47 2
                $rule->setProperty($node, $domNode);
48 2
            }
49 2
        }
50
51 2
        return $this;
52
    }
53
54
    /**
55
     * creates the accurate DomElement content according to the $item's property
56
     *
57
     * @param  \DomDocument  $document
58
     * @param  NodeInterface $node
59
     * @return \DomElement
60
     */
61 1
    public function createElement(\DomDocument $document, NodeInterface $node)
62
    {
63 1
        $element = $document->createElement($this->getNodeName());
64 1
        foreach ($this->ruleSet->getRules() as $rule) {
65 1
            $element->appendChild($rule->createElement($document, $node));
66 1
        }
67
68 1
        return $element;
69
    }
70
}
71