LibraryController::deletePost()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 7
rs 10
ccs 0
cts 4
cp 0
crap 2
1
<?php
2
3
namespace App\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6
use App\Entity\Main\Book;
7
use App\Repository\BookRepository;
8
use Doctrine\ORM\EntityManagerInterface;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\Routing\Annotation\Route;
12
13
class LibraryController extends AbstractController
14
{
15
    #[Route('/library', name: 'library_index')]
16
    public function index(): Response
17
    {
18
        return $this->render('library/index.html.twig');
19
    }
20
21
    #[Route('/library/create', name: 'library_create', methods: ['GET'])]
22
    public function create(): Response
23
    {
24
        return $this->render('library/create.html.twig');
25
    }
26
27
    #[Route('/library/create', name: 'library_create_post', methods: ['POST'])]
28
    public function createPost(Request $request, EntityManagerInterface $entityManager): Response
29
    {
30
        /** @var string $title */
31
        $title = $request->request->get('title');
32
33
        /** @var string $isbn ISBN number (13 digits). */
34
        $isbn = $request->request->get('isbn');
35
36
        /** @var string $description */
37
        $description = $request->request->get('description');
38
39
        /** @var string $author */
40
        $author = $request->request->get('author');
41
42
        /** @var string $imageUrl URL to image used as book cover. */
43
        $imageUrl = $request->request->get('image_url');
44
45
        $book = new Book();
46
        $book->setTitle($title);
47
        $book->setIsbn($isbn);
48
        $book->setDescription($description);
49
        $book->setAuthor($author);
50
        $book->setImageUrl($imageUrl);
51
52
        $entityManager->persist($book);
53
        $entityManager->flush();
54
55
        return $this->redirectToRoute('library_read_many');
56
    }
57
58
    #[Route('/library/read/', name: 'library_read_many')]
59
    public function readMany(BookRepository $bookRepository): Response
60
    {
61
        $books = $bookRepository->findAll();
62
63
        return $this->render('library/read_many.html.twig', ['books' => $books]);
64
    }
65
66
    #[Route('/library/read/{id}', name: 'library_read_one')]
67
    public function readById(Book $book): Response
68
    {
69
        return $this->render('library/read.html.twig', ['book' => $book]);
70
    }
71
72
    #[Route('/library/edit/{id}', name: 'library_edit', methods: ['GET'])]
73
    public function edit(Book $book): Response
74
    {
75
        return $this->render('library/edit.html.twig', ['book' => $book]);
76
    }
77
78
    #[Route('/library/edit/{id}', name: 'library_edit_post', methods: ['POST'])]
79
    public function editPost(Request $request, EntityManagerInterface $entityManager, Book $book): Response
80
    {
81
        /** @var string $title */
82
        $title = $request->request->get('title', $book->getTitle());
83
84
        /** @var string $isbn ISBN number (13 digits). */
85
        $isbn = $request->request->get('isbn', $book->getIsbn());
86
87
        /** @var string $description */
88
        $description = $request->request->get('description', $book->getDescription());
89
90
        /** @var string $author */
91
        $author = $request->request->get('author', $book->getAuthor());
92
93
        /** @var string $imageUrl URL to image used as book cover. */
94
        $imageUrl = $request->request->get('image_url', $book->getImageUrl());
95
96
        $book->setTitle($title);
97
        $book->setIsbn($isbn);
98
        $book->setDescription($description);
99
        $book->setAuthor($author);
100
        $book->setImageUrl($imageUrl);
101
        $entityManager->flush();
102
103
        return $this->redirectToRoute('library_read_one', [
104
            'id' => $book->getId()
105
        ]);
106
    }
107
108
    #[Route('/library/delete/{id}', name: 'library_delete', methods: ['GET'])]
109
    public function delete(Book $book): Response
110
    {
111
        return $this->render('library/delete.html.twig', ['book' => $book]);
112
    }
113
114
    #[Route('/library/delete/{id}', name: 'library_delete_post', methods: ['POST'])]
115
    public function deletePost(EntityManagerInterface $entityManager, Book $book): Response
116
    {
117
        $entityManager->remove($book);
118
        $entityManager->flush();
119
120
        return $this->redirectToRoute('library_read_many');
121
    }
122
123
    #[Route('/library/reset/', name: 'library_reset', methods: ['POST'])]
124
    public function reset(BookRepository $bookRepository): Response
125
    {
126
        $bookRepository->drop();   // Drop book table
127
        $bookRepository->create(); // Create book table
128
        $bookRepository->insert(); // Insert default rows
129
130
        return $this->redirectToRoute('library_read_many');
131
    }
132
}
133