Passed
Push — master ( d82392...67272d )
by Alexander
04:51
created

CommentController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 26
ccs 9
cts 11
cp 0.8182
rs 10
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\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