Skip to content

Commit be7367c

Browse files
committed
refactor
1 parent b9228cc commit be7367c

File tree

15 files changed

+212
-236
lines changed

15 files changed

+212
-236
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ If you discover any security related issues, please email freek@spatie.be instea
129129
## Credits
130130

131131
- [Jolita Grazyte](https://github.com/JolitaGrazyte)
132+
- [Freek Van der Herten](https://github.com/freekmurze)
132133
- [All Contributors](../../contributors)
133134

134135
## About Spatie

config/laravel-feed.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@
55
'feeds' => [
66
[
77
/*
8-
* Fill in for items a class with a method that returns a collection of items
9-
* that you want in the feed.
10-
* e.g.: 'App\Repositories\NewsItemRepository@getAllOnline'
11-
* For url fill in a url, on which the feeds will be shown.
8+
* Here you can specify which class an method will return the items
9+
* that should appeary in the feed. For example:
10+
* 'App\Repositories\NewsItemRepository@getAllOnline'
1211
*/
1312
'items' => '',
13+
14+
/*
15+
* The feed will be available on this url
16+
*/
1417
'url' => '',
15-
'title' => 'This is feed 1 from the unit tests',
16-
'description' => 'This is feed 1 from the unit tests.',
18+
19+
'title' => 'My feed',
20+
21+
'description' => 'Description of my feed',
1722
],
1823
],
1924

resources/views/feed.blade.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
@endforeach
1010
@foreach($items as $item)
1111
<entry>
12-
<title>{{ $item['title'] }}</title>
13-
<link>{{ $item['link'] }}</link>
14-
<id>{{ $item['id'] }}</id>
12+
<title>{{ $item->getFeedItemTitle() }}</title>
13+
<link>{{ $item->getFeedItemLink() }}</link>
14+
<id>{{ $item->getFeedItemId() }}</id>
1515
<summary>
16-
{{ $item['summary'] }}
16+
{{ $item->getFeedItemSummary() }}
1717
</summary>
18-
<updated>{{ $item['updated'] }}</updated>
18+
<updated>{{ $item->getFeedItemUpdated() }}</updated>
1919
</entry>
2020
@endforeach
2121
</feed>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Spatie\Feed\Exceptions;
4+
5+
use Exception;
6+
7+
class InvalidConfiguration extends Exception
8+
{
9+
public static function delimiterNotPresent($configValue)
10+
{
11+
return new static("Could not find delimeter '@' in `{$configValue}`");
12+
}
13+
}

src/Feed.php

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,47 @@
22

33
namespace Spatie\Feed;
44

5-
use Illuminate\Contracts\Foundation\Application;
65
use Illuminate\Http\Response as HttpResponse;
7-
use Illuminate\Support\Facades\Response;
6+
use Illuminate\Support\Collection;
7+
use Spatie\Feed\Exceptions\InvalidConfiguration;
88

99
class Feed
1010
{
11-
protected $app;
12-
13-
public function __construct(Application $app)
11+
public function getViewResponse(array $feedConfiguration) : HttpResponse
1412
{
15-
$this->app = $app;
13+
$feedContent = $this->getFeedContent($feedConfiguration);
14+
15+
return response($feedContent, 200, ['Content-Type' => 'application/atom+xml; chartset=UTF-8']);
1616
}
1717

18-
public function feed($feed) : HttpResponse
18+
public function getFeedContent(array $feedConfiguration) : string
1919
{
20-
$data = [];
21-
$items = explode('@', $feed['items']);
22-
$method = $items[1];
23-
24-
$data['items'] = $this->app->make($items[0])->$method()->map(function (FeedItem $item) {
25-
return $item->getFeedData();
26-
});
27-
28-
$data['meta'] = [
29-
'link' => $feed['url'],
30-
'description' => $feed['description'],
31-
'title' => $feed['title'],
32-
'updated' => collect($data['items'])->sortBy('updated')->last()['updated']->format('Y-m-d H:i:s'),
20+
if (!str_contains($feedConfiguration['items'], '@')) {
21+
throw InvalidConfiguration::delimiterNotPresent($feedConfiguration['items']);
22+
}
23+
24+
list($class, $method) = explode('@', $feedConfiguration['items']);
25+
26+
$items = app($class)->$method();
27+
28+
$meta = [
29+
'link' => $feedConfiguration['url'],
30+
'description' => $feedConfiguration['description'],
31+
'title' => $feedConfiguration['title'],
32+
'updated' => $this->getLastUpdatedDate($items, 'Y-m-d H:i:s'),
3333
];
3434

35-
return Response::view(
36-
'laravel-feed::feed',
37-
$data, 200,
38-
['Content-Type' => 'application/atom+xml; chartset=UTF-8']
39-
);
35+
return view('laravel-feed::feed', compact('meta', 'items'))->render();
36+
}
37+
38+
protected function getLastUpdatedDate(Collection $items, string $format) : string
39+
{
40+
$lastItem = $items
41+
->sortBy(function (FeedItem $feedItem) {
42+
return $feedItem->getFeedItemUpdated()->format('YmdHis');
43+
})
44+
->last();
45+
46+
return $lastItem->getFeedItemUpdated()->format($format);
4047
}
4148
}

src/FeedItem.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@
66

77
interface FeedItem
88
{
9-
public function getFeedData() : array;
10-
11-
public function getUpdated() : Carbon;
9+
public function getFeedItemId();
1210

1311
public function getFeedItemTitle() : string;
1412

15-
public function getFeedItemId();
16-
1713
public function getFeedItemUpdated() : Carbon;
1814

1915
public function getFeedItemSummary() : string;

src/FeedServiceProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ public function register()
3535
*/
3636
public function getFeeds()
3737
{
38-
foreach (config('laravel-feed.feeds') as $feed) {
39-
$this->app['router']->get($feed['url'], function () use ($feed) {
38+
foreach (config('laravel-feed.feeds') as $feedConfiguration) {
39+
$this->app['router']->get($feedConfiguration['url'], function () use ($feedConfiguration) {
4040

41-
return $this->app->make(Feed::class)->feed($feed);
41+
return $this->app->make(Feed::class)->getViewResponse($feedConfiguration);
4242

4343
});
4444
}

tests/DummyItem.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,28 @@
77

88
class DummyItem implements FeedItem
99
{
10-
public function getFeedData() : array
11-
{
12-
return [
13-
'title' => $this->getFeedItemTitle(),
14-
'id' => $this->getFeedItemId(),
15-
'updated' => $this->getFeedItemUpdated(),
16-
'summary' => $this->getFeedItemSummary(),
17-
'link' => $this->getFeedItemLink(),
18-
];
19-
}
20-
21-
public function getUpdated() : Carbon
22-
{
23-
return Carbon::create('2016', '02', '29', '16', '06', '18');
24-
}
25-
2610
public function getFeedItemId()
2711
{
2812
return 1;
2913
}
3014

3115
public function getFeedItemTitle() : string
3216
{
33-
return 'Ducimus ipsum consequatur vel libero debitis quis voluptatem.';
17+
return 'feedItemTitle';
3418
}
3519

3620
public function getFeedItemSummary() : string
3721
{
38-
return 'Officia aliquid rem repudiandae ut sed voluptatem non. Fuga libero omnis atque quam error. Iure dolorum labore ducimus temporibus.';
22+
return 'feedItemSummary';
3923
}
4024

4125
public function getFeedItemUpdated() : Carbon
4226
{
43-
return Carbon::create('2016', '02', '29', '16', '06', '18');
27+
return Carbon::now();
4428
}
4529

4630
public function getFeedItemLink() : string
4731
{
48-
return 'http://localhost/news/ducimus-ipsum-consequatur-vel-libero-debitis-quis-voluptatem';
32+
return 'https://localhost/news/testItem1';
4933
}
5034
}

tests/DummyRepository.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44

55
class DummyRepository
66
{
7-
public function getAllOnline()
7+
public function getAll()
88
{
99
return collect([
1010
new DummyItem(),
1111
new DummyItem(),
1212
new DummyItem(),
1313
new DummyItem(),
1414
new DummyItem(),
15-
1615
]);
1716
}
1817
}

tests/FeedTest.php

Lines changed: 27 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,91 +2,56 @@
22

33
namespace Spatie\Feed\Test;
44

5+
use XMLReader;
6+
57
class FeedTest extends TestCase
68
{
9+
protected $feedNames = ['feed1', 'feed2'];
10+
711
/** @test */
812
public function it_registers_routes_where_feeds_will_be_available()
913
{
10-
$this->assertEquals(200, $this->call('GET', '/feed1')->getStatusCode());
11-
$this->assertEquals(200, $this->call('GET', '/feed2')->getStatusCode());
12-
}
14+
collect($this->feedNames)->each(function (string $feedName) {
1315

14-
/** @test */
15-
public function a_feed_contains_meta_data()
16-
{
17-
$contents = $this->getContent();
18-
$metaData = $this->getMetaData();
19-
for ($i = 0; $i < count($contents); ++$i) {
20-
foreach ($metaData[$i] as $metaDataItem) {
21-
$this->assertContains($metaDataItem, $contents[$i]);
22-
}
23-
}
16+
$this->assertEquals(200, $this->call('GET', "/{$feedName}")->getStatusCode());
17+
18+
});
2419
}
2520

2621
/** @test */
2722
public function a_feed_contains_xml_content()
2823
{
29-
$i = 0;
30-
foreach ($this->getContent() as $content) {
31-
$this->assertTrue($this->validateXml($i, $content));
32-
++$i;
33-
}
34-
}
24+
collect($this->feedNames)->each(function (string $feedName) {
3525

36-
/** @test */
37-
public function a_feed_contains_all_selected_models()
38-
{
39-
foreach ($this->getContent() as $content) {
40-
$this->assertEquals(5, substr_count($content, '<entry>'));
41-
}
26+
$generatedFeedContent = $this->call('GET', "/{$feedName}")->getContent();
27+
28+
$this->assertTrue($this->validateXml($generatedFeedContent));
29+
30+
});
4231
}
4332

4433
/** @test */
4534
public function all_feed_items_have_expected_data()
4635
{
47-
$i = 0;
48-
foreach ($this->getContent() as $content) {
49-
$file = file_get_contents('tests/stubs/feeds_'.$i.'.xml');
50-
if (is_null($file)) {
51-
file_put_contents('tests/stubs/feeds_'.$i.'.xml', $content);
52-
}
53-
$this->assertEquals($file, $content);
54-
++$i;
55-
}
56-
}
36+
collect($this->feedNames)->each(function (string $feedName) {
5737

58-
protected function getMetaData()
59-
{
60-
return [
61-
[
62-
'<description>This is feed 1 from the unit tests</description>',
63-
'<link href="http://localhost/feed1">',
64-
'<updated>',
65-
],
66-
[
67-
'<description>This is feed 2 from the unit tests</description>',
68-
'<link href="http://localhost/feed2">',
69-
'<updated>',
70-
],
71-
];
72-
}
38+
$stubContent = file_get_contents("tests/stubs/{$feedName}.xml");
39+
$generatedFeedContent = $this->call('GET', "/{$feedName}")->getContent();
7340

74-
protected function getContent()
75-
{
76-
return [
77-
$this->call('GET', '/feed1')->getContent(),
78-
$this->call('GET', '/feed2')->getContent(),
79-
];
41+
$this->assertEquals($stubContent, $generatedFeedContent);
42+
});
8043
}
8144

82-
protected function validateXml($i, $content)
45+
protected function validateXml(string $content) : bool
8346
{
84-
$file = 'tests/temp/feeds_'.$i.'.xml';
47+
$file = 'tests/temp/validate.xml';
48+
8549
file_put_contents($file, $content);
86-
$xml_reader = new \XMLReader();
87-
$xml_reader->open($file);
88-
$xml_reader->setParserProperty($xml_reader::VALIDATE, true);
89-
return $xml_reader->isValid();
9050

51+
$xmlReader = new XMLReader();
52+
$xmlReader->open($file);
53+
$xmlReader->setParserProperty($xmlReader::VALIDATE, true);
54+
55+
return $xmlReader->isValid();
9156
}
9257
}

0 commit comments

Comments
 (0)