Completed
Push — master ( 3a917b...8faa80 )
by Alexander
15s queued 12s
created

ArchiveController::monthlyArchive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 22
rs 9.7666
cc 1
nc 1
nop 3
1
<?php
2
3
namespace App\Blog\Archive;
4
5
use App\Controller;
6
use App\Blog\Entity\Tag;
7
use App\Blog\Tag\TagRepository;
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 extends Controller
14
{
15
    private const POSTS_PER_PAGE = 3;
16
    private const POPULAR_TAGS_COUNT = 10;
17
18
    protected function getId(): string
19
    {
20
        return 'blog/archive';
21
    }
22
23
    public function index(ArchiveRepository $archiveRepo): Response
24
    {
25
        return $this->render('index', ['archive' => $archiveRepo->getFullArchive()]);
26
    }
27
28
    public function monthlyArchive(Request $request, ORMInterface $orm, ArchiveRepository $archiveRepo): Response
29
    {
30
        /** @var TagRepository $postRepo */
31
        $tagRepo = $orm->getRepository(Tag::class);
32
33
        $pageNum = (int)$request->getAttribute('page', 1);
34
        $year = $request->getAttribute('year', null);
35
        $month = $request->getAttribute('month', null);
36
37
        $dataReader = $archiveRepo->getMonthlyArchive($year, $month);
38
        $paginator = (new OffsetPaginator($dataReader))
39
            ->withPageSize(self::POSTS_PER_PAGE)
40
            ->withCurrentPage($pageNum);
41
42
        $data = [
43
            'year' => $year,
44
            'month' => $month,
45
            'paginator' => $paginator,
46
            'archive' => $archiveRepo->getFullArchive()->withLimit(12),
47
            'tags' => $tagRepo->getTagMentions(self::POPULAR_TAGS_COUNT),
0 ignored issues
show
Bug introduced by
The method getTagMentions() does not exist on Cycle\ORM\RepositoryInterface. It seems like you code against a sub-type of Cycle\ORM\RepositoryInterface such as App\Blog\Tag\TagRepository. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
            'tags' => $tagRepo->/** @scrutinizer ignore-call */ getTagMentions(self::POPULAR_TAGS_COUNT),
Loading history...
48
        ];
49
        return $this->render('monthly-archive', $data);
50
    }
51
52
    public function yearlyArchive(Request $request, ArchiveRepository $archiveRepo): Response
53
    {
54
        $year = $request->getAttribute('year', null);
55
56
        $data = [
57
            'year' => $year,
58
            'items' => $archiveRepo->getYearlyArchive($year),
59
        ];
60
        return $this->render('yearly-archive', $data);
61
    }
62
}
63