DetectorController::detect()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 2
dl 0
loc 18
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controller;
4
5
use App\AppKernel;
6
use Slim\Views\PhpRenderer;
7
use Symfony\Component\HttpFoundation\{JsonResponse, Request, Response};
8
9
final class DetectorController
10
{
11
    public const FORMAT_JSON = 'json';
12
    public const FORMAT_HTML = 'html';
13
    public const FORMAT_PLAIN = 'plaintext';
14
15
    private const TEMPLATES_PATH = AppKernel::PROJECT_ROOT.'/templates';
16
17
    public static function detect(Request $request, string $format = 'html'): Response
18
    {
19
        $clientData = [
20
            'ip_address' => $request->getClientIp(),
21
            'locale' => $request->getLocale(),
22
            'preferred_language' => $request->getPreferredLanguage(),
23
        ];
24
25
        if (static::FORMAT_JSON === $format) {
26
            return new JsonResponse($clientData);
27
        } elseif (static::FORMAT_PLAIN === $format) {
28
            return new Response($request->getClientIp());
29
        }
30
31
        $templater = new PhpRenderer(static::TEMPLATES_PATH);
32
        $templater->setAttributes($clientData);
33
34
        return new Response($templater->fetch('/client_data.php', $clientData));
35
    }
36
}