Test Failed
Pull Request — master (#494)
by
unknown
03:32
created

CommentController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 26
rs 10
c 0
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\Http\Controllers\Blog;
6
7
use App\Modules\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
    public function __construct(ViewRenderer $viewRenderer)
18
    {
19
        $this->viewRenderer = $viewRenderer->withControllerName('blog/comments');
20
    }
21
22
    public function index(Request $request, CommentService $service, CurrentRoute $currentRoute): Response
23
    {
24
        $paginator = $service->getFeedPaginator();
25
        if ($currentRoute->getArgument('next') !== null) {
26
            $paginator = $paginator->withNextPageToken((string)$currentRoute->getArgument('next'));
27
        }
28
29
        if ($this->isAjaxRequest($request)) {
30
            return $this->viewRenderer->renderPartial('_comments', ['data' => $paginator]);
31
        }
32
33
        return $this->viewRenderer->render('index', ['data' => $paginator]);
34
    }
35
36
    private function isAjaxRequest(Request $request): bool
37
    {
38
        return $request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest';
39
    }
40
}
41