1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* RssAtomBundle. |
5
|
|
|
* |
6
|
|
|
* |
7
|
|
|
* @license http://opensource.org/licenses/lgpl-3.0.html LGPL |
8
|
|
|
* @copyright (c) 2013, Alexandre Debril |
9
|
|
|
* |
10
|
|
|
* creation date : 31 mars 2013 |
11
|
|
|
*/ |
12
|
|
|
namespace Debril\RssAtomBundle\Provider; |
13
|
|
|
|
14
|
|
|
use FeedIo\Feed; |
15
|
|
|
use FeedIo\Feed\Item; |
16
|
|
|
use Debril\RssAtomBundle\Exception\FeedException\FeedNotFoundException; |
17
|
|
|
use FeedIo\FeedInterface; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
|
20
|
|
|
class MockProvider implements FeedProviderInterface |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Request $request |
25
|
|
|
* @return FeedInterface |
26
|
|
|
* @throws FeedNotFoundException |
27
|
|
|
*/ |
28
|
7 |
|
public function getFeed(Request $request): FeedInterface |
29
|
|
|
{ |
30
|
7 |
|
$id = $request->get('id'); |
31
|
|
|
|
32
|
7 |
|
return $this->buildFeed($id); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $id |
37
|
|
|
* @return FeedInterface |
38
|
|
|
* @throws FeedNotFoundException |
39
|
|
|
*/ |
40
|
5 |
View Code Duplication |
protected function buildFeed(string $id): FeedInterface |
|
|
|
|
41
|
|
|
{ |
42
|
5 |
|
if ($id === 'not-found') { |
43
|
1 |
|
throw new FeedNotFoundException(); |
44
|
|
|
} |
45
|
|
|
|
46
|
4 |
|
$feed = new Feed(); |
47
|
|
|
|
48
|
4 |
|
$feed->setPublicId($id); |
49
|
4 |
|
$feed->setTitle('thank you for using RssAtomBundle'); |
50
|
4 |
|
$feed->setDescription('this is the mock FeedContent'); |
51
|
4 |
|
$feed->setLink('https://raw.github.com/alexdebril/rss-atom-bundle/'); |
52
|
4 |
|
$feed->setLastModified(new \DateTime()); |
53
|
|
|
|
54
|
4 |
|
return $this->addItem($feed); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Feed $feed |
59
|
|
|
* @return FeedInterface |
60
|
|
|
* @throws \Exception |
61
|
|
|
*/ |
62
|
4 |
View Code Duplication |
protected function addItem(Feed $feed) : FeedInterface |
|
|
|
|
63
|
|
|
{ |
64
|
4 |
|
$item = new Item(); |
65
|
|
|
|
66
|
4 |
|
$item->setPublicId('1'); |
67
|
4 |
|
$item->setLink('https://raw.github.com/alexdebril/rss-atom-bundle/somelink'); |
68
|
4 |
|
$item->setTitle('This is an item'); |
69
|
4 |
|
$item->setDescription('this stream was generated using the MockProvider class'); |
70
|
4 |
|
$item->setLastModified(new \DateTime()); |
71
|
|
|
|
72
|
4 |
|
$feed->add($item); |
73
|
|
|
|
74
|
4 |
|
return $feed; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
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.