Completed
Push — issue/163 ( 777402 )
by Alex
02:34
created

Description   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 2
dl 0
loc 44
ccs 15
cts 17
cp 0.8824
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setProperty() 0 13 4
A hasValue() 0 4 1
A addElement() 0 10 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
        if ($element->firstChild && $element->firstChild->nodeType == XML_CDATA_SECTION_NODE) {
25
            $string = $element->firstChild->textContent;
26
        } else {
27 11
            foreach ($element->childNodes as $childNode) {
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