|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Mark |
|
5
|
|
|
* Date: 20/05/2016 |
|
6
|
|
|
* Time: 22:27. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace App\Plugins\Articles; |
|
10
|
|
|
|
|
11
|
|
|
use App\Classes\Interfaces\Sitemap; |
|
12
|
|
|
use App\Classes\SitemapGenerator; |
|
13
|
|
|
use App\Model\Article; |
|
14
|
|
|
use Illuminate\Http\Request; |
|
15
|
|
|
use Illuminate\View\Factory as View; |
|
16
|
|
|
use App\Classes\Library\PageLoader\Frontpage; |
|
17
|
|
|
use App\Classes\Repositories\ArticleRepository; |
|
18
|
|
|
use App\Classes\Repositories\MenuRepository; |
|
19
|
|
|
use App\Classes\Repositories\PageRepository; |
|
20
|
|
|
use App\Model\Page; |
|
21
|
|
|
use App\Plugins\PluginEngine; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class UserController. |
|
25
|
|
|
*/ |
|
26
|
|
|
class FrontendController extends PluginEngine implements Sitemap |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var View |
|
31
|
|
|
*/ |
|
32
|
|
|
private $view; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var Page |
|
36
|
|
|
*/ |
|
37
|
|
|
private $currentPage; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* PageController constructor. |
|
41
|
|
|
* @param PageRepository $pages |
|
42
|
|
|
* @param View $view |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(PageRepository $pages, View $view) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->view = $view; |
|
47
|
|
|
|
|
48
|
|
|
$this->currentPage = $pages->wherePlugin('Articles'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return mixed |
|
53
|
|
|
*/ |
|
54
|
|
|
private function navigationData() |
|
55
|
|
|
{ |
|
56
|
|
|
return app(MenuRepository::class)->allParentsWithChildren(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Usually the place for listing all the articles. (index) |
|
61
|
|
|
* |
|
62
|
|
|
* @param ArticleRepository $repository |
|
63
|
|
|
* @return \Illuminate\Http\Response |
|
64
|
|
|
*/ |
|
65
|
|
|
public function index(ArticleRepository $repository) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->view->share('articles', $repository->paginateLatest(7)); |
|
68
|
|
|
|
|
69
|
|
|
return $this->articleFrontPage(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function article(ArticleRepository $repository, string $slug) |
|
73
|
|
|
{ |
|
74
|
|
|
/** @var Article $article */ |
|
75
|
|
|
$article = $repository->collectArticle($slug); |
|
76
|
|
|
|
|
77
|
|
|
$article->incrementView(1); |
|
78
|
|
|
|
|
79
|
|
|
$article->save(); |
|
80
|
|
|
|
|
81
|
|
|
$this->view->share('articles', collect([0 => $article])); |
|
82
|
|
|
|
|
83
|
|
|
return $this->articleFrontPage(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function search(ArticleRepository $repository, Request $request) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->view->share('articles', $repository->searchThenPaginate($request->get('search'))); |
|
89
|
|
|
|
|
90
|
|
|
return $this->articleFrontPage(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function category(ArticleRepository $repository, int $id) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->view->share('articles', $repository->whereCategoryId($id)); |
|
96
|
|
|
|
|
97
|
|
|
return $this->articleFrontPage(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function creator(ArticleRepository $repository, int $id) |
|
101
|
|
|
{ |
|
102
|
|
|
$this->view->share('articles', $repository->whereCreatorId($id)); |
|
103
|
|
|
|
|
104
|
|
|
return $this->articleFrontPage(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
private function articleFrontPage() |
|
108
|
|
|
{ |
|
109
|
|
|
return (new Frontpage($this->currentPage, $this->navigationData()))->publish('articles', false, 200); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* The sitemap function allows plugins to quickly and effectively |
|
114
|
|
|
* show their content for search engines in a modular way. |
|
115
|
|
|
* |
|
116
|
|
|
* @param SitemapGenerator $sitemap |
|
117
|
|
|
* @return SitemapGenerator |
|
118
|
|
|
*/ |
|
119
|
|
View Code Duplication |
public function sitemap(SitemapGenerator $sitemap) |
|
|
|
|
|
|
120
|
|
|
{ |
|
121
|
|
|
/** @var ArticleRepository $repository */ |
|
122
|
|
|
$repository = app(ArticleRepository::class); |
|
123
|
|
|
|
|
124
|
|
|
/** @var Article $article */ |
|
125
|
|
|
foreach ($repository->whereSitemappable() as $article) { |
|
126
|
|
|
$sitemap->store(url($article->route()), $article->updated_at, 'weekly', '1.0'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $sitemap; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
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.