Passed
Push — main ( a57e19...f52e71 )
by Vedrana
28:10
created

QuoteControllerJson   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 35
ccs 18
cts 18
cp 1
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A jsonQuote() 0 28 1
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 1
    #[Route("/api/quote")]
20
    public function jsonQuote(): Response
21
    {
22 1
        $quotes = [
23 1
            "An old silent pond - A frog jumps into the pond - Splash! Silence again.",
24 1
            "A world of dew - And within every dewdrop - A world of struggle.",
25 1
            "The apparition of these faces in the crowd - Petals on a wet, black bough.",
26 1
            "I write, erase, rewrite - Erase again, and then - A poppy blooms."
27 1
        ];
28
29
        // Choose a random quote from the list
30 1
        $randomQuote = $quotes[array_rand($quotes)];
31
32 1
        $data = [
33 1
            'quote' => $randomQuote,
34 1
            'date' => date('Y-m-d'),
35 1
            'timestamp' => date('Y-m-d H:i:s')
36 1
        ];
37
38
        // Create a JSON response with the quote, today's date, and timestamp
39 1
        $response = new JsonResponse($data);
40
41
        // Optionally, set JSON_PRETTY_PRINT flag for pretty formatting
42 1
        $response->setEncodingOptions(
43 1
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
44 1
        );
45
46 1
        return $response;
47
    }
48
}
49