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

ArchiveController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 46
rs 10
c 1
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A index() 0 3 1
A monthlyArchive() 0 19 1
A yearlyArchive() 0 9 1
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