ApiJson::jsonRoutes()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 14
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 28
rs 9.7998
ccs 0
cts 16
cp 0
crap 12
1
<?php
2
3
namespace App\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6
use Symfony\Component\HttpFoundation\JsonResponse;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\Routing\Annotation\Route;
10
use Symfony\Component\Routing\RouterInterface;
11
12
class ApiJson extends AbstractController
13
{
14
    #[Route("/api", name: 'json_routes')]
15
    public function jsonRoutes(
16
        RouterInterface $router
17
    ): Response {
18
19
        //Get all routes.
20
        $routes = $router->getRouteCollection()->all();
21
        $apiArr = array();
22
23
        //Add route to array if starts with api.
24
        foreach ($routes as $route) {
25
26
            if (str_starts_with($route->getPath(), '/api/')) {
27
                $description = $route->getDefault('description') ?? 'Beskrivning saknas';
28
                $isbn = $route->getDefault('isbn') ?? null;
29
30
                $apiArr[] = [
31
                    'header' => $route->getPath(),
32
                    'description' => $description,
33
                    'isbn' => $isbn
34
                ];
35
            }
36
        }
37
38
        $data = [
39
            'routes' => $apiArr
40
        ];
41
        return $this->render('api.html.twig', $data);
42
    }
43
44
    #[Route("/api/quote", defaults: ['description' => 'Väljer ett citat från en lista och returnerar det med tiddatum stämpel.'])]
45
    public function jsonQuote(): Response
46
    {
47
        //Create quote and date.
48
        $quoteArr = array(
49
            'Människan är alltings mått. - Protagoras',
50
            'Att lära många ting lär oss inte att förstå. - Herakleitos',
51
            'Svin njuter av gyttja mer än rent vatten. - Herakleitos',
52
            'Den som vill försöka bli lycklig skall inte lägga an på att öka sina tillgångar utan på att begränsa sina krav. - Platon',
53
            'En god och ädel man bör avhålla sig från varje som helst kontakt med arbetslivet. - Platon',
54
            'Om jag ändå kunde finna sanningen lika lätt som jag kan avslöja osanningen. - Cicero'
55
        );
56
57
        $index = random_int(0, count($quoteArr) - 1);
58
59
        date_default_timezone_set("Europe/Stockholm");
60
        $dateTime = date("Y-m-d H:i:s", time());
61
62
        //Add quote and date for json response.
63
        $data = [
64
            'today-quote' => $quoteArr[$index],
65
            'today-date' => $dateTime
66
        ];
67
68
        $response = new JsonResponse($data);
69
        $response->setEncodingOptions(
70
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
71
        );
72
        return $response;
73
    }
74
}
75