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

ArchiveController::index()   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 1
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
use Yiisoft\Router\UrlGeneratorInterface;
13
14
final class ArchiveController extends Controller
15
{
16
    private const POSTS_PER_PAGE = 3;
17
    private const POPULAR_TAGS_COUNT = 10;
18
19
    protected function getId(): string
20
    {
21
        return 'blog/archive';
22
    }
23
24
    public function index(ArchiveRepository $archiveRepo): Response
25
    {
26
        $output = $this->render(__FUNCTION__, [
27
            'archive' => $archiveRepo->getFullArchive(),
28
        ]);
29
30
        $response = $this->responseFactory->createResponse();
31
        $response->getBody()->write($output);
32
        return $response;
33
    }
34
35
    public function monthlyArchive(
36
        Request $request,
37
        ORMInterface $orm,
38
        UrlGeneratorInterface $urlGenerator,
0 ignored issues
show
Unused Code introduced by
The parameter $urlGenerator 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

38
        /** @scrutinizer ignore-unused */ UrlGeneratorInterface $urlGenerator,

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...
39
        ArchiveRepository $archiveRepo
40
    ): Response {
41
        /** @var TagRepository $postRepo */
42
        $tagRepo = $orm->getRepository(Tag::class);
43
44
        $pageNum = (int)$request->getAttribute('page', 1);
45
        $year = $request->getAttribute('year', null);
46
        $month = $request->getAttribute('month', null);
47
48
        $dataReader = $archiveRepo->getMonthlyArchive($year, $month);
49
        $paginator = (new OffsetPaginator($dataReader))
50
            ->withPageSize(self::POSTS_PER_PAGE)
51
            ->withCurrentPage($pageNum);
52
53
        $data = [
54
            'year' => $year,
55
            'month' => $month,
56
            'paginator' => $paginator,
57
            'archive' => $archiveRepo->getFullArchive()->withLimit(12),
58
            '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

58
            'tags' => $tagRepo->/** @scrutinizer ignore-call */ getTagMentions(self::POPULAR_TAGS_COUNT),
Loading history...
59
        ];
60
        $output = $this->render('monthly-archive', $data);
61
62
        $response = $this->responseFactory->createResponse();
63
        $response->getBody()->write($output);
64
        return $response;
65
    }
66
67
    public function yearlyArchive(
68
        Request $request,
69
        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

69
        /** @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...
70
        ArchiveRepository $archiveRepo
71
    ): Response {
72
        $year = $request->getAttribute('year', null);
73
74
        $data = [
75
            'year' => $year,
76
            'items' => $archiveRepo->getYearlyArchive($year),
77
        ];
78
        $output = $this->render('yearly-archive', $data);
79
80
        $response = $this->responseFactory->createResponse();
81
        $response->getBody()->write($output);
82
        return $response;
83
    }
84
}
85