Passed
Pull Request — master (#115)
by Dmitriy
23:50 queued 08:56
created

ArchiveController::yearlyArchive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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