Skip to content

Commit

Permalink
Refactored saving of clone to fix attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
wimski committed Jun 18, 2020
1 parent 4373516 commit 16d3b2a
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/Cloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,22 @@ public function __construct(AttachmentAdapter $attachment = null,
*/
public function duplicate($model, $relation = null) {
$clone = $this->cloneModel($model);
$this->saveClone($clone, $relation, $model);
$this->duplicateAttachments($model, $clone);

$this->dispatchOnCloningEvent($clone, $relation, $model);

if ($relation) {
$relation->save($clone);
} else {
$clone->save();
}

$this->duplicateAttachments($model, $clone);
$clone->save();

$this->dispatchOnClonedEvent($clone, $model);

$this->cloneRelations($model, $clone);

return $clone;
}

Expand Down Expand Up @@ -96,28 +109,29 @@ protected function duplicateAttachments($model, $clone) {
}

/**
* Save the clone. If a relation was passed, save the clone onto that
* relation. Otherwise, just save it.
*
* @param Illuminate\Database\Eloquent\Model $clone
* @param Illuminate\Database\Eloquent\Relations\Relation $relation
* @param Illuminate\Database\Eloquent\Model $src The orginal model
* @param boolean $child
* @return void
*/
protected function saveClone($clone, $relation = null, $src, $child = null) {

protected function dispatchOnCloningEvent($clone, $relation = null, $src, $child = null)
{
// Set the child flag
if ($relation) $child = true;

// Notify listeners via callback or event
if (method_exists($clone, 'onCloning')) $clone->onCloning($src, $child);
$this->events->dispatch('cloner::cloning: '.get_class($src), [$clone, $src]);
}

// Do the save
if ($relation) $relation->save($clone);
else $clone->save();

/**
* @param Illuminate\Database\Eloquent\Model $clone
* @param Illuminate\Database\Eloquent\Model $src The orginal model
* @return void
*/
protected function dispatchOnClonedEvent($clone, $src)
{
// Notify listeners via callback or event
if (method_exists($clone, 'onCloned')) $clone->onCloned($src);
$this->events->dispatch('cloner::cloned: '.get_class($src), [$clone, $src]);
Expand Down

0 comments on commit 16d3b2a

Please sign in to comment.