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

ArchiveController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 35
c 1
b 0
f 0
dl 0
loc 62
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 9 1
A getId() 0 3 1
A monthlyArchive() 0 26 1
A yearlyArchive() 0 13 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
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('index', [
26
            'archive' => $archiveRepo->getFullArchive(),
27
        ]);
28
29
        $response = $this->responseFactory->createResponse();
30
        $response->getBody()->write($output);
0 ignored issues
show
Bug introduced by
$output of type Psr\Http\Message\ResponseInterface is incompatible with the type string expected by parameter $string of Psr\Http\Message\StreamInterface::write(). ( Ignorable by Annotation )

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

30
        $response->getBody()->write(/** @scrutinizer ignore-type */ $output);
Loading history...
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);
0 ignored issues
show
Bug introduced by
$output of type Psr\Http\Message\ResponseInterface is incompatible with the type string expected by parameter $string of Psr\Http\Message\StreamInterface::write(). ( Ignorable by Annotation )

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

58
        $response->getBody()->write(/** @scrutinizer ignore-type */ $output);
Loading history...
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);
0 ignored issues
show
Bug introduced by
$output of type Psr\Http\Message\ResponseInterface is incompatible with the type string expected by parameter $string of Psr\Http\Message\StreamInterface::write(). ( Ignorable by Annotation )

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

73
        $response->getBody()->write(/** @scrutinizer ignore-type */ $output);
Loading history...
74
        return $response;
75
    }
76
}
77