Passed
Pull Request — master (#49)
by
unknown
11:39
created

BlogController::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
    protected function getId(): string
25
    {
26
        return 'blog';
27
    }
28
29
    public function index(Request $request, ORMInterface $orm, ArchiveRepository $archiveRepo): Response
30
    {
31
        /** @var PostRepository $postRepo */
32
        $postRepo = $orm->getRepository(Post::class);
33
        /** @var TagRepository $tagRepo */
34
        $tagRepo = $orm->getRepository(Tag::class);
35
36
        $pageNum = (int)$request->getAttribute('page', 1);
37
38
        $dataReader = $postRepo->findAllPreloaded();
39
40
        $paginator = (new OffsetPaginator($dataReader))
41
            ->withPageSize(self::POSTS_PER_PAGE)
42
            ->withCurrentPage($pageNum);
43
44
        $data = [
45
            'paginator' => $paginator,
46
            'archive' => $archiveRepo->getFullArchive()->withLimit(self::ARCHIVE_MONTHS_COUNT),
47
            'tags' => $tagRepo->getTagMentions(self::POPULAR_TAGS_COUNT),
48
        ];
49
        return $this->render('index', $data);
50
    }
51
}
52