1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Albert221\Blog\Controller; |
4
|
|
|
|
5
|
|
|
use Albert221\Blog\Pagination\PaginatorBuilder; |
6
|
|
|
use Albert221\Blog\Repository\CategoryRepositoryInterface; |
7
|
|
|
use Albert221\Blog\Repository\PostRepositoryInterface; |
8
|
|
|
use Albert221\Blog\Repository\TagRepositoryInterface; |
9
|
|
|
use Albert221\Blog\Sidebar\Widget\HTML; |
10
|
|
|
use Albert221\Blog\Sidebar\Widget\RecentCategories; |
11
|
|
|
use Albert221\Blog\Sidebar\Widget\RecentPosts; |
12
|
|
|
use Albert221\Blog\Sidebar\Widget\TagCloud; |
13
|
|
|
use Albert221\Blog\Sidebar\WidgetManager; |
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
15
|
|
|
use Twig_Environment; |
16
|
|
|
use Zend\Diactoros\Request; |
17
|
|
|
|
18
|
|
|
class PostController extends AbstractController |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var PostRepositoryInterface Posts |
22
|
|
|
*/ |
23
|
|
|
protected $posts; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var CategoryRepositoryInterface Categories |
27
|
|
|
*/ |
28
|
|
|
protected $categories; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var PaginatorBuilder Paginator builder |
32
|
|
|
*/ |
33
|
|
|
protected $paginatorBuilder; |
34
|
|
|
|
35
|
|
|
public function __construct( |
36
|
|
|
PostRepositoryInterface $posts, |
37
|
|
|
CategoryRepositoryInterface $categories, |
38
|
|
|
TagRepositoryInterface $tags, |
39
|
|
|
PaginatorBuilder $paginatorBuilder, |
40
|
|
|
Twig_Environment $twig |
41
|
|
|
) { |
42
|
|
|
parent::__construct($twig); |
43
|
|
|
|
44
|
|
|
$this->posts = $posts; |
45
|
|
|
$this->categories = $categories; |
46
|
|
|
$this->paginatorBuilder = $paginatorBuilder; |
47
|
|
|
|
48
|
|
|
// TODO: Find a better place for everything below. |
49
|
|
|
|
50
|
|
|
$sidebarWidgetManager = new WidgetManager(); |
51
|
|
|
$sidebarWidgetManager->add(new RecentPosts($this->posts, $twig, 10)); |
52
|
|
|
$sidebarWidgetManager->add(new TagCloud($tags, $twig)); |
53
|
|
|
|
54
|
|
|
$footerWidgetManager = new WidgetManager(); |
55
|
|
|
$footerWidgetManager->add(new RecentPosts($this->posts, $twig, 5)); |
56
|
|
|
$footerWidgetManager->add(new RecentCategories($this->categories, $twig, 5)); |
57
|
|
|
$footerWidgetManager->add(new HTML('Polecane strony', '<ul> |
58
|
|
|
<li><a href="#">Lorem ipsum.</a></li> |
59
|
|
|
<li><a href="#">Amet, ipsam?</a></li> |
60
|
|
|
<li><a href="#">Animi, alias.</a></li> |
61
|
|
|
<li><a href="#">Sed, non.</a></li> |
62
|
|
|
<li><a href="#">Officiis, harum.</a></li> |
63
|
|
|
</ul>')); |
64
|
|
|
|
65
|
|
|
$this->twig->addGlobal('sidebarWidgets', $sidebarWidgetManager->getWidgets()); |
66
|
|
|
$this->twig->addGlobal('footerWidgets', $footerWidgetManager->getWidgets()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Route: / |
71
|
|
|
* |
72
|
|
|
* @param ServerRequestInterface $request |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function index(ServerRequestInterface $request) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$paginator = $this->paginatorBuilder->build($request, $this->posts->count()); |
79
|
|
|
|
80
|
|
|
$posts = $this->posts->paginated($paginator->getPage(), $paginator->getPerPage()); |
81
|
|
|
|
82
|
|
|
return $this->view('index.twig', [ |
83
|
|
|
'posts' => $posts, |
84
|
|
|
'paginator' => $paginator |
85
|
|
|
]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Route: /{slug} |
90
|
|
|
* |
91
|
|
|
* @param string $slug |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function post($slug) |
95
|
|
|
{ |
96
|
|
|
$post = $this->posts->bySlug($slug); |
97
|
|
|
|
98
|
|
|
return $this->view('post.twig', compact('post')); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Route: /kategoria/{slug} |
103
|
|
|
* |
104
|
|
|
* @param ServerRequestInterface $request |
105
|
|
|
* @param string $slug |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
public function category(ServerRequestInterface $request, $slug) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$paginator = $this->paginatorBuilder->build($request, $this->posts->byCategoryCount($slug)); |
111
|
|
|
|
112
|
|
|
$posts = $this->posts->byCategory($slug, $paginator->getPage(), $paginator->getPerPage()); |
113
|
|
|
|
114
|
|
|
return $this->view('index.twig', compact('posts', 'paginator')); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param ServerRequestInterface $request |
119
|
|
|
* @param string $slug |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
View Code Duplication |
public function tag(ServerRequestInterface $request, $slug) |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$paginator = $this->paginatorBuilder->build($request, $this->posts->byTagCount($slug)); |
125
|
|
|
|
126
|
|
|
$posts = $this->posts->byTag($slug, $paginator->getPage(), $paginator->getPerPage()); |
127
|
|
|
|
128
|
|
|
return $this->view('index.twig', compact('posts', 'paginator')); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
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.