Completed
Push — issue/87 ( bbd942 )
by Alex
04:44
created

Media   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 51.85%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 78
ccs 14
cts 27
cp 0.5185
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getUrlAttributeName() 0 4 1
A setUrlAttributeName() 0 6 1
A setProperty() 0 13 2
A createElement() 0 10 3
A createMediaElement() 0 9 1
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\Item;
14
use FeedIo\Feed\Item\MediaInterface;
15
use FeedIo\Feed\ItemInterface;
16
use FeedIo\Feed\NodeInterface;
17
use FeedIo\RuleAbstract;
18
19
class Media extends RuleAbstract
20
{
21
22
    const NODE_NAME = 'enclosure';
23
24
    protected $urlAttributeName = 'url';
25
26
    /**
27
     * @return string
28
     */
29 2
    public function getUrlAttributeName()
30
    {
31 2
        return $this->urlAttributeName;
32
    }
33
34
    /**
35
     * @param  string $name
36
     * @return $this
37
     */
38 4
    public function setUrlAttributeName($name)
39
    {
40 4
        $this->urlAttributeName = $name;
41
42 4
        return $this;
43
    }
44
45
    /**
46
     * @param  NodeInterface $node
47
     * @param  \DOMElement   $element
48
     * @return $this
49
     */
50 2
    public function setProperty(NodeInterface $node, \DOMElement $element)
51
    {
52 2
        if ($node instanceof ItemInterface) {
53 2
            $media = $node->newMedia();
54 2
            $media->setType($this->getAttributeValue($element, 'type'))
55 2
                ->setUrl($this->getAttributeValue($element, $this->getUrlAttributeName()))
56 2
                ->setLength($this->getAttributeValue($element, 'length'));
57
58 2
            $node->addMedia($media);
59 2
        }
60
61 2
        return $this;
62
    }
63
64
    /**
65
     * creates the accurate DomElement content according to the $item's property
66
     *
67
     * @param  \DomDocument  $document
68
     * @param  NodeInterface $node
69
     * @return \DomElement
70
     */
71
    public function createElement(\DomDocument $document, NodeInterface $node)
72
    {
73
        if ($node instanceof ItemInterface) {
74
            foreach ($node->getMedias() as $media) {
75
                return $this->createMediaElement($document, $media);
76
            }
77
        }
78
79
        return;
80
    }
81
82
    /**
83
     * @param  \DomDocument   $document
84
     * @param  MediaInterface $media
85
     * @return \DomElement
86
     */
87
    public function createMediaElement(\DomDocument $document, MediaInterface $media)
88
    {
89
        $element = $document->createElement($this->getNodeName());
90
        $element->setAttribute($this->getUrlAttributeName(), $media->getUrl());
91
        $element->setAttribute('type', $media->getType());
92
        $element->setAttribute('length', $media->getLength());
93
94
        return $element;
95
    }
96
}
97