Passed
Pull Request — master (#49)
by
unknown
11:29
created

ArchiveController::monthlyArchive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 38
rs 9.552
cc 1
nc 1
nop 4
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 App\Pagination\PaginationSet;
9
use Cycle\ORM\ORMInterface;
10
use Psr\Http\Message\ResponseInterface as Response;
11
use Psr\Http\Message\ServerRequestInterface as Request;
12
use Yiisoft\Data\Paginator\OffsetPaginator;
13
use Yiisoft\Router\UrlGeneratorInterface;
14
15
final class ArchiveController extends Controller
16
{
17
    private const POSTS_PER_PAGE = 3;
18
    private const POPULAR_TAGS_COUNT = 10;
19
20
    protected function getId(): string
21
    {
22
        return 'blog/archive';
23
    }
24
25
    public function index(ArchiveRepository $archiveRepo): Response
26
    {
27
        $output = $this->render(__FUNCTION__, [
28
            'archive' => $archiveRepo->getFullArchive(),
29
        ]);
30
31
        $response = $this->responseFactory->createResponse();
32
        $response->getBody()->write($output);
33
        return $response;
34
    }
35
36
    public function monthlyArchive(
37
        Request $request,
38
        ORMInterface $orm,
39
        UrlGeneratorInterface $urlGenerator,
40
        ArchiveRepository $archiveRepo
41
    ): Response {
42
        /** @var TagRepository $postRepo */
43
        $tagRepo = $orm->getRepository(Tag::class);
44
45
        $pageNum = (int)$request->getAttribute('page', 1);
46
        $year = $request->getAttribute('year', null);
47
        $month = $request->getAttribute('month', null);
48
49
        $dataReader = $archiveRepo->getMonthlyArchive($year, $month);
50
        $pageUrlGenerator = fn ($page) => $urlGenerator->generate(
51
            'blog/archive/month',
52
            ['year' => $year, 'month' => $month, 'page' => $page]
53
        );
54
55
        $paginationSet = new PaginationSet(
0 ignored issues
show
Deprecated Code introduced by
The class App\Pagination\PaginationSet has been deprecated: Should be replaced to a Pagination Widget ( Ignorable by Annotation )

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

55
        $paginationSet = /** @scrutinizer ignore-deprecated */ new PaginationSet(
Loading history...
56
            (new OffsetPaginator($dataReader))
57
                ->withPageSize(self::POSTS_PER_PAGE)
58
                ->withCurrentPage($pageNum),
59
            $pageUrlGenerator
60
        );
61
62
        $data = [
63
            'year' => $year,
64
            'month' => $month,
65
            'paginationSet' => $paginationSet,
66
            'archive' => $archiveRepo->getFullArchive()->withLimit(12),
67
            '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

67
            'tags' => $tagRepo->/** @scrutinizer ignore-call */ getTagMentions(self::POPULAR_TAGS_COUNT),
Loading history...
68
        ];
69
        $output = $this->render('monthly-archive', $data);
70
71
        $response = $this->responseFactory->createResponse();
72
        $response->getBody()->write($output);
73
        return $response;
74
    }
75
76
    public function yearlyArchive(
77
        Request $request,
78
        ORMInterface $orm,
0 ignored issues
show
Unused Code introduced by
The parameter $orm is not used and could be removed. ( Ignorable by Annotation )

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

78
        /** @scrutinizer ignore-unused */ ORMInterface $orm,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
        ArchiveRepository $archiveRepo
80
    ): Response {
81
        $year = $request->getAttribute('year', null);
82
83
        $data = [
84
            'year' => $year,
85
            'items' => $archiveRepo->getYearlyArchive($year),
86
        ];
87
        $output = $this->render('yearly-archive', $data);
88
89
        $response = $this->responseFactory->createResponse();
90
        $response->getBody()->write($output);
91
        return $response;
92
    }
93
}
94