1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Blog; |
6
|
|
|
|
7
|
|
|
use App\Blog\Archive\ArchiveRepository; |
8
|
|
|
use App\Blog\Post\PostRepository; |
9
|
|
|
use App\Blog\Tag\TagRepository; |
10
|
|
|
use App\ViewRenderer; |
11
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
12
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
13
|
|
|
use Yiisoft\Data\Paginator\OffsetPaginator; |
14
|
|
|
|
15
|
|
|
final class BlogController |
16
|
|
|
{ |
17
|
|
|
private const POSTS_PER_PAGE = 3; |
18
|
|
|
private const POPULAR_TAGS_COUNT = 10; |
19
|
|
|
private const ARCHIVE_MONTHS_COUNT = 12; |
20
|
|
|
|
21
|
|
|
private ViewRenderer $viewRenderer; |
22
|
|
|
|
23
|
|
|
public function __construct(ViewRenderer $viewRenderer) |
24
|
|
|
{ |
25
|
|
|
$this->viewRenderer = $viewRenderer->withControllerName('blog'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function index( |
29
|
|
|
Request $request, |
30
|
|
|
PostRepository $postRepository, |
31
|
|
|
TagRepository $tagRepository, |
32
|
|
|
ArchiveRepository $archiveRepo |
33
|
|
|
): Response { |
34
|
|
|
$pageNum = (int)$request->getAttribute('page', 1); |
35
|
|
|
$dataReader = $postRepository->findAllPreloaded(); |
36
|
|
|
$paginator = (new OffsetPaginator($dataReader)) |
37
|
|
|
->withPageSize(self::POSTS_PER_PAGE) |
38
|
|
|
->withCurrentPage($pageNum); |
39
|
|
|
|
40
|
|
|
$data = [ |
41
|
|
|
'paginator' => $paginator, |
42
|
|
|
'archive' => $archiveRepo->getFullArchive()->withLimit(self::ARCHIVE_MONTHS_COUNT), |
43
|
|
|
'tags' => $tagRepository->getTagMentions(self::POPULAR_TAGS_COUNT), |
44
|
|
|
]; |
45
|
|
|
return $this->viewRenderer->render('index', $data); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|