CommentController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 26
ccs 10
cts 12
cp 0.8333
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A isAjaxRequest() 0 3 1
A index() 0 12 3
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