Skip to content

Commit

Permalink
Merge pull request #23 from dragonfire1119/master
Browse files Browse the repository at this point in the history
Use contract for event
  • Loading branch information
weotch authored Jun 25, 2019
2 parents d2cd16c + c026b9a commit f5a0066
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions src/Cloner.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php namespace Bkwld\Cloner;

// Deps
use Illuminate\Events\Dispatcher as Events;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;

/**
* Core class that traverses a model's relationships and replicates model
* attributes
*/
class Cloner {
class Cloner
{

/**
* @var AttachmentAdapter
Expand All @@ -29,8 +30,10 @@ class Cloner {
*
* @param AttachmentAdapter $attachment
*/
public function __construct(AttachmentAdapter $attachment = null,
Events $events = null) {
public function __construct(
AttachmentAdapter $attachment = null,
DispatcherContract $events = null
) {
$this->attachment = $attachment;
$this->events = $events;
}
Expand All @@ -42,7 +45,8 @@ public function __construct(AttachmentAdapter $attachment = null,
* @param Illuminate\Database\Eloquent\Relations\Relation $relation
* @return Illuminate\Database\Eloquent\Model The new model instance
*/
public function duplicate($model, $relation = null) {
public function duplicate($model, $relation = null)
{
$clone = $this->cloneModel($model);
$this->duplicateAttachments($clone);
$this->saveClone($clone, $relation, $model);
Expand All @@ -57,7 +61,8 @@ public function duplicate($model, $relation = null) {
* @param string $connection A Laravel database connection
* @return Illuminate\Database\Eloquent\Model The new model instance
*/
public function duplicateTo($model, $connection) {
public function duplicateTo($model, $connection)
{
$this->write_connection = $connection; // Store the write database connection
$clone = $this->duplicate($model); // Do a normal duplicate
$this->write_connection = null; // Null out the connection for next run
Expand All @@ -70,7 +75,8 @@ public function duplicateTo($model, $connection) {
* @param Illuminate\Database\Eloquent\Model $model
* @return Illuminate\Database\Eloquent\Model The new model instance
*/
protected function cloneModel($model) {
protected function cloneModel($model)
{
$exempt = method_exists($model, 'getCloneExemptAttributes') ?
$model->getCloneExemptAttributes() : null;
$clone = $model->replicate($exempt);
Expand All @@ -85,9 +91,10 @@ protected function cloneModel($model) {
* @param Illuminate\Database\Eloquent\Model $clone
* @return void
*/
protected function duplicateAttachments($clone) {
protected function duplicateAttachments($clone)
{
if (!$this->attachment || !method_exists($clone, 'getCloneableFileAttributes')) return;
foreach($clone->getCloneableFileAttributes() as $attribute) {
foreach ($clone->getCloneableFileAttributes() as $attribute) {
if (!$original = $clone->getAttribute($attribute)) continue;
$clone->setAttribute($attribute, $this->attachment->duplicate($original));
}
Expand All @@ -103,22 +110,23 @@ protected function duplicateAttachments($clone) {
* @param boolean $child
* @return void
*/
protected function saveClone($clone, $relation = null, $src, $child = null) {
protected function saveClone($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]);
$this->events->dispatch('cloner::cloning: ' . get_class($src), [$clone, $src]);

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

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

/**
Expand All @@ -128,9 +136,10 @@ protected function saveClone($clone, $relation = null, $src, $child = null) {
* @param Illuminate\Database\Eloquent\Model $clone
* @return void
*/
protected function cloneRelations($model, $clone) {
protected function cloneRelations($model, $clone)
{
if (!method_exists($model, 'getCloneableRelations')) return;
foreach($model->getCloneableRelations() as $relation_name) {
foreach ($model->getCloneableRelations() as $relation_name) {
$this->duplicateRelation($model, $relation_name, $clone);
}
}
Expand All @@ -143,7 +152,8 @@ protected function cloneRelations($model, $clone) {
* @param Illuminate\Database\Eloquent\Model $clone
* @return void
*/
protected function duplicateRelation($model, $relation_name, $clone) {
protected function duplicateRelation($model, $relation_name, $clone)
{
$relation = call_user_func([$model, $relation_name]);
if (is_a($relation, 'Illuminate\Database\Eloquent\Relations\BelongsToMany')) {
$this->duplicatePivotedRelation($relation, $relation_name, $clone);
Expand All @@ -159,7 +169,8 @@ protected function duplicateRelation($model, $relation_name, $clone) {
* @param Illuminate\Database\Eloquent\Model $clone
* @return void
*/
protected function duplicatePivotedRelation($relation, $relation_name, $clone) {
protected function duplicatePivotedRelation($relation, $relation_name, $clone)
{

// If duplicating between databases, do not duplicate relations. The related
// instance may not exist in the other database or could have a different
Expand Down Expand Up @@ -187,8 +198,9 @@ protected function duplicatePivotedRelation($relation, $relation_name, $clone) {
* @param Illuminate\Database\Eloquent\Model $clone
* @return void
*/
protected function duplicateDirectRelation($relation, $relation_name, $clone) {
$relation->get()->each(function($foreign) use ($clone, $relation_name) {
protected function duplicateDirectRelation($relation, $relation_name, $clone)
{
$relation->get()->each(function ($foreign) use ($clone, $relation_name) {
$this->duplicate($foreign, $clone->$relation_name());
});
}
Expand Down

0 comments on commit f5a0066

Please sign in to comment.