1 | <?php |
||
22 | class FeedController extends Controller |
||
23 | { |
||
24 | /** |
||
25 | * Feed facade. |
||
26 | * |
||
27 | * @var Roumen\Feed\Facades\Feed |
||
28 | */ |
||
29 | private $feed; |
||
30 | |||
31 | /** |
||
32 | * Create a new feed controller instance. |
||
33 | * |
||
34 | * @return void |
||
35 | */ |
||
36 | public function __construct() |
||
37 | { |
||
38 | $this->feed = Feed::make(); |
||
39 | $this->feed->title = Setting::get('app_name'); |
||
40 | $this->feed->description = trans('gitamin.feed'); |
||
41 | $this->feed->link = Str::canonicalize(Setting::get('app_domain')); |
||
42 | $this->feed->setDateFormat('datetime'); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Generates an Atom feed of all issues. |
||
47 | * |
||
48 | * @param \Gitamin\Models\ProjectTeam|null $namespace |
||
49 | * |
||
50 | * @return \Illuminate\Http\Response |
||
51 | */ |
||
52 | public function atomAction(ProjectNamespace $namespace = null) |
||
53 | { |
||
54 | return $this->feedAction($namespace, false); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Generates a Rss feed of all issues. |
||
59 | * |
||
60 | * @param \Gitamin\Models\ProjectTeam|null $group |
||
61 | * |
||
62 | * @return \Illuminate\Http\Response |
||
63 | */ |
||
64 | public function rssAction(ProjectNamespace $namespace = null) |
||
65 | { |
||
66 | $this->feed->lang = Setting::get('app_locale'); |
||
67 | |||
68 | return $this->feedAction($namespace, true); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Generates a feed of all issues. |
||
73 | * |
||
74 | * @param \Gitamin\Models\ProjectTeam|null $namespace |
||
75 | * @param bool $isRss |
||
76 | * |
||
77 | * @return \Illuminate\Http\Response |
||
78 | */ |
||
79 | private function feedAction(ProjectNamespace &$namespace, $isRss) |
||
80 | { |
||
81 | if ($namespace->exists) { |
||
82 | $namespace->projects->map(function ($project) { |
||
83 | $project->issues()->visible()->orderBy('created_at', 'desc')->get()->map(function ($issue) use ($isRss) { |
||
84 | $this->feedAddItem($issue, $isRss); |
||
85 | }); |
||
86 | }); |
||
87 | } else { |
||
88 | Issue::visible()->orderBy('created_at', 'desc')->get()->map(function ($issue) use ($isRss) { |
||
89 | $this->feedAddItem($issue, $isRss); |
||
90 | }); |
||
91 | } |
||
92 | |||
93 | return $this->feed->render($isRss ? 'rss' : 'atom'); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Adds an item to the feed. |
||
98 | * |
||
99 | * @param \Gitamin\Models\Issue $issue |
||
100 | * @param bool $isRss |
||
101 | */ |
||
102 | private function feedAddItem($issue, $isRss) |
||
103 | { |
||
104 | $this->feed->add( |
||
105 | $issue->name, |
||
106 | Setting::get('app_name'), |
||
107 | Str::canonicalize(route('issue', ['id' => $issue->id])), |
||
108 | $isRss ? $issue->created_at->toRssString() : $issue->created_at->toAtomString(), |
||
109 | $isRss ? $issue->message : Markdown::convertToHtml($issue->message) |
||
110 | ); |
||
111 | } |
||
112 | |||
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.