X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/eb76e882c5544e319e5084fedcc7cdac0bdf6c2d..refs/pull/3113/head:/app/Entities/Tools/TrashCan.php diff --git a/app/Entities/Tools/TrashCan.php b/app/Entities/Tools/TrashCan.php index 0b6081ae4..ab62165af 100644 --- a/app/Entities/Tools/TrashCan.php +++ b/app/Entities/Tools/TrashCan.php @@ -1,11 +1,13 @@ -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,32 +128,34 @@ 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 { $count = 0; $pages = $chapter->pages()->withTrashed()->get(); - if (count($pages)) { - foreach ($pages as $page) { - $this->destroyPage($page); - $count++; - } + foreach ($pages as $page) { + $this->destroyPage($page); + $count++; } $this->destroyCommonRelations($chapter); $chapter->forceDelete(); + return $count + 1; } /** * Remove a page from the system. + * * @throws Exception */ protected function destroyPage(Page $page): int @@ -160,6 +170,7 @@ class TrashCan } $page->forceDelete(); + return 1; } @@ -171,9 +182,10 @@ class TrashCan { $counts = []; - /** @var Entity $instance */ - foreach ((new EntityProvider)->all() as $key => $instance) { - $counts[$key] = $instance->newQuery()->onlyTrashed()->count(); + foreach ((new EntityProvider())->all() as $key => $instance) { + /** @var Builder $query */ + $query = $instance->newQuery(); + $counts[$key] = $query->onlyTrashed()->count(); } return $counts; @@ -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 @@ -287,6 +308,7 @@ class TrashCan /** * Destroy the given entity. + * * @throws Exception */ protected function destroyEntity(Entity $entity): int @@ -303,6 +325,8 @@ class TrashCan if ($entity instanceof Bookshelf) { return $this->destroyShelf($entity); } + + return 0; } /** @@ -320,9 +344,9 @@ class TrashCan $entity->deletions()->delete(); $entity->favourites()->delete(); - if ($entity instanceof HasCoverImage && $entity->cover) { + if ($entity instanceof HasCoverImage && $entity->cover()->exists()) { $imageService = app()->make(ImageService::class); - $imageService->destroy($entity->cover); + $imageService->destroy($entity->cover()->first()); } } }