]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/BookRepo.php
26b9414fbac24ebd74cb7db1d9d604e422029a42
[bookstack] / app / Entities / Repos / BookRepo.php
1 <?php
2
3 namespace BookStack\Entities\Repos;
4
5 use BookStack\Activity\ActivityType;
6 use BookStack\Activity\TagRepo;
7 use BookStack\Entities\Models\Book;
8 use BookStack\Entities\Tools\TrashCan;
9 use BookStack\Exceptions\ImageUploadException;
10 use BookStack\Facades\Activity;
11 use BookStack\Uploads\ImageRepo;
12 use Exception;
13 use Illuminate\Http\UploadedFile;
14
15 class BookRepo
16 {
17     public function __construct(
18         protected BaseRepo $baseRepo,
19         protected TagRepo $tagRepo,
20         protected ImageRepo $imageRepo
21     ) {
22     }
23
24     /**
25      * Create a new book in the system.
26      */
27     public function create(array $input): Book
28     {
29         $book = new Book();
30         $this->baseRepo->create($book, $input);
31         $this->baseRepo->updateCoverImage($book, $input['image'] ?? null);
32         $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id'] ?? null));
33         Activity::add(ActivityType::BOOK_CREATE, $book);
34
35         return $book;
36     }
37
38     /**
39      * Update the given book.
40      */
41     public function update(Book $book, array $input): Book
42     {
43         $this->baseRepo->update($book, $input);
44
45         if (array_key_exists('default_template_id', $input)) {
46             $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id']));
47         }
48
49         if (array_key_exists('image', $input)) {
50             $this->baseRepo->updateCoverImage($book, $input['image'], $input['image'] === null);
51         }
52
53         Activity::add(ActivityType::BOOK_UPDATE, $book);
54
55         return $book;
56     }
57
58     /**
59      * Update the given book's cover image, or clear it.
60      *
61      * @throws ImageUploadException
62      * @throws Exception
63      */
64     public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false)
65     {
66         $this->baseRepo->updateCoverImage($book, $coverImage, $removeImage);
67     }
68
69     /**
70      * Remove a book from the system.
71      *
72      * @throws Exception
73      */
74     public function destroy(Book $book)
75     {
76         $trashCan = new TrashCan();
77         $trashCan->softDestroyBook($book);
78         Activity::add(ActivityType::BOOK_DELETE, $book);
79
80         $trashCan->autoClearOld();
81     }
82 }