Passed
Pull Request — master (#108)
by Dmitriy
24:06 queued 09:02
created

CommentController::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
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Blog;
6
7
use App\Blog\Comment\CommentService;
8
use App\Controller;
9
use Psr\Http\Message\ResponseInterface as Response;
10
use Psr\Http\Message\ServerRequestInterface as Request;
11
12
final class CommentController extends Controller
13
{
14
    protected static ?string $controllerName = 'blog/comments';
15
    public function index(Request $request, CommentService $service): Response
16
    {
17
        $paginator = $service->getFeedPaginator();
18
        if ($request->getAttribute('next') !== null) {
19
            $paginator = $paginator->withNextPageToken((string)$request->getAttribute('next'));
20
        }
21
22
        if ($this->isAjaxRequest($request)) {
23
            return $this->renderPartial('_comments', ['data' => $paginator]);
24
        }
25
26
        return $this->render('index', ['data' => $paginator]);
27
    }
28
29
    private function isAjaxRequest(Request $request): bool
30
    {
31
        return $request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest';
32
    }
33
}
34