Passed
Push — master ( ba613a...1f1f0a )
by Alexander
14:26
created

ArchiveController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Blog\Archive;
4
5
use App\Blog\Tag\TagRepository;
6
use App\ViewRenderer;
7
use Psr\Http\Message\ResponseInterface as Response;
8
use Psr\Http\Message\ServerRequestInterface as Request;
9
use Yiisoft\Data\Paginator\OffsetPaginator;
10
11
final class ArchiveController
12
{
13
    private const POSTS_PER_PAGE = 3;
14
    private const POPULAR_TAGS_COUNT = 10;
15
    private ViewRenderer $viewRenderer;
16
17
    public function __construct(ViewRenderer $viewRenderer)
18
    {
19
        $this->viewRenderer = $viewRenderer->withControllerName('blog/archive');
20
    }
21
22
    public function index(ArchiveRepository $archiveRepo): Response
23
    {
24
        return $this->viewRenderer->render('index', ['archive' => $archiveRepo->getFullArchive()]);
25
    }
26
27
    public function monthlyArchive(Request $request, TagRepository $tagRepository, ArchiveRepository $archiveRepo): Response
28
    {
29
        $pageNum = (int)$request->getAttribute('page', 1);
30
        $year = $request->getAttribute('year', null);
31
        $month = $request->getAttribute('month', null);
32
33
        $dataReader = $archiveRepo->getMonthlyArchive($year, $month);
34
        $paginator = (new OffsetPaginator($dataReader))
35
            ->withPageSize(self::POSTS_PER_PAGE)
36
            ->withCurrentPage($pageNum);
37
38
        $data = [
39
            'year' => $year,
40
            'month' => $month,
41
            'paginator' => $paginator,
42
            'archive' => $archiveRepo->getFullArchive()->withLimit(12),
43
            'tags' => $tagRepository->getTagMentions(self::POPULAR_TAGS_COUNT),
44
        ];
45
        return $this->viewRenderer->render('monthly-archive', $data);
46
    }
47
48
    public function yearlyArchive(Request $request, ArchiveRepository $archiveRepo): Response
49
    {
50
        $year = $request->getAttribute('year', null);
51
52
        $data = [
53
            'year' => $year,
54
            'items' => $archiveRepo->getYearlyArchive($year),
55
        ];
56
        return $this->viewRenderer->render('yearly-archive', $data);
57
    }
58
}
59