Passed
Pull Request — master (#49)
by
unknown
10:51
created

TagController::index()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 24
rs 9.7998
cc 2
nc 2
nop 2
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 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
use Yiisoft\Router\UrlGeneratorInterface;
14
15
final class TagController extends Controller
16
{
17
    private const POSTS_PER_PAGE = 10;
18
19
    protected function getId(): string
20
    {
21
        return 'blog/tag';
22
    }
23
24
    public function index(Request $request, ORMInterface $orm): Response
25
    {
26
        /** @var TagRepository $tagRepo */
27
        $tagRepo = $orm->getRepository(Tag::class);
28
        /** @var PostRepository $postRepo */
29
        $postRepo = $orm->getRepository(Post::class);
30
        $label = $request->getAttribute('label', null);
31
        $pageNum = (int)$request->getAttribute('page', 1);
32
33
        $item = $tagRepo->findByLabel($label);
34
35
        if ($item === null) {
36
            return $this->responseFactory->createResponse(404);
37
        }
38
        // preloading of posts
39
        $paginator = (new OffsetPaginator($postRepo->findByTag($item->getId())))
40
            ->withPageSize(self::POSTS_PER_PAGE)
41
            ->withCurrentPage($pageNum);
42
43
        $data = [
44
            'item' => $item,
45
            'paginator' => $paginator,
46
        ];
47
        return $this->render('index', $data);
48
    }
49
}
50