Passed
Pull Request — master (#93)
by Alexander
12:07
created

CommentController::index()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
rs 10
c 0
b 0
f 0
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\Controller;
9
use Psr\Http\Message\ResponseInterface as Response;
10
use Psr\Http\Message\ServerRequestInterface as Request;
11
12
final class CommentController extends Controller
13
{
14
    protected function getId(): string
15
    {
16
        return 'blog/comments';
17
    }
18
19
    public function index(Request $request, CommentService $service): Response
20
    {
21
        $paginator = $service->getFeedPaginator();
22
        if ($request->getAttribute('next') !== null) {
23
            $paginator = $paginator->withNextPageToken((string)$request->getAttribute('next'));
24
        }
25
26
        if ($this->isAjaxRequest($request)) {
27
            return $this->renderPartial('_comments', ['data' => $paginator]);
28
        }
29
30
        return $this->render('index', ['data' => $paginator]);
31
    }
32
33
    private function isAjaxRequest(Request $request): bool
34
    {
35
        return $request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest';
36
    }
37
}
38