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

TagController::index()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 20
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 35
rs 9.6
1
<?php
2
3
namespace App\Blog\Tag;
4
5
use App\Controller;
6
use App\Blog\Entity\Post;
7
use App\Blog\Entity\Tag;
8
use App\Blog\Post\PostRepository;
9
use App\Pagination\PaginationSet;
10
use Cycle\ORM\ORMInterface;
11
use Psr\Http\Message\ResponseInterface as Response;
12
use Psr\Http\Message\ServerRequestInterface as Request;
13
use Yiisoft\Data\Paginator\OffsetPaginator;
14
use Yiisoft\Router\UrlGeneratorInterface;
15
16
class TagController extends Controller
17
{
18
    private const POSTS_PER_PAGE = 10;
19
20
    protected function getId(): string
21
    {
22
        return 'blog/tag';
23
    }
24
25
    public function index(
26
        Request $request,
27
        ORMInterface $orm,
28
        UrlGeneratorInterface $urlGenerator
29
    ): Response {
30
        /** @var TagRepository $tagRepo */
31
        $tagRepo = $orm->getRepository(Tag::class);
32
        /** @var PostRepository $postRepo */
33
        $postRepo = $orm->getRepository(Post::class);
34
        $label = $request->getAttribute('label', null);
35
        $pageNum = (int)$request->getAttribute('page', 1);
36
37
        $item = $tagRepo->findByLabel($label);
38
39
        if ($item === null) {
40
            return $this->responseFactory->createResponse(404);
41
        }
42
        // preloading of posts
43
        $paginator = (new OffsetPaginator($postRepo->findByTag($item->getId())))
44
            ->withPageSize(self::POSTS_PER_PAGE)
45
            ->withCurrentPage($pageNum);
46
        $pageUrlGenerator = fn ($page) => $urlGenerator->generate(
47
            'blog/tag',
48
            ['label' => $label, 'page' => $page]
49
        );
50
51
        $data = [
52
            'item' => $item,
53
            'paginationSet' => new PaginationSet($paginator, $pageUrlGenerator),
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

53
            'paginationSet' => /** @scrutinizer ignore-deprecated */ new PaginationSet($paginator, $pageUrlGenerator),
Loading history...
54
        ];
55
        $output = $this->render(__FUNCTION__, $data);
56
57
        $response = $this->responseFactory->createResponse();
58
        $response->getBody()->write($output);
59
        return $response;
60
    }
61
}
62