Completed
Push — master ( 13bd05...68fc02 )
by Alex
04:24
created

AtomParser   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 156
Duplicated Lines 5.77 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 15
Bugs 4 Features 1
Metric Value
wmc 19
c 15
b 4
f 1
lcom 1
cbo 4
dl 9
loc 156
ccs 64
cts 64
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A canHandle() 0 4 1
B parseBody() 0 32 4
A parseHeaders() 0 12 1
A detectLink() 0 11 3
A parseContent() 0 13 4
A handleEnclosure() 0 11 3
A parseCategories() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Rss/Atom Bundle for Symfony 2.
5
 *
6
 *
7
 * @license http://opensource.org/licenses/lgpl-3.0.html LGPL
8
 * @copyright (c) 2013, Alexandre Debril
9
 */
10
namespace Debril\RssAtomBundle\Protocol\Parser;
11
12
use Debril\RssAtomBundle\Exception\ParserException;
13
use Debril\RssAtomBundle\Protocol\FeedInterface;
14
use Debril\RssAtomBundle\Protocol\ItemInInterface;
15
use Debril\RssAtomBundle\Protocol\Parser;
16
use SimpleXMLElement;
17
18
/**
19
 * Class AtomParser.
20
 */
21
class AtomParser extends Parser
22
{
23
    protected $mandatoryFields = array(
24
        'id',
25
        'updated',
26
        'title',
27
        'link',
28
        'entry',
29
    );
30
31
    /**
32
     *
33
     */
34 7
    public function __construct()
35
    {
36 7
        $this->setdateFormats(array(\DateTime::RFC3339));
37 7
    }
38
39
    /**
40
     * @param SimpleXMLElement $xmlBody
41
     *
42
     * @return bool
43
     */
44 5
    public function canHandle(SimpleXMLElement $xmlBody)
45
    {
46 5
        return 'feed' === $xmlBody->getName();
47
    }
48
49
    /**
50
     * @param SimpleXMLElement $xmlBody
51
     * @param FeedInterface    $feed
52
     * @param array            $filters
53
     *
54
     * @return FeedInterface
55
     *
56
     * @throws ParserException
57
     */
58 4
    protected function parseBody(SimpleXMLElement $xmlBody, FeedInterface $feed, array $filters)
59
    {
60 4
        $this->parseHeaders($xmlBody, $feed);
61
62 4
        $namespaces = $xmlBody->getNamespaces(true);
63
64 4
        foreach ($xmlBody->entry as $xmlElement) {
65 4
            $itemFormat = isset($itemFormat) ? $itemFormat : $this->guessDateFormat($xmlElement->updated);
66
67 4
            $item = $this->newItem();
68 4
            $item->setTitle($xmlElement->title)
69 4
                    ->setPublicId($xmlElement->id)
70 4
                    ->setSummary($this->parseContent($xmlElement->summary))
71 4
                    ->setDescription($this->parseContent($xmlElement->content))
72 4
                    ->setUpdated(static::convertToDateTime($xmlElement->updated, $itemFormat));
73
74 4
            $item->setLink($this->detectLink($xmlElement, 'alternate'));
75
76 4
            if ($xmlElement->author) {
77 4
                $item->setAuthor($xmlElement->author->name);
78 4
            }
79
80 4
            $item->setAdditional($this->getAdditionalNamespacesElements($xmlElement, $namespaces));
81 4
            $this->handleEnclosure($xmlElement, $item);
82
83 4
            $this->parseCategories($xmlElement, $item);
84
85 4
            $this->addValidItem($feed, $item, $filters);
86 4
        }
87
88 4
        return $feed;
89
    }
90
91
    /**
92
     * @param SimpleXMLElement $xmlBody
93
     * @param FeedInterface    $feed
94
     *
95
     * @throws ParserException
96
     */
97 3
    protected function parseHeaders(SimpleXMLElement $xmlBody, FeedInterface $feed)
98
    {
99 3
        $feed->setPublicId($xmlBody->id);
100
101 3
        $feed->setLink(current($this->detectLink($xmlBody, 'self')));
102 3
        $feed->setTitle($xmlBody->title);
103 3
        $feed->setDescription($xmlBody->subtitle);
104
105 3
        $format = $this->guessDateFormat($xmlBody->updated);
106 3
        $updated = static::convertToDateTime($xmlBody->updated, $format);
107 3
        $feed->setLastModified($updated);
108 3
    }
109
110
    /**
111
     * @param SimpleXMLElement $xmlElement
112
     * @param string           $type
113
     */
114 4
    protected function detectLink(SimpleXMLElement $xmlElement, $type)
115
    {
116 4
        foreach ($xmlElement->link as $xmlLink) {
117 4
            if ((string) $xmlLink['rel'] === $type) {
118 4
                return $xmlLink['href'];
119
            }
120 3
        }
121
122
        // return the first if the desired link does not exist
123 2
        return $xmlElement->link[0]['href'];
124
    }
125
126 4
    protected function parseContent(SimpleXMLElement $content)
127
    {
128 4
        if ($content && 0 < $content->children()->count()) {
129 3
            $out = '';
130 3
            foreach ($content->children() as $child) {
131 3
                $out .= $child->asXML();
132 3
            }
133
134 3
            return $out;
135
        }
136
137 4
        return $content;
138
    }
139
140
    /**
141
     * Handles enclosures if any.
142
     *
143
     * @param SimpleXMLElement $element
144
     * @param ItemInInterface  $item
145
     *
146
     * @return $this
147
     */
148 3
    protected function handleEnclosure(SimpleXMLElement $element, ItemInInterface $item)
149
    {
150 3
        foreach ($element->link as $link) {
151 3
            if (strcasecmp($this->getAttributeValue($link, 'rel'), 'enclosure') === 0) {
152 1
                $media = $this->createMedia($link);
153 1
                $item->addMedia($media);
154 1
            }
155 3
        }
156
157 3
        return $this;
158
    }
159
160
    /**
161
     * Parse category elements.
162
     * We may have more than one.
163
     *
164
     * @param SimpleXMLElement $element
165
     * @param ItemInInterface $item
166
     */
167 3 View Code Duplication
    protected function parseCategories(SimpleXMLElement $element, ItemInInterface $item)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
    {
169 3
        foreach ($element->category as $xmlCategory) {
170 1
            $category = new Category();
171 1
            $category->setName($this->getAttributeValue($xmlCategory, 'term'));
172
173 1
            $item->addCategory($category);
174 3
        }
175 3
    }
176
}
177