cloner->entityToInputData($chapter); $book = $this->bookRepo->create($inputData); $this->cloner->copyEntityPermissions($chapter, $book); /** @var Page $page */ foreach ($chapter->pages as $page) { $page->chapter_id = 0; $page->changeBook($book->id); } $this->trashCan->destroyEntity($chapter); // TODO - Log activity for change return $book; } public function transformBookToShelf(Book $book): Bookshelf { // TODO - Check permissions before call // Permissions: edit-book, delete-book, create-shelf $inputData = $this->cloner->entityToInputData($book); $shelf = $this->shelfRepo->create($inputData, []); $this->cloner->copyEntityPermissions($book, $shelf); $shelfBookSyncData = []; /** @var Chapter $chapter */ foreach ($book->chapters as $index => $chapter) { $newBook = $this->transformChapterToBook($chapter); $shelfBookSyncData[$newBook->id] = ['order' => $index]; } $shelf->books()->sync($shelfBookSyncData); if ($book->directPages->count() > 0) { $book->name .= ' ' . trans('entities.pages'); } else { $this->trashCan->destroyEntity($book); } // TODO - Log activity for change return $shelf; } }