Passed
Pull Request — master (#49)
by
unknown
13:21
created

ArchiveController::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
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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
        $output = $this->render(__FUNCTION__, [
26
            'archive' => $archiveRepo->getFullArchive(),
27
        ]);
28
29
        $response = $this->responseFactory->createResponse();
30
        $response->getBody()->write($output);
31
        return $response;
32
    }
33
34
    public function monthlyArchive(Request $request, ORMInterface $orm, ArchiveRepository $archiveRepo): Response
35
    {
36
        /** @var TagRepository $postRepo */
37
        $tagRepo = $orm->getRepository(Tag::class);
38
39
        $pageNum = (int)$request->getAttribute('page', 1);
40
        $year = $request->getAttribute('year', null);
41
        $month = $request->getAttribute('month', null);
42
43
        $dataReader = $archiveRepo->getMonthlyArchive($year, $month);
44
        $paginator = (new OffsetPaginator($dataReader))
45
            ->withPageSize(self::POSTS_PER_PAGE)
46
            ->withCurrentPage($pageNum);
47
48
        $data = [
49
            'year' => $year,
50
            'month' => $month,
51
            'paginator' => $paginator,
52
            'archive' => $archiveRepo->getFullArchive()->withLimit(12),
53
            '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

53
            'tags' => $tagRepo->/** @scrutinizer ignore-call */ getTagMentions(self::POPULAR_TAGS_COUNT),
Loading history...
54
        ];
55
        $output = $this->render('monthly-archive', $data);
56
57
        $response = $this->responseFactory->createResponse();
58
        $response->getBody()->write($output);
59
        return $response;
60
    }
61
62
    public function yearlyArchive(Request $request, ArchiveRepository $archiveRepo): Response
63
    {
64
        $year = $request->getAttribute('year', null);
65
66
        $data = [
67
            'year' => $year,
68
            'items' => $archiveRepo->getYearlyArchive($year),
69
        ];
70
        $output = $this->render('yearly-archive', $data);
71
72
        $response = $this->responseFactory->createResponse();
73
        $response->getBody()->write($output);
74
        return $response;
75
    }
76
}
77