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