Passed
Pull Request — master (#108)
by Dmitriy
24:06 queued 09:02
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\Blog\Entity\Post;
6
use App\Blog\Entity\Tag;
7
use App\Blog\Post\PostRepository;
8
use App\Controller;
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
    protected static ?string $controllerName = 'blog/tag';
17
    private const POSTS_PER_PAGE = 10;
18
19
    public function index(Request $request, ORMInterface $orm): Response
20
    {
21
        /** @var TagRepository $tagRepo */
22
        $tagRepo = $orm->getRepository(Tag::class);
23
        /** @var PostRepository $postRepo */
24
        $postRepo = $orm->getRepository(Post::class);
25
        $label = $request->getAttribute('label', null);
26
        $pageNum = (int)$request->getAttribute('page', 1);
27
28
        $item = $tagRepo->findByLabel($label);
29
30
        if ($item === null) {
31
            return $this->responseFactory->createResponse(404);
32
        }
33
        // preloading of posts
34
        $paginator = (new OffsetPaginator($postRepo->findByTag($item->getId())))
35
            ->withPageSize(self::POSTS_PER_PAGE)
36
            ->withCurrentPage($pageNum);
37
38
        $data = [
39
            'item' => $item,
40
            'paginator' => $paginator,
41
        ];
42
        return $this->render('index', $data);
43
    }
44
}
45