]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/BookRepo.php
Normalize ini and properties values
[bookstack] / app / Entities / Repos / BookRepo.php
1 <?php
2
3
4 namespace BookStack\Entities\Repos;
5
6
7 use BookStack\Entities\Book;
8 use BookStack\Entities\Bookshelf;
9 use BookStack\Exceptions\NotFoundException;
10 use BookStack\Exceptions\NotifyException;
11
12 class BookRepo extends EntityRepo
13 {
14
15     /**
16      * Fetch a book by its slug.
17      * @param string $slug
18      * @return Book
19      * @throws NotFoundException
20      */
21     public function getBySlug(string $slug): Book
22     {
23         /** @var Book $book */
24         $book = $this->getEntityBySlug('book', $slug);
25         return $book;
26     }
27
28     /**
29      * Append a Book to a BookShelf.
30      * @param Bookshelf $shelf
31      * @param Book $book
32      */
33     public function appendBookToShelf(Bookshelf $shelf, Book $book)
34     {
35         if ($shelf->contains($book)) {
36             return;
37         }
38
39         $maxOrder = $shelf->books()->max('order');
40         $shelf->books()->attach($book->id, ['order' => $maxOrder + 1]);
41     }
42
43     /**
44      * Destroy the provided book and all its child entities.
45      * @param Book $book
46      * @throws NotifyException
47      * @throws \Throwable
48      */
49     public function destroyBook(Book $book)
50     {
51         foreach ($book->pages as $page) {
52             $this->destroyPage($page);
53         }
54
55         foreach ($book->chapters as $chapter) {
56             $this->destroyChapter($chapter);
57         }
58
59         $this->destroyEntityCommonRelations($book);
60         $book->delete();
61     }
62
63 }