Passed
Pull Request — master (#115)
by Dmitriy
23:50 queued 08:56
created

CommentController::index()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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