1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Gitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) 2015-2016 The Gitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Gitamin\Http\Controllers; |
13
|
|
|
|
14
|
|
|
use Gitamin\Facades\Setting; |
15
|
|
|
use Gitamin\Models\Issue; |
16
|
|
|
use Gitamin\Models\ProjectTeam; |
17
|
|
|
use Illuminate\Routing\Controller; |
18
|
|
|
use Illuminate\Support\Str; |
19
|
|
|
use Roumen\Feed\Facades\Feed; |
20
|
|
|
|
21
|
|
|
class FeedController extends Controller |
22
|
|
|
{ |
23
|
|
|
protected $feed; |
24
|
|
|
|
25
|
|
|
public function __construct() |
26
|
|
|
{ |
27
|
|
|
$this->feed = Feed::make(); |
28
|
|
|
$this->feed->title = Setting::get('app_name'); |
29
|
|
|
$this->feed->description = trans('gitamin.feed'); |
30
|
|
|
$this->feed->link = Str::canonicalize(Setting::get('app_domain')); |
31
|
|
|
$this->feed->setDateFormat('datetime'); |
32
|
|
|
} |
33
|
|
|
/** |
34
|
|
|
* Generates a Rss feed of all issues. |
35
|
|
|
* |
36
|
|
|
* @param \Gitamin\Models\ProjectTeam|null $group |
37
|
|
|
* |
38
|
|
|
* @return \Illuminate\Http\Response |
39
|
|
|
*/ |
40
|
|
|
public function rssAction(ProjectTeam $group = null) |
41
|
|
|
{ |
42
|
|
|
|
43
|
|
|
$this->feed->lang = Setting::get('app_locale'); |
44
|
|
|
|
45
|
|
View Code Duplication |
if ($group->exists) { |
|
|
|
|
46
|
|
|
$group->projects->map(function ($project) { |
|
|
|
|
47
|
|
|
$project->issues()->visible()->orderBy('created_at', 'desc')->get()->map(function ($issue) { |
48
|
|
|
$this->feedAddItem($issue); |
49
|
|
|
}); |
50
|
|
|
}); |
51
|
|
|
} else { |
52
|
|
|
Issue::visible()->orderBy('created_at', 'desc')->get()->map(function ($issue) { |
|
|
|
|
53
|
|
|
$this->feedAddItem($issue); |
54
|
|
|
}); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->feed->render('rss'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Generates an Atom feed of all issues. |
62
|
|
|
* |
63
|
|
|
* @param \Gitamin\Models\ProjectTeam|null $group |
64
|
|
|
* |
65
|
|
|
* @return \Illuminate\Http\Response |
66
|
|
|
*/ |
67
|
|
|
public function atomAction(ProjectTeam $group = null) |
68
|
|
|
{ |
69
|
|
View Code Duplication |
if ($group->exists) { |
|
|
|
|
70
|
|
|
$group->projects->map(function ($project) { |
|
|
|
|
71
|
|
|
$project->issues()->visible()->orderBy('created_at', 'desc')->get()->map(function ($issue) { |
72
|
|
|
$this->feedAddItem($issue, false); |
73
|
|
|
}); |
74
|
|
|
}); |
75
|
|
|
} else { |
76
|
|
|
Issue::visible()->orderBy('created_at', 'desc')->get()->map(function ($issue) { |
|
|
|
|
77
|
|
|
$this->feedAddItem($issue, false); |
78
|
|
|
}); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->feed->render('atom'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Adds an item to the feed. |
86
|
|
|
* |
87
|
|
|
* @param \Gitamin\Models\Issue $issue |
88
|
|
|
* @param bool $isRss |
89
|
|
|
*/ |
90
|
|
|
private function feedAddItem($issue, $isRss = true) |
91
|
|
|
{ |
92
|
|
|
$this->feed->add( |
93
|
|
|
$issue->name, |
|
|
|
|
94
|
|
|
Setting::get('app_name'), |
95
|
|
|
Str::canonicalize(route('issue', ['id' => $issue->id])), |
|
|
|
|
96
|
|
|
$isRss ? $issue->created_at->toRssString() : $issue->created_at->toAtomString(), |
|
|
|
|
97
|
|
|
$isRss ? $issue->message : Markdown::convertToHtml($issue->message) |
|
|
|
|
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.