Passed
Pull Request — master (#93)
by
unknown
13:24 queued 39s
created

CommentController::isAxaRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
c 1
b 0
f 1
dl 0
loc 6
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller;
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 'comment';
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->isAxaRequest($request)) {
27
            return $this->renderPartial('_comments', ['data' => $paginator]);
28
        }
29
30
        return $this->render('index', ['data' => $paginator]);
31
    }
32
33
    private function isAxaRequest(Request $request): bool
34
    {
35
        $params = $request->getServerParams();
36
37
        return !empty($params['HTTP_X_REQUESTED_WITH']) &&
38
            strtolower($params['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
39
    }
40
}
41