]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/ChapterRepo.php
Apply fixes from StyleCI
[bookstack] / app / Entities / Repos / ChapterRepo.php
1 <?php
2
3 namespace BookStack\Entities\Repos;
4
5 use BookStack\Actions\ActivityType;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Entities\Tools\BookContents;
9 use BookStack\Entities\Tools\TrashCan;
10 use BookStack\Exceptions\MoveOperationException;
11 use BookStack\Exceptions\NotFoundException;
12 use BookStack\Facades\Activity;
13 use Exception;
14
15 class ChapterRepo
16 {
17     protected $baseRepo;
18
19     /**
20      * ChapterRepo constructor.
21      */
22     public function __construct(BaseRepo $baseRepo)
23     {
24         $this->baseRepo = $baseRepo;
25     }
26
27     /**
28      * Get a chapter via the slug.
29      *
30      * @throws NotFoundException
31      */
32     public function getBySlug(string $bookSlug, string $chapterSlug): Chapter
33     {
34         $chapter = Chapter::visible()->whereSlugs($bookSlug, $chapterSlug)->first();
35
36         if ($chapter === null) {
37             throw new NotFoundException(trans('errors.chapter_not_found'));
38         }
39
40         return $chapter;
41     }
42
43     /**
44      * Create a new chapter in the system.
45      */
46     public function create(array $input, Book $parentBook): Chapter
47     {
48         $chapter = new Chapter();
49         $chapter->book_id = $parentBook->id;
50         $chapter->priority = (new BookContents($parentBook))->getLastPriority() + 1;
51         $this->baseRepo->create($chapter, $input);
52         Activity::addForEntity($chapter, ActivityType::CHAPTER_CREATE);
53
54         return $chapter;
55     }
56
57     /**
58      * Update the given chapter.
59      */
60     public function update(Chapter $chapter, array $input): Chapter
61     {
62         $this->baseRepo->update($chapter, $input);
63         Activity::addForEntity($chapter, ActivityType::CHAPTER_UPDATE);
64
65         return $chapter;
66     }
67
68     /**
69      * Remove a chapter from the system.
70      *
71      * @throws Exception
72      */
73     public function destroy(Chapter $chapter)
74     {
75         $trashCan = new TrashCan();
76         $trashCan->softDestroyChapter($chapter);
77         Activity::addForEntity($chapter, ActivityType::CHAPTER_DELETE);
78         $trashCan->autoClearOld();
79     }
80
81     /**
82      * Move the given chapter into a new parent book.
83      * The $parentIdentifier must be a string of the following format:
84      * 'book:<id>' (book:5).
85      *
86      * @throws MoveOperationException
87      */
88     public function move(Chapter $chapter, string $parentIdentifier): Book
89     {
90         $stringExploded = explode(':', $parentIdentifier);
91         $entityType = $stringExploded[0];
92         $entityId = intval($stringExploded[1]);
93
94         if ($entityType !== 'book') {
95             throw new MoveOperationException('Chapters can only be moved into books');
96         }
97
98         /** @var Book $parent */
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         Activity::addForEntity($chapter, ActivityType::CHAPTER_MOVE);
107
108         return $parent;
109     }
110 }