1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebDevEtc\BlogEtc\Services; |
4
|
|
|
|
5
|
|
|
use Auth; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Illuminate\Database\Eloquent\Collection; |
8
|
|
|
use Illuminate\Http\Response; |
9
|
|
|
use Laravelium\Feed\Feed; |
10
|
|
|
use WebDevEtc\BlogEtc\Models\Post; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class BlogEtcFeedService. |
14
|
|
|
*/ |
15
|
|
|
class FeedService |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var PostsService |
19
|
|
|
*/ |
20
|
|
|
private $postsService; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* FeedService constructor. |
24
|
|
|
*/ |
25
|
|
|
public function __construct(PostsService $postsService) |
26
|
|
|
{ |
27
|
|
|
$this->postsService = $postsService; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Build the Feed object and populate it with blog posts. |
32
|
|
|
*/ |
33
|
|
|
public function getFeed(Feed $feed, string $feedType): Response |
34
|
|
|
{ |
35
|
|
|
// RSS feed is cached. Admin/writer users might see different content, so |
36
|
|
|
// use a different cache for different users. |
37
|
|
|
|
38
|
|
|
// This should not be a problem unless your site has many logged in users. |
39
|
|
|
// (Use check(), as it is possible for user to be logged in without having an ID (depending on how the guard |
40
|
|
|
// is set up...) |
41
|
|
|
$userOrGuest = Auth::check() |
42
|
|
|
? 'logged-in-'.Auth::id() |
43
|
|
|
: 'guest'; |
44
|
|
|
|
45
|
|
|
$key = 'blogetc-'.$feedType.$userOrGuest; |
46
|
|
|
|
47
|
|
|
$feed->setCache( |
48
|
|
|
config('blogetc.rssfeed.cache_in_minutes', 60), |
49
|
|
|
$key |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
if (!$feed->isCached()) { |
53
|
|
|
$this->makeFreshFeed($feed); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $feed->render($feedType); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Create fresh feed by passing latest blog posts. |
61
|
|
|
* |
62
|
|
|
* @param $feed |
63
|
|
|
*/ |
64
|
|
|
protected function makeFreshFeed(Feed $feed): void |
65
|
|
|
{ |
66
|
|
|
$blogPosts = $this->postsService->rssItems(); |
67
|
|
|
|
68
|
|
|
$this->setupFeed( |
69
|
|
|
$feed, |
70
|
|
|
$this->pubDate($blogPosts) |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
/** @var Post $blogPost */ |
74
|
|
|
foreach ($blogPosts as $blogPost) { |
75
|
|
|
$feed->add( |
76
|
|
|
$blogPost->title, |
77
|
|
|
$blogPost->authorString(), |
78
|
|
|
$blogPost->url(), |
79
|
|
|
$blogPost->posted_at, |
80
|
|
|
$blogPost->short_description |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Basic set up of the Feed object. |
87
|
|
|
*/ |
88
|
|
|
protected function setupFeed(Feed $feed, Carbon $pubDate): Feed |
89
|
|
|
{ |
90
|
|
|
$feed->title = config('blogetc.rssfeed.title'); |
91
|
|
|
$feed->description = config('blogetc.rssfeed.description'); |
92
|
|
|
$feed->link = route('blogetc.index'); |
93
|
|
|
$feed->lang = config('blogetc.rssfeed.language'); |
94
|
|
|
$feed->setShortening(config('blogetc.rssfeed.should_shorten_text')); |
95
|
|
|
$feed->setTextLimit(config('blogetc.rssfeed.text_limit')); |
96
|
|
|
$feed->setDateFormat('carbon'); |
97
|
|
|
$feed->pubdate = $pubDate; |
98
|
|
|
|
99
|
|
|
return $feed; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Return the first post posted_at date, or if none exist then return today. |
104
|
|
|
*/ |
105
|
|
|
protected function pubDate(Collection $blogPosts): Carbon |
106
|
|
|
{ |
107
|
|
|
return $blogPosts->first() |
108
|
|
|
? $blogPosts->first()->posted_at |
109
|
|
|
: Carbon::now(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|