Issues (977)

src/Controller/LuckyControllerJson.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace App\Controller;
4
5
use App\Repository\BookRepository;
6
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\Routing\Annotation\Route;
10
11
/**
12
 * API endpoints for lucky numbers, quotes, games, and library books.
13
 */
14
class LuckyControllerJson extends AbstractController
15
{
16
    /**
17
     * Generate a random lucky number between 1 and 100 with current date and time.
18
     *
19
     * @return JsonResponse JSON response containing lucky number, date, and timestamp
20
     */
21
    #[Route('/api/lucky', name: 'api_lucky')]
22
    public function lucky(): JsonResponse
23
    {
24
        $number = random_int(1, 100);
25
26
        return new JsonResponse([
27
            'lucky_number' => $number,
28
            'date' => (new \DateTime())->format('Y-m-d'),
29
            'timestamp' => (new \DateTime())->format('H:i:s'),
30
        ]);
31
    }
32
33
    /**
34
     * Placeholder endpoint for game API status.
35
     *
36
     * @return JsonResponse JSON response with status and message
37
     */
38
    #[Route('/api/game', name: 'api_game', methods: ['GET'])]
39
    public function game(): JsonResponse
40
    {
41
        return new JsonResponse(['status' => 'ok', 'message' => 'API game placeholder']);
42
    }
43
44
    /**
45
     * Return a random programming-related quote with current date and time.
46
     *
47
     * @return JsonResponse JSON response containing a quote, date, and timestamp
48
     */
49
    #[Route('/api/quote', name: 'api_quote')]
50
    public function quote(): JsonResponse
51
    {
52
        $quotes = [
53
            'Code is poetry.',
54
            "Don't repeat yourself. Unless it’s coffee.",
55
            'Simplicity is the soul of efficiency.',
56
        ];
57
58
        $randomQuote = $quotes[array_rand($quotes)];
59
        $now = new \DateTime();
60
61
        return new JsonResponse([
62
            'quote' => $randomQuote,
63
            'date' => $now->format('Y-m-d'),
64
            'timestamp' => $now->format('H:i:s'),
65
        ]);
66
    }
67
68
    /**
69
     * Render the API landing page.
70
     *
71
     * @return Response HTTP response with rendered Twig template
72
     */
73
    #[Route('/api', name: 'api_landing')]
74
    public function apiLanding(): Response
75
    {
76
        return $this->render('api.html.twig');
0 ignored issues
show
The method render() does not exist on App\Controller\LuckyControllerJson. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
        return $this->/** @scrutinizer ignore-call */ render('api.html.twig');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
    }
78
79
    /**
80
     * Get all books from the library repository.
81
     *
82
     * @param BookRepository $bookRepository repository for fetching books
83
     *
84
     * @return JsonResponse JSON response with list of books
85
     */
86
    #[Route('/api/library/books', name: 'api_library_books', methods: ['GET'])]
87
    public function getAllBooks(BookRepository $bookRepository): JsonResponse
88
    {
89
        $books = $bookRepository->findAll();
90
91
        $data = array_map(function ($book) {
92
            return [
93
                'id' => $book->getId(),
94
                'title' => $book->getTitle(),
95
                'isbn' => $book->getIsbn(),
96
                'author' => $book->getAuthor(),
97
                'image' => $book->getImage(),
98
            ];
99
        }, $books);
100
101
        return $this->json($data);
0 ignored issues
show
The method json() does not exist on App\Controller\LuckyControllerJson. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
        return $this->/** @scrutinizer ignore-call */ json($data);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
    }
103
104
    /**
105
     * Get a single book by ISBN.
106
     *
107
     * @param BookRepository $bookRepository repository for fetching books
108
     * @param string         $isbn           ISBN identifier of the book
109
     *
110
     * @return JsonResponse JSON response with book data or error if not found
111
     */
112
    #[Route('/api/library/book/{isbn}', name: 'api_library_book_by_isbn', methods: ['GET'])]
113
    public function getBookByIsbn(BookRepository $bookRepository, string $isbn): JsonResponse
114
    {
115
        $book = $bookRepository->findOneBy(['isbn' => $isbn]);
116
117
        if (!$book) {
118
            return $this->json(['error' => 'Book not found'], 404);
119
        }
120
121
        return $this->json([
122
            'id' => $book->getId(),
123
            'title' => $book->getTitle(),
124
            'isbn' => $book->getIsbn(),
125
            'author' => $book->getAuthor(),
126
            'image' => $book->getImage(),
127
        ]);
128
    }
129
}
130