CommentController::isAjaxRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Blog;
6
7
use App\Blog\Comment\CommentService;
8
use Psr\Http\Message\ResponseInterface as Response;
9
use Psr\Http\Message\ServerRequestInterface as Request;
10
use Yiisoft\Yii\View\ViewRenderer;
11
12
final class CommentController
13
{
14
    private ViewRenderer $viewRenderer;
15
16 1
    public function __construct(ViewRenderer $viewRenderer)
17
    {
18 1
        $this->viewRenderer = $viewRenderer->withControllerName('blog/comments');
19 1
    }
20
21 1
    public function index(Request $request, CommentService $service): Response
22
    {
23 1
        $paginator = $service->getFeedPaginator();
24 1
        if ($request->getAttribute('next') !== null) {
25
            $paginator = $paginator->withNextPageToken((string)$request->getAttribute('next'));
26
        }
27
28 1
        if ($this->isAjaxRequest($request)) {
29
            return $this->viewRenderer->renderPartial('_comments', ['data' => $paginator]);
30
        }
31
32 1
        return $this->viewRenderer->render('index', ['data' => $paginator]);
33
    }
34
35 1
    private function isAjaxRequest(Request $request): bool
36
    {
37 1
        return $request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest';
38
    }
39
}
40