Completed
Pull Request — master (#122)
by Alex
03:03
created

RssParser::canHandle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * Rss/Atom Bundle for Symfony.
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\Protocol\FeedInterface;
13
use Debril\RssAtomBundle\Protocol\ItemInInterface;
14
use Debril\RssAtomBundle\Protocol\Parser;
15
use SimpleXMLElement;
16
17
/**
18
 * Class RssParser.
19
 * @deprecated removed in version 3.0
20
 */
21
class RssParser extends Parser
22
{
23
    protected $mandatoryFields = array(
24
        'channel',
25
    );
26
27
    /**
28
     *
29
     * @deprecated removed in version 3.0
30
     */
31 3
    public function __construct()
32
    {
33 3
        $this->setdateFormats(array(\DateTime::RSS));
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...arser::setDateFormats() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
34 3
    }
35
36
    /**
37
     * @param SimpleXMLElement $xmlBody
38
     *
39
     * @return bool
40
     * @deprecated removed in version 3.0
41
     */
42 3
    public function canHandle(SimpleXMLElement $xmlBody)
43
    {
44 3
        return 'rss' === strtolower($xmlBody->getName());
45
    }
46
47
    /**
48
     * @param SimpleXMLElement $xmlBody
49
     * @param FeedInterface    $feed
50
     * @param array            $filters
51
     *
52
     * @return FeedInterface
53
     */
54 6
    protected function parseBody(SimpleXMLElement $xmlBody, FeedInterface $feed, array $filters)
55
    {
56 6
        $namespaces = $xmlBody->getNamespaces(true);
57
58 6
        $feed->setPublicId($xmlBody->channel->link);
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...nterface::setPublicId() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
59 6
        $feed->setLink($xmlBody->channel->link);
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...dInInterface::setLink() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
60 6
        $feed->setTitle($xmlBody->channel->title);
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...InInterface::setTitle() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
61 6
        $feed->setDescription($xmlBody->channel->description);
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...rface::setDescription() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
62
63 6
        $latest = new \DateTime('@0');
64 6
        $date = new \DateTime('now');
65 6
        foreach ($xmlBody->channel->item as $xmlElement) {
66 6
            $item = $this->newItem();
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Protocol\Parser::newItem() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
67
68 6
            if (isset($xmlElement->pubDate)) {
69 6
                $readDate = trim($xmlElement->pubDate);
70
71 6
                $format = isset($format) ? $format : $this->guessDateFormat($readDate);
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...rser::guessDateFormat() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
72 6
                $date = static::convertToDateTime($readDate, $format);
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...er::convertToDateTime() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
73 6
            }
74
75 6
            $item->setTitle($xmlElement->title)
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...InInterface::setTitle() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
76 6
                 ->setDescription($xmlElement->description)
77 6
                 ->setPublicId($xmlElement->guid)
78 6
                 ->setUpdated($date)
79 6
                 ->setLink($xmlElement->link)
80 6
                 ->setComment($xmlElement->comments);
81
82 6
            if ($date > $latest) {
83 6
                $latest = $date;
84 6
            }
85
86 6
            $this->parseCategories($xmlElement, $item);
87
88 6
            $this->handleAuthor($xmlElement, $item);
89 6
            $this->handleDescription($xmlElement, $item);
90
91 6
            $item->setAdditional($this->getAdditionalNamespacesElements($xmlElement, $namespaces));
92
93 6
            $this->handleEnclosure($xmlElement, $item);
94 6
            $this->handleMediaExtension($xmlElement, $item);
95
96 6
            $this->addValidItem($feed, $item, $filters);
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...\Parser::addValidItem() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
97 6
        }
98
99 6
        $this->detectAndSetLastModified($xmlBody, $feed, $latest);
100
101 6
        return $feed;
102
    }
103
104
    /**
105
     * @param SimpleXMLElement $xmlBody
106
     * @param FeedInterface    $feed
107
     * @param $latestItemDate
108
     */
109 1
    protected function detectAndSetLastModified(SimpleXMLElement $xmlBody, FeedInterface $feed, $latestItemDate)
110
    {
111 1
        if (isset($xmlBody->channel->lastBuildDate)) {
112 1
            $this->setLastModified($feed, $xmlBody->channel->lastBuildDate);
113 1
        } elseif (isset($xmlBody->channel->pubDate)) {
114
            $this->setLastModified($feed, $xmlBody->channel->pubDate);
115
        } else {
116
            $feed->setLastModified($latestItemDate);
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...face::setLastModified() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
117
        }
118 1
    }
119
120
    /**
121
     * @param FeedInterface $feed
122
     * @param string        $rssDate
123
     */
124 2
    protected function setLastModified(FeedInterface $feed, $rssDate)
125
    {
126 2
        $format = $this->guessDateFormat($rssDate);
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...rser::guessDateFormat() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
127 2
        $updated = static::convertToDateTime($rssDate, $format);
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...er::convertToDateTime() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
128 2
        $feed->setLastModified($updated);
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...face::setLastModified() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
129 2
    }
130
131
    /**
132
     * Handles enclosures if any.
133
     *
134
     * @param SimpleXMLElement $element
135
     * @param ItemInInterface  $item
136
     *
137
     * @return $this
138
     */
139 1
    protected function handleEnclosure(SimpleXMLElement $element, ItemInInterface $item)
140
    {
141 1
        if (isset($element->enclosure)) {
142
            foreach ($element->enclosure as $enclosure) {
143
                $media = $this->createMedia($enclosure);
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...l\Parser::createMedia() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
144
                $item->addMedia($media);
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...InInterface::addMedia() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
145
            }
146 1
        }
147
148 1
        return $this;
149
    }
150
151
    /**
152
     * According to RSS specs, either we can have a summary in description ;
153
     * full content in description ; or a summary in description AND full content in content:encoded
154
     *
155
     * @param SimpleXMLElement $xmlElement
156
     * @param ItemInInterface $item
157
     */
158 1
    protected function handleDescription(SimpleXMLElement $xmlElement, ItemInInterface $item)
159
    {
160 1
        $contentChild = $xmlElement->children('http://purl.org/rss/1.0/modules/content/');
161
162 1
        if (isset($contentChild->encoded)) {
163
            $item->setDescription($contentChild->encoded);
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...rface::setDescription() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
164 1
            $item->setSummary($xmlElement->description);
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...Interface::setSummary() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
165
        } else {
166 1
            $item->setDescription($xmlElement->description);
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...rface::setDescription() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
167
        }
168 1
    }
169
170
    /**
171
     * Parse elements from Yahoo RSS Media extension
172
     *
173
     * @param SimpleXMLElement $xmlElement
174
     * @param ItemInInterface $item with Media added
175
     */
176 1
    protected function handleMediaExtension(SimpleXMLElement $xmlElement, ItemInInterface $item)
177
    {
178 1
        foreach ($xmlElement->children('http://search.yahoo.com/mrss/') as $xmlMedia) {
179 1
            $media = new Media();
0 ignored issues
show
Deprecated Code introduced by
The class Debril\RssAtomBundle\Protocol\Parser\Media has been deprecated with message: removed in version 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
180 1
            $media->setUrl($this->getAttributeValue($xmlMedia, 'url'))
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...er::getAttributeValue() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...\Parser\Media::setUrl() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...Parser\Media::setType() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...rser\Media::setLength() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
181 1
                ->setType($this->searchAttributeValue($xmlMedia, array('type', 'medium')))
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...:searchAttributeValue() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
182 1
                ->setLength($this->getAttributeValue($xmlMedia, 'fileSize'))
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...er::getAttributeValue() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
183
            ;
184
185 1
            $item->addMedia($media);
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...InInterface::addMedia() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
186 1
        }
187 1
    }
188
189
    /**
190
     * Parse category elements.
191
     * We may have more than one.
192
     *
193
     * @param SimpleXMLElement $element
194
     * @param ItemInInterface $item
195
     */
196 1 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...
197
    {
198 1
        foreach ($element->category as $xmlCategory) {
199
            $category = new Category();
0 ignored issues
show
Deprecated Code introduced by
The class Debril\RssAtomBundle\Protocol\Parser\Category has been deprecated with message: removed in version 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
200
            $category->setName((string) $xmlCategory);
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...ser\Category::setName() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
201
202 1
            $item->addCategory($category);
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...nterface::addCategory() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
203 1
        }
204 1
    }
205
206
    /**
207
     * Parse author:
208
     * first we look at optional dc:creator, which is the author name
209
     * if no, we fallback to the RSS author element which is the author email
210
     *
211
     * @param SimpleXMLElement $element
212
     * @param ItemInInterface $item
213
     */
214 1
    protected function handleAuthor(SimpleXMLElement $element, ItemInInterface $item)
215
    {
216 1
        $dcChild = $element->children('http://purl.org/dc/elements/1.1/');
217
218 1
        if (isset($dcChild->creator)) {
219 1
            $item->setAuthor((string) $dcChild->creator);
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...nInterface::setAuthor() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
220 1
        } else {
221
            $item->setAuthor((string) $element->author);
0 ignored issues
show
Deprecated Code introduced by
The method Debril\RssAtomBundle\Pro...nInterface::setAuthor() has been deprecated with message: removed in version 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
222
        }
223 1
    }
224
}
225