Passed
Branch main (02626f)
by Vedrana
05:25
created

LibraryController::addBook()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 20
rs 9.8666
ccs 0
cts 12
cp 0
crap 6
1
<?php
2
3
namespace App\Controller;
4
5
use App\Entity\Library;
6
use Doctrine\Persistence\ManagerRegistry;
7
use App\Repository\LibraryRepository;
8
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\Routing\Attribute\Route;
12
13
/**
14
 * Controller for managing the library.
15
 */
16
final class LibraryController extends AbstractController
17
{
18
    /**
19
     * Library landing page.
20
     *
21
     * @return Response
22
     */
23
    #[Route('/library', name: 'app_library')]
24
    public function index(): Response
25
    {
26
        return $this->render('library/index.html.twig', [
27
            'controller_name' => 'LibraryController',
28
        ]);
29
    }
30
31
    /**
32
     * Handles adding a new book.
33
     *
34
     * @param Request $request
35
     * @param ManagerRegistry $doctrine
36
     * @return Response
37
     */
38
    #[Route('/library/create', name: 'library_create', methods: ['GET', 'POST'])]
39
    public function addBook(
40
        ManagerRegistry $doctrine,
41
        Request $request
42
    ): Response {
43
        if ($request->isMethod('POST')) {
44
            $entityManager = $doctrine->getManager();
45
46
            $library = new Library();
47
            $library->setTitle((string) $request->request->get('title'));
48
            $library->setAuthor((string) $request->request->get('author'));
49
            $library->setIsbn((string) $request->request->get('isbn'));
50
            $library->setBookcover((string) $request->request->get('bookcover'));
51
52
            $entityManager->persist($library);
53
            $entityManager->flush();
54
55
            return $this->redirectToRoute('view_library');
56
        }
57
        return $this->render('library/create.html.twig');
58
    }
59
60
    /**
61
     * Lists all books in the library.
62
     *
63
     * @param LibraryRepository $libraryRepository
64
     * @return Response
65
     */
66
    #[Route('/library/view', name: 'view_library')]
67
    public function viewLibrary(LibraryRepository $libraryRepository): Response
68
    {
69
70
        $library = $libraryRepository->findAll();
71
72
        return $this->render('library/view.html.twig', [
73
            'library' => $library,
74
        ]);
75
    }
76
77
    /**
78
     * Displays a single book by its ID.
79
     *
80
     * @param LibraryRepository $libraryRepository
81
     * @param int $id
82
     * @return Response
83
     */
84
    #[Route('/library/view/{id}', name: 'book_by_id')]
85
    public function viewBookById(
86
        LibraryRepository $libraryRepository,
87
        int $id
88
    ): Response {
89
        $book = $this->getBookById($libraryRepository, $id);
90
91
        return $this->render('library/book.html.twig', [
92
            'book' => $book,
93
        ]);
94
    }
95
96
    /**
97
     * Handles delete of a book by ID.
98
     *
99
     * @param Request $request
100
     * @param ManagerRegistry $doctrine
101
     * @param LibraryRepository $libraryRepository
102
     * @param int $id
103
     * @return Response
104
     */
105
    #[Route('/library/delete/{id}', name: 'library_delete', methods: ['GET', 'POST'])]
106
    public function deleteBook(
107
        Request $request,
108
        ManagerRegistry $doctrine,
109
        LibraryRepository $libraryRepository,
110
        int $id
111
    ): Response {
112
        $book = $this->getBookById($libraryRepository, $id);
113
114
        if ($request->isMethod('POST')) {
115
            $entityManager = $doctrine->getManager();
116
            $entityManager->remove($book);
117
            $entityManager->flush();
118
119
            return $this->redirectToRoute('view_library');
120
        }
121
        return $this->render('library/delete.html.twig', [
122
            'book' => $book,
123
        ]);
124
    }
125
126
    /**
127
     * Handles update of a book's data.
128
     *
129
     * @param Request $request
130
     * @param ManagerRegistry $doctrine
131
     * @param LibraryRepository $libraryRepository
132
     * @param int $id
133
     * @return Response
134
     */
135
    #[Route('/library/update/{id}', name: 'book_update', methods: ['GET', 'POST'])]
136
    public function updateBook(
137
        ManagerRegistry $doctrine,
138
        LibraryRepository $libraryRepository,
139
        int $id,
140
        Request $request
141
    ): Response {
142
        $book = $this->getBookById($libraryRepository, $id);
143
144
        if ($request->isMethod('POST')) {
145
            $book->setTitle((string) $request->request->get('title'));
146
            $book->setAuthor((string) $request->request->get('author'));
147
            $book->setIsbn((string) $request->request->get('isbn'));
148
            $book->setBookcover((string) $request->request->get('bookcover'));
149
150
            $doctrine->getManager()->flush();
151
152
            return $this->redirectToRoute('view_library');
153
        }
154
155
        return $this->render('library/update.html.twig', ['book' => $book]);
156
    }
157
158
    /**
159
     * Helper function to find a book, 404 if not found.
160
     *
161
     * @param LibraryRepository $libraryRepository
162
     * @param int $id
163
     * @return Library
164
     */
165
    private function getBookById(LibraryRepository $libraryRepository, int $id): Library
166
    {
167
        $book = $libraryRepository->find($id);
168
        if (!$book) {
169
            throw $this->createNotFoundException('No book found for id ' . $id);
170
        }
171
        return $book;
172
    }
173
}
174