QuoteControllerJson::jsonQuote()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 28
ccs 0
cts 18
cp 0
crap 2
rs 9.7666
1
<?php
2
3
namespace App\Controller;
4
5
use Symfony\Component\HttpFoundation\JsonResponse;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\Routing\Annotation\Route;
8
9
/**
10
 * Controller that provides a random quote as a JSON response.
11
 */
12
class QuoteControllerJson
13
{
14
    /**
15
     * Returns a JSON response containing a random quote with date and timestamp.
16
     *
17
     * @return Response A JSON response.
18
     */
19
    #[Route("/api/quote")]
20
    public function jsonQuote(): Response
21
    {
22
        $quotes = [
23
            "An old silent pond - A frog jumps into the pond - Splash! Silence again.",
24
            "A world of dew - And within every dewdrop - A world of struggle.",
25
            "The apparition of these faces in the crowd - Petals on a wet, black bough.",
26
            "I write, erase, rewrite - Erase again, and then - A poppy blooms."
27
        ];
28
29
        // Choose a random quote from the list
30
        $randomQuote = $quotes[array_rand($quotes)];
31
32
        $data = [
33
            'quote' => $randomQuote,
34
            'date' => date('Y-m-d'),
35
            'timestamp' => date('Y-m-d H:i:s')
36
        ];
37
38
        // Create a JSON response with the quote, today's date, and timestamp
39
        $response = new JsonResponse($data);
40
41
        // Optionally, set JSON_PRETTY_PRINT flag for pretty formatting
42
        $response->setEncodingOptions(
43
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
44
        );
45
46
        return $response;
47
    }
48
}
49