Completed
Push — master ( 3a917b...8faa80 )
by Alexander
15s queued 12s
created

TagController::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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