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