1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Albert221\Blog\Controller; |
4
|
|
|
|
5
|
|
|
use Albert221\Blog\Pagination\PaginatorBuilder; |
6
|
|
|
use Albert221\Blog\Repository\PostRepositoryInterface; |
7
|
|
|
use League\Route\Http\Exception\NotFoundException; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
9
|
|
|
use Zend\Diactoros\Request; |
10
|
|
|
|
11
|
|
|
class PostController extends AbstractWidgetController |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var PostRepositoryInterface Posts |
15
|
|
|
*/ |
16
|
|
|
protected $posts; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var PaginatorBuilder Paginator builder |
20
|
|
|
*/ |
21
|
|
|
protected $paginatorBuilder; |
22
|
|
|
|
23
|
|
|
public function __construct( |
24
|
|
|
PostRepositoryInterface $posts, |
25
|
|
|
PaginatorBuilder $paginatorBuilder |
26
|
|
|
) { |
27
|
|
|
$this->posts = $posts; |
28
|
|
|
$this->paginatorBuilder = $paginatorBuilder; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* / |
33
|
|
|
* |
34
|
|
|
* @param ServerRequestInterface $request |
35
|
|
|
* |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
public function index(ServerRequestInterface $request) |
39
|
|
|
{ |
40
|
|
|
$this->provideWidgets(); |
41
|
|
|
|
42
|
|
|
if (isset($request->getQueryParams()['q'])) { |
43
|
|
|
return $this->search($request, $request->getQueryParams()['q']); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$paginator = $this->paginatorBuilder->build($request, $this->posts->count()); |
47
|
|
|
|
48
|
|
|
$posts = $this->posts->paginated($paginator->getPage(), $paginator->getPerPage()); |
49
|
|
|
|
50
|
|
|
return $this->view('index.twig', compact('posts', 'paginator')); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* /{slug} |
55
|
|
|
* |
56
|
|
|
* @param string $slug |
57
|
|
|
* @return string |
58
|
|
|
* @throws NotFoundException when post does not exist |
59
|
|
|
*/ |
60
|
|
|
public function post($slug) |
61
|
|
|
{ |
62
|
|
|
$this->provideWidgets(); |
63
|
|
|
|
64
|
|
|
$post = $this->posts->bySlug($slug); |
65
|
|
|
|
66
|
|
|
if (!$post) { |
67
|
|
|
throw new NotFoundException('Post has not been found'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->view('post.twig', compact('post')); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* /kategoria/{slug} |
75
|
|
|
* |
76
|
|
|
* @param ServerRequestInterface $request |
77
|
|
|
* @param string $slug |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function category(ServerRequestInterface $request, $slug) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$this->provideWidgets(); |
83
|
|
|
|
84
|
|
|
$paginator = $this->paginatorBuilder->build($request, $this->posts->byCategoryCount($slug)); |
85
|
|
|
|
86
|
|
|
$posts = $this->posts->byCategory($slug, $paginator->getPage(), $paginator->getPerPage()); |
87
|
|
|
|
88
|
|
|
return $this->view('index.twig', [ |
89
|
|
|
'posts' => $posts, |
90
|
|
|
'paginator' => $paginator, |
91
|
|
|
'title' => 'Posty w kategorii \''.$posts[0]->getCategory()->getName().'\'' |
92
|
|
|
]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* /tag/{slug} |
97
|
|
|
* |
98
|
|
|
* @param ServerRequestInterface $request |
99
|
|
|
* @param string $slug |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function tag(ServerRequestInterface $request, $slug) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$this->provideWidgets(); |
105
|
|
|
|
106
|
|
|
$paginator = $this->paginatorBuilder->build($request, $this->posts->byTagCount($slug)); |
107
|
|
|
|
108
|
|
|
$posts = $this->posts->byTag($slug, $paginator->getPage(), $paginator->getPerPage()); |
109
|
|
|
|
110
|
|
|
return $this->view('index.twig', [ |
111
|
|
|
'posts' => $posts, |
112
|
|
|
'paginator' => $paginator, |
113
|
|
|
'title' => 'Posty otagowane \''.$slug.'\'' |
114
|
|
|
]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* /?q={term} |
119
|
|
|
* |
120
|
|
|
* @param ServerRequestInterface $request |
121
|
|
|
* @param string $term |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function search(ServerRequestInterface $request, $term) |
125
|
|
|
{ |
126
|
|
|
$paginator = $this->paginatorBuilder->build($request, $this->posts->searchCount($term)); |
127
|
|
|
|
128
|
|
|
$posts = $this->posts->search($term, $paginator->getPage(), $paginator->getPerPage()); |
129
|
|
|
|
130
|
|
|
return $this->view('index.twig', [ |
131
|
|
|
'posts' => $posts, |
132
|
|
|
'paginator' => $paginator, |
133
|
|
|
'title' => 'Wyniki wyszukiwania dla \''.$term.'\'', |
134
|
|
|
'searchTerm' => $term |
135
|
|
|
]); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
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.