Skip to content

Commit

Permalink
Revert "Use contract for event"
Browse files Browse the repository at this point in the history
  • Loading branch information
weotch authored Jun 25, 2019
1 parent f5a0066 commit db32473
Showing 1 changed file with 18 additions and 30 deletions.
48 changes: 18 additions & 30 deletions src/Cloner.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php namespace Bkwld\Cloner;

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

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

/**
* @var AttachmentAdapter
Expand All @@ -30,10 +29,8 @@ class Cloner
*
* @param AttachmentAdapter $attachment
*/
public function __construct(
AttachmentAdapter $attachment = null,
DispatcherContract $events = null
) {
public function __construct(AttachmentAdapter $attachment = null,
Events $events = null) {
$this->attachment = $attachment;
$this->events = $events;
}
Expand All @@ -45,8 +42,7 @@ public function __construct(
* @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 @@ -61,8 +57,7 @@ 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 @@ -75,8 +70,7 @@ 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 @@ -91,10 +85,9 @@ 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 @@ -110,23 +103,22 @@ 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 @@ -136,10 +128,9 @@ 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 @@ -152,8 +143,7 @@ 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 @@ -169,8 +159,7 @@ 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 @@ -198,9 +187,8 @@ 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 db32473

Please sign in to comment.