]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/BookRepo.php
19d159eb1e7e9cc539a86f6031ef8004a20e70ef
[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         protected TrashCan $trashCan,
22     ) {
23     }
24
25     /**
26      * Create a new book in the system.
27      */
28     public function create(array $input): Book
29     {
30         $book = new Book();
31         $this->baseRepo->create($book, $input);
32         $this->baseRepo->updateCoverImage($book, $input['image'] ?? null);
33         $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id'] ?? null));
34         Activity::add(ActivityType::BOOK_CREATE, $book);
35
36         return $book;
37     }
38
39     /**
40      * Update the given book.
41      */
42     public function update(Book $book, array $input): Book
43     {
44         $this->baseRepo->update($book, $input);
45
46         if (array_key_exists('default_template_id', $input)) {
47             $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id']));
48         }
49
50         if (array_key_exists('image', $input)) {
51             $this->baseRepo->updateCoverImage($book, $input['image'], $input['image'] === null);
52         }
53
54         Activity::add(ActivityType::BOOK_UPDATE, $book);
55
56         return $book;
57     }
58
59     /**
60      * Update the given book's cover image, or clear it.
61      *
62      * @throws ImageUploadException
63      * @throws Exception
64      */
65     public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false)
66     {
67         $this->baseRepo->updateCoverImage($book, $coverImage, $removeImage);
68     }
69
70     /**
71      * Remove a book from the system.
72      *
73      * @throws Exception
74      */
75     public function destroy(Book $book)
76     {
77         $this->trashCan->softDestroyBook($book);
78         Activity::add(ActivityType::BOOK_DELETE, $book);
79
80         $this->trashCan->autoClearOld();
81     }
82 }