Completed
Push — issue/265 ( f8d18d )
by Alex
01:45
created

Description   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 44
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hasValue() 0 4 1
A addElement() 0 10 3
A setProperty() 0 13 3
1
<?php declare(strict_types=1);
2
/**
3
 * Created by PhpStorm.
4
 * User: alex
5
 * Date: 26/10/14
6
 * Time: 00:26
7
 */
8
namespace FeedIo\Rule;
9
10
use FeedIo\Feed\NodeInterface;
11
use FeedIo\RuleAbstract;
12
13
class Description extends RuleAbstract
14
{
15
    const NODE_NAME = 'description';
16
17
    /**
18
     * @param  NodeInterface $node
19
     * @param  \DOMElement   $element
20
     */
21 11
    public function setProperty(NodeInterface $node, \DOMElement $element) : void
22
    {
23 11
        $string = '';
24 11
        foreach ($element->childNodes as $childNode) {
25 11
            if ($childNode->nodeType == XML_CDATA_SECTION_NODE) {
26 2
                $string .= $childNode->textContent;
27
            } else {
28 11
                $string .= $element->ownerDocument->saveXML($childNode);
29
            }
30
        }
31
32 11
        $node->setDescription(htmlspecialchars_decode($string));
33 11
    }
34
35
    /**
36
     * @inheritDoc
37
     */
38 5
    protected function hasValue(NodeInterface $node) : bool
39
    {
40 5
        return !! $node->getDescription();
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46 5
    protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void
47
    {
48 5
        $description = htmlspecialchars($node->getDescription());
49 5
        $element = $document->createElement($this->getNodeName(), $description);
50 5
        if ($description !== $node->getDescription() && $this->getNodeName() != 'description') {
51
            $element->setAttribute('type', 'html');
52
        }
53
54 5
        $rootElement->appendChild($element);
55 5
    }
56
}
57