Passed
Pull Request — master (#448)
by
unknown
13:50
created

CommentController::index()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

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