Passed
Pull Request — master (#75)
by
unknown
10:29
created

ArchiveController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 3 1
A index() 0 8 1
A monthlyArchive() 0 22 1
A yearlyArchive() 0 9 1
A custom() 0 5 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 App\Stream\Value\DataResponseProvider;
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
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)
25
    {
26
        return (new DataResponseProvider(['archive' => $archiveRepo->getFullArchive()->read()]))
27
            ->setHeaders(['X-Action' => __METHOD__])
28
            ->setFormat(null, [
29
                'viewPath' => $this->getViewPath(),
30
                'view'     => 'index',
31
                'layout'   => '@views/layout/main.php',
32
            ]);
33
        // return $this->render('index', ['archive' => $archiveRepo->getFullArchive()]);
34
    }
35
36
    public function monthlyArchive(Request $request, ORMInterface $orm, ArchiveRepository $archiveRepo): Response
37
    {
38
        /** @var TagRepository $postRepo */
39
        $tagRepo = $orm->getRepository(Tag::class);
40
41
        $pageNum = (int)$request->getAttribute('page', 1);
42
        $year = $request->getAttribute('year', null);
43
        $month = $request->getAttribute('month', null);
44
45
        $dataReader = $archiveRepo->getMonthlyArchive($year, $month);
46
        $paginator = (new OffsetPaginator($dataReader))
47
            ->withPageSize(self::POSTS_PER_PAGE)
48
            ->withCurrentPage($pageNum);
49
50
        $data = [
51
            'year' => $year,
52
            'month' => $month,
53
            'paginator' => $paginator,
54
            'archive' => $archiveRepo->getFullArchive()->withLimit(12),
55
            '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

55
            'tags' => $tagRepo->/** @scrutinizer ignore-call */ getTagMentions(self::POPULAR_TAGS_COUNT),
Loading history...
56
        ];
57
        return $this->render('monthly-archive', $data);
58
    }
59
60
    public function yearlyArchive(Request $request, ArchiveRepository $archiveRepo): Response
61
    {
62
        $year = $request->getAttribute('year', null);
63
64
        $data = [
65
            'year' => $year,
66
            'items' => $archiveRepo->getYearlyArchive($year),
67
        ];
68
        return $this->render('yearly-archive', $data);
69
    }
70
71
    public function custom()
72
    {
73
        return (new DataResponseProvider(['hello', 'world']))
74
            ->setHeaders(['My-Name' => 'Trololosha'])
75
            ->setCode(500);
76
    }
77
}
78