X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/c7a2d568bf693add30c8402d68d1f46f09a44c5b..refs/pull/3072/head:/app/Entities/Tools/TrashCan.php diff --git a/app/Entities/Tools/TrashCan.php b/app/Entities/Tools/TrashCan.php index 62d373e97..fcf933726 100644 --- a/app/Entities/Tools/TrashCan.php +++ b/app/Entities/Tools/TrashCan.php @@ -1,13 +1,15 @@ -destroyCommonRelations($shelf); $shelf->forceDelete(); + return 1; } /** * Remove a book from the system. * Destroys any child chapters and pages. + * * @throws Exception */ protected function destroyBook(Book $book): int @@ -120,12 +127,14 @@ class TrashCan $this->destroyCommonRelations($book); $book->forceDelete(); + return $count + 1; } /** * Remove a chapter from the system. * Destroys all pages within. + * * @throws Exception */ protected function destroyChapter(Chapter $chapter): int @@ -141,16 +150,19 @@ class TrashCan $this->destroyCommonRelations($chapter); $chapter->forceDelete(); + return $count + 1; } /** * Remove a page from the system. + * * @throws Exception */ protected function destroyPage(Page $page): int { $this->destroyCommonRelations($page); + $page->allRevisions()->delete(); // Delete Attached Files $attachmentService = app(AttachmentService::class); @@ -159,6 +171,7 @@ class TrashCan } $page->forceDelete(); + return 1; } @@ -168,11 +181,10 @@ class TrashCan */ public function getTrashedCounts(): array { - $provider = app(EntityProvider::class); $counts = []; /** @var Entity $instance */ - foreach ($provider->all() as $key => $instance) { + foreach ((new EntityProvider())->all() as $key => $instance) { $counts[$key] = $instance->newQuery()->onlyTrashed()->count(); } @@ -181,6 +193,7 @@ class TrashCan /** * Destroy all items that have pending deletions. + * * @throws Exception */ public function empty(): int @@ -190,11 +203,13 @@ class TrashCan foreach ($deletions as $deletion) { $deleteCount += $this->destroyFromDeletion($deletion); } + return $deleteCount; } /** * Destroy an element from the given deletion model. + * * @throws Exception */ public function destroyFromDeletion(Deletion $deletion): int @@ -207,28 +222,33 @@ class TrashCan $count = $this->destroyEntity($deletion->deletable); } $deletion->delete(); + return $count; } /** * Restore the content within the given deletion. + * * @throws Exception */ public function restoreFromDeletion(Deletion $deletion): int { $shouldRestore = true; $restoreCount = 0; - $parent = $deletion->deletable->getParent(); - if ($parent && $parent->trashed()) { - $shouldRestore = false; + if ($deletion->deletable instanceof Entity) { + $parent = $deletion->deletable->getParent(); + if ($parent && $parent->trashed()) { + $shouldRestore = false; + } } - if ($shouldRestore) { + if ($deletion->deletable instanceof Entity && $shouldRestore) { $restoreCount = $this->restoreEntity($deletion->deletable); } $deletion->delete(); + return $restoreCount; } @@ -236,6 +256,7 @@ class TrashCan * Automatically clear old content from the recycle bin * depending on the configured lifetime. * Returns the total number of deleted elements. + * * @throws Exception */ public function autoClearOld(): int @@ -274,11 +295,11 @@ class TrashCan $count++; }; - if ($entity->isA('chapter') || $entity->isA('book')) { + if ($entity instanceof Chapter || $entity instanceof Book) { $entity->pages()->withTrashed()->withCount('deletions')->get()->each($restoreAction); } - if ($entity->isA('book')) { + if ($entity instanceof Book) { $entity->chapters()->withTrashed()->withCount('deletions')->get()->each($restoreAction); } @@ -287,21 +308,25 @@ class TrashCan /** * Destroy the given entity. + * + * @throws Exception */ protected function destroyEntity(Entity $entity): int { - if ($entity->isA('page')) { + if ($entity instanceof Page) { return $this->destroyPage($entity); } - if ($entity->isA('chapter')) { + if ($entity instanceof Chapter) { return $this->destroyChapter($entity); } - if ($entity->isA('book')) { + if ($entity instanceof Book) { return $this->destroyBook($entity); } - if ($entity->isA('shelf')) { + if ($entity instanceof Bookshelf) { return $this->destroyShelf($entity); } + + return 0; } /** @@ -317,6 +342,7 @@ class TrashCan $entity->jointPermissions()->delete(); $entity->searchTerms()->delete(); $entity->deletions()->delete(); + $entity->favourites()->delete(); if ($entity instanceof HasCoverImage && $entity->cover) { $imageService = app()->make(ImageService::class);