]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/ChapterRepo.php
Entity Repo & Controller Refactor (#1690)
[bookstack] / app / Entities / Repos / ChapterRepo.php
1 <?php namespace BookStack\Entities\Repos;
2
3 use BookStack\Entities\Book;
4 use BookStack\Entities\Chapter;
5 use BookStack\Entities\Managers\BookContents;
6 use BookStack\Entities\Managers\TrashCan;
7 use BookStack\Exceptions\MoveOperationException;
8 use BookStack\Exceptions\NotFoundException;
9 use BookStack\Exceptions\NotifyException;
10 use Exception;
11 use Illuminate\Contracts\Container\BindingResolutionException;
12 use Illuminate\Database\Eloquent\Builder;
13 use Illuminate\Support\Collection;
14
15 class ChapterRepo
16 {
17
18     protected $baseRepo;
19
20     /**
21      * ChapterRepo constructor.
22      * @param $baseRepo
23      */
24     public function __construct(BaseRepo $baseRepo)
25     {
26         $this->baseRepo = $baseRepo;
27     }
28
29     /**
30      * Get a chapter via the slug.
31      * @throws NotFoundException
32      */
33     public function getBySlug(string $bookSlug, string $chapterSlug): Chapter
34     {
35         $chapter = Chapter::visible()->whereSlugs($bookSlug, $chapterSlug)->first();
36
37         if ($chapter === null) {
38             throw new NotFoundException(trans('errors.chapter_not_found'));
39         }
40
41         return $chapter;
42     }
43
44     /**
45      * Create a new chapter in the system.
46      */
47     public function create(array $input, Book $parentBook): Chapter
48     {
49         $chapter = new Chapter();
50         $chapter->book_id = $parentBook->id;
51         $chapter->priority = (new BookContents($parentBook))->getLastPriority() + 1;
52         $this->baseRepo->create($chapter, $input);
53         return $chapter;
54     }
55
56     /**
57      * Update the given chapter.
58      */
59     public function update(Chapter $chapter, array $input): Chapter
60     {
61         $this->baseRepo->update($chapter, $input);
62         return $chapter;
63     }
64
65     /**
66      * Update the permissions of a chapter.
67      */
68     public function updatePermissions(Chapter $chapter, bool $restricted, Collection $permissions = null)
69     {
70         $this->baseRepo->updatePermissions($chapter, $restricted, $permissions);
71     }
72
73     /**
74      * Remove a chapter from the system.
75      * @throws Exception
76      */
77     public function destroy(Chapter $chapter)
78     {
79         $trashCan = new TrashCan();
80         $trashCan->destroyChapter($chapter);
81     }
82
83     /**
84      * Move the given chapter into a new parent book.
85      * The $parentIdentifier must be a string of the following format:
86      * 'book:<id>' (book:5)
87      * @throws MoveOperationException
88      */
89     public function move(Chapter $chapter, string $parentIdentifier): Book
90     {
91         $stringExploded = explode(':', $parentIdentifier);
92         $entityType = $stringExploded[0];
93         $entityId = intval($stringExploded[1]);
94
95         if ($entityType !== 'book') {
96             throw new MoveOperationException('Chapters can only be moved into books');
97         }
98
99         $parent = Book::visible()->where('id', '=', $entityId)->first();
100         if ($parent === null) {
101             throw new MoveOperationException('Book to move chapter into not found');
102         }
103
104         $chapter->changeBook($parent->id);
105         $chapter->rebuildPermissions();
106         return $parent;
107     }
108 }