]> BookStack Code Mirror - bookstack/commitdiff
Cleaned up duplicate code in recycle-bin restore
authorDan Brown <redacted>
Mon, 2 Nov 2020 22:54:00 +0000 (22:54 +0000)
committerDan Brown <redacted>
Mon, 2 Nov 2020 22:54:00 +0000 (22:54 +0000)
app/Entities/Managers/TrashCan.php

index f99c62801cab68a2066079205c5234b44a541449..c567edaf37d33b9084e5eab3e9e90f35fbaa4526 100644 (file)
@@ -240,26 +240,21 @@ class TrashCan
         $count = 1;
         $entity->restore();
 
-        if ($entity->isA('chapter') || $entity->isA('book')) {
-            foreach ($entity->pages()->withTrashed()->withCount('deletions')->get() as $page) {
-                if ($page->deletions_count > 0) {
-                    $page->deletions()->delete();
-                }
-
-                $page->restore();
-                $count++;
+        $restoreAction = function ($entity) use (&$count) {
+            if ($entity->deletions_count > 0) {
+                $entity->deletions()->delete();
             }
+
+            $entity->restore();
+            $count++;
+        };
+
+        if ($entity->isA('chapter') || $entity->isA('book')) {
+            $entity->pages()->withTrashed()->withCount('deletions')->get()->each($restoreAction);
         }
 
         if ($entity->isA('book')) {
-            foreach ($entity->chapters()->withTrashed()->withCount('deletions')->get() as $chapter) {
-                if ($chapter->deletions_count === 0) {
-                    $chapter->deletions()->delete();
-                }
-
-                $chapter->restore();
-                $count++;
-            }
+            $entity->chapters()->withTrashed()->withCount('deletions')->get()->each($restoreAction);
         }
 
         return $count;