From f085cbd9ff9451c6b25603b234c7165c1295a5ca Mon Sep 17 00:00:00 2001 From: Vildan Bina Date: Sat, 10 Feb 2024 14:22:12 +0100 Subject: [PATCH 1/2] Refactor: Extract relation replication logic into separate method - Isolates the logic for replicating and associating draftable relations to a published model into a standalone method for improved readability and modularity - Enhances flexibility for future adjustments and custom use-cases --- src/Concerns/HasDrafts.php | 73 ++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/src/Concerns/HasDrafts.php b/src/Concerns/HasDrafts.php index cb3992f..36b4661 100644 --- a/src/Concerns/HasDrafts.php +++ b/src/Concerns/HasDrafts.php @@ -192,40 +192,7 @@ public function setLive(): void $published->setCurrent(); $published->saveQuietly(); - collect($this->getDraftableRelations())->each(function (string $relationName) use ($published) { - $relation = $published->{$relationName}(); - switch (true) { - case $relation instanceof HasOne: - if ($related = $this->{$relationName}) { - $replicated = $related->replicate(); - - $method = method_exists($replicated, 'getDraftableAttributes') - ? 'getDraftableAttributes' - : 'getAttributes'; - - $published->{$relationName}()->create($replicated->$method()); - } - - break; - case $relation instanceof HasMany: - $this->{$relationName}()->get()->each(function ($model) use ($published, $relationName) { - $replicated = $model->replicate(); - - $method = method_exists($replicated, 'getDraftableAttributes') - ? 'getDraftableAttributes' - : 'getAttributes'; - - $published->{$relationName}()->create($replicated->$method()); - }); - - break; - case $relation instanceof MorphToMany: - case $relation instanceof BelongsToMany: - $published->{$relationName}()->sync($this->{$relationName}()->pluck('id')); - - break; - } - }); +; $this->replicateAndAssociateDraftableRelations($published); }); $this->{$this->getIsPublishedColumn()} = false; @@ -235,6 +202,44 @@ public function setLive(): void $this->shouldCreateRevision = false; } + public function replicateAndAssociateDraftableRelations(Model $model): void + { + collect($this->getDraftableRelations())->each(function (string $relationName) use ($published) { + $relation = $published->{$relationName}(); + switch (true) { + case $relation instanceof HasOne: + if ($related = $this->{$relationName}) { + $replicated = $related->replicate(); + + $method = method_exists($replicated, 'getDraftableAttributes') + ? 'getDraftableAttributes' + : 'getAttributes'; + + $published->{$relationName}()->create($replicated->$method()); + } + + break; + case $relation instanceof HasMany: + $this->{$relationName}()->get()->each(function ($model) use ($published, $relationName) { + $replicated = $model->replicate(); + + $method = method_exists($replicated, 'getDraftableAttributes') + ? 'getDraftableAttributes' + : 'getAttributes'; + + $published->{$relationName}()->create($replicated->$method()); + }); + + break; + case $relation instanceof MorphToMany: + case $relation instanceof BelongsToMany: + $published->{$relationName}()->sync($this->{$relationName}()->pluck('id')); + + break; + } + }); + } + public function getDraftableRelations(): array { return property_exists($this, 'draftableRelations') ? $this->draftableRelations : []; From 29ad6f7ea4770b73b29c8568d90078e73ca5d217 Mon Sep 17 00:00:00 2001 From: Vildan Bina Date: Sat, 10 Feb 2024 14:27:28 +0100 Subject: [PATCH 2/2] quick fix --- src/Concerns/HasDrafts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Concerns/HasDrafts.php b/src/Concerns/HasDrafts.php index 36b4661..5db2d61 100644 --- a/src/Concerns/HasDrafts.php +++ b/src/Concerns/HasDrafts.php @@ -202,7 +202,7 @@ public function setLive(): void $this->shouldCreateRevision = false; } - public function replicateAndAssociateDraftableRelations(Model $model): void + public function replicateAndAssociateDraftableRelations(Model $published): void { collect($this->getDraftableRelations())->each(function (string $relationName) use ($published) { $relation = $published->{$relationName}();