Passed
Pull Request — master (#49)
by
unknown
15:36
created

BlogController::tag()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 21
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 35
rs 9.584
1
<?php
2
3
namespace App\Controller;
4
5
use App\Controller;
6
use App\Entity\Post;
7
use App\Entity\Tag;
8
use App\Repository\PostRepository;
9
use Cycle\ORM\ORMInterface;
10
use Psr\Http\Message\ResponseInterface as Response;
11
use Psr\Http\Message\ServerRequestInterface as Request;
12
use Yiisoft\Router\UrlGeneratorInterface;
13
14
class BlogController 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';
22
    }
23
24
    public function index(
25
        Request $request,
26
        ORMInterface $orm,
27
        UrlGeneratorInterface $urlGenerator
28
    ): Response {
29
        /** @var PostRepository $postRepo */
30
        $postRepo = $orm->getRepository(Post::class);
31
        $tagRepo = $orm->getRepository(Tag::class);
32
33
        $pageNum = (int)$request->getAttribute('page', 1);
34
        $year = $request->getAttribute('year', null);
35
        $month = $request->getAttribute('month', null);
36
        $isArchive = $year !== null && $month !== null;
37
38
        $paginator = $isArchive
39
            ? $postRepo->findArchivedPublic($year, $month)
40
                       ->withTokenGenerator(fn ($page) => $urlGenerator->generate(
41
                           'blog/archive',
42
                           ['year' => $year, 'month' => $month, 'page' => $page]
43
                       ))
44
            : $postRepo->findLastPublic()
45
                       ->withTokenGenerator(fn ($page) => $urlGenerator->generate('blog/index', ['page' => $page]));
46
47
        $paginator = $paginator
48
            ->withPageSize(self::POSTS_PER_PAGE)
49
            ->withCurrentPage($pageNum);
50
51
        $data = [
52
            'paginator' => $paginator,
53
            'archive' => $postRepo->getArchive(),
54
            '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\Repository\TagRepository. ( Ignorable by Annotation )

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

54
            'tags' => $tagRepo->/** @scrutinizer ignore-call */ getTagMentions(self::POPULAR_TAGS_COUNT),
Loading history...
55
        ];
56
        $output = $this->render('index', $data);
57
58
        $response = $this->responseFactory->createResponse();
59
        $response->getBody()->write($output);
60
        return $response;
61
    }
62
63
    public function page(Request $request, ORMInterface $orm): Response
64
    {
65
        $postRepo = $orm->getRepository(Post::class);
66
        $slug = $request->getAttribute('slug', null);
67
68
        $item = $postRepo->fullPostPage($slug, $this->user->getId());
0 ignored issues
show
Bug introduced by
The method fullPostPage() does not exist on Cycle\ORM\RepositoryInterface. It seems like you code against a sub-type of Cycle\ORM\RepositoryInterface such as App\Repository\PostRepository. ( Ignorable by Annotation )

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

68
        /** @scrutinizer ignore-call */ 
69
        $item = $postRepo->fullPostPage($slug, $this->user->getId());
Loading history...
69
        if ($item === null) {
70
            return $this->responseFactory->createResponse(404);
71
        }
72
73
        $data = [
74
            'item' => $item,
75
        ];
76
        $output = $this->render('post', $data);
77
78
        $response = $this->responseFactory->createResponse();
79
        $response->getBody()->write($output);
80
        return $response;
81
    }
82
83
    public function tag(
84
        Request $request,
85
        ORMInterface $orm,
86
        UrlGeneratorInterface $urlGenerator
87
    ): Response {
88
        $tagRepo = $orm->getRepository(Tag::class);
89
        /** @var PostRepository $postRepo */
90
        $postRepo = $orm->getRepository(Post::class);
91
        $label = $request->getAttribute('label', null);
92
        $pageNum = (int)$request->getAttribute('page', 1);
93
94
        $item = $tagRepo->findByLabel($label);
0 ignored issues
show
Bug introduced by
The method findByLabel() does not exist on Cycle\ORM\RepositoryInterface. It seems like you code against a sub-type of Cycle\ORM\RepositoryInterface such as App\Repository\TagRepository. ( Ignorable by Annotation )

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

94
        /** @scrutinizer ignore-call */ 
95
        $item = $tagRepo->findByLabel($label);
Loading history...
95
96
        if ($item === null) {
97
            return $this->responseFactory->createResponse(404);
98
        }
99
        // preloading of posts
100
        $paginator = $postRepo
101
            ->findByTag($item->getId())
102
            ->withTokenGenerator(fn ($page) => $urlGenerator->generate(
103
                'blog/tag',
104
                ['label' => $label, 'page' => $page]
105
            ))
106
            ->withPageSize(self::POSTS_PER_PAGE)
107
            ->withCurrentPage($pageNum);
108
109
        $data = [
110
            'item' => $item,
111
            'paginator' => $paginator,
112
        ];
113
        $output = $this->render('tag', $data);
114
115
        $response = $this->responseFactory->createResponse();
116
        $response->getBody()->write($output);
117
        return $response;
118
    }
119
}
120