1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-07-31 15:24:31 +03:00

Extracted page copy to new cloner class

Fundemental refactor for planned additional clone operations.
No behaviour change intended in this commit.
This commit is contained in:
Dan Brown
2021-12-19 12:56:27 +00:00
parent da01913616
commit 3f9527f166
3 changed files with 54 additions and 49 deletions

View File

@ -0,0 +1,44 @@
<?php
namespace BookStack\Entities\Tools;
use BookStack\Entities\Models\Entity;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Repos\PageRepo;
class Cloner
{
/**
* @var PageRepo
*/
protected $pageRepo;
public function __construct(PageRepo $pageRepo)
{
$this->pageRepo = $pageRepo;
}
/**
* Clone the given page into the given parent using the provided name.
*/
public function clonePage(Page $original, Entity $parent, string $newName): Page
{
$copyPage = $this->pageRepo->getNewDraftPage($parent);
$pageData = $original->getAttributes();
// Update name
$pageData['name'] = $newName;
// Copy tags from previous page if set
if ($original->tags) {
$pageData['tags'] = [];
foreach ($original->tags as $tag) {
$pageData['tags'][] = ['name' => $tag->name, 'value' => $tag->value];
}
}
return $this->pageRepo->publishDraft($copyPage, $pageData);
}
}