BookController::createBookForm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
ccs 0
cts 2
cp 0
crap 2
1
<?php
2
3
namespace App\Controller;
4
5
use App\Entity\Book;
6
use App\Repository\BookRepository;
7
use Doctrine\Persistence\ManagerRegistry;
8
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\Routing\Attribute\Route;
13
14
/**
15
 * Controller for managing library.
16
 */
17
class BookController extends AbstractController
18
{
19
    /**
20
     * Displays form for creating a new book.
21
     */
22
    #[Route('/book/create', name: 'book_create', methods: ['GET'])]
23
    public function createBookForm(): Response
24
    {
25
        return $this->render('book/create.html.twig');
26
    }
27
28
    /**
29
     * Handles creation of new book.
30
     */
31
    #[Route('/book/create', name: 'book_create_post', methods: ['POST'])]
32
    public function createBook(Request $request, ManagerRegistry $doctrine): Response
33
    {
34
        $entityManager = $doctrine->getManager();
35
36
        $book = new Book();
37
        $book->setTitle($request->request->get('title'));
38
        $book->setIsbn($request->request->get('isbn'));
39
        $book->setAuthor($request->request->get('author'));
40
        $book->setImage($request->request->get('image_filename'));
41
42
        $entityManager->persist($book);
43
        $entityManager->flush();
44
45
        return new Response('Saved new book with id ' . $book->getId());
46
    }
47
    /**
48
     * Display a book by ID.
49
     */
50
    #[Route('/book/show/{id}', name: 'book_by_id')]
51
    public function showBookById(
52
        bookRepository $bookRepository,
53
        int $id
54
    ): Response {
55
        $book = $bookRepository
56
            ->find($id);
57
58
        return $this->render('book/show_one.html.twig', [
59
            'book' => $book,
60
        ]);
61
    }
62
    /**
63
     * Display all books.
64
     */
65
    #[Route('/book/show', name: 'book_show_all')]
66
    public function showAllBooks(BookRepository $bookRepository): Response
67
    {
68
        $books = $bookRepository->findAll();
69
70
        return $this->render('book/show_all.html.twig', [
71
            'books' => $books,
72
        ]);
73
    }
74
    /**
75
     * Delete a book by ID.
76
     */
77
    #[Route('/book/delete/{id}', name: 'book_delete_by_id')]
78
    public function deleteBookById(
79
        ManagerRegistry $doctrine,
80
        int $id
81
    ): Response {
82
        $entityManager = $doctrine->getManager();
83
        $book = $entityManager->getRepository(book::class)->find($id);
84
85
        $entityManager->remove($book);
86
        $entityManager->flush();
87
88
        return $this->redirectToRoute('book_show_all');
89
    }
90
    /**
91
     * Display form for update of a book.
92
     */
93
    #[Route('/book/edit/{id}', name: 'book_edit', methods: ['GET'])]
94
    public function editBookForm(int $id, ManagerRegistry $doctrine): Response
95
    {
96
        $book = $doctrine->getRepository(Book::class)->find($id);
97
98
        return $this->render('book/edit.html.twig', [
99
            'book' => $book,
100
        ]);
101
    }
102
    /**
103
     * Handles update of a book.
104
     */
105
    #[Route('/book/update/{id}', name: 'book_update', methods: ['POST'])]
106
    public function updateBook(
107
        Request $request,
108
        ManagerRegistry $doctrine,
109
        int $id
110
    ): Response {
111
        $entityManager = $doctrine->getManager();
112
        $book = $entityManager->getRepository(Book::class)->find($id);
113
114
        $book->setTitle($request->request->get('title'));
115
        $book->setIsbn($request->request->get('isbn'));
116
        $book->setAuthor($request->request->get('author'));
117
        $book->setImage($request->request->get('image_filename'));
118
119
        $entityManager->flush();
120
121
        return $this->redirectToRoute('book_show_all');
122
    }
123
}
124