Skip to content

Commit 7adc295

Browse files
authored
Refactor auditSync, auditDetach, auditAttach methods (#866)
1 parent a8de033 commit 7adc295

File tree

2 files changed

+72
-42
lines changed

2 files changed

+72
-42
lines changed

src/Auditable.php

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ public function transitionTo(Contracts\Audit $audit, bool $old = false): Contrac
647647
* @param mixed $id
648648
* @param array $attributes
649649
* @param bool $touch
650+
* @param array $columns
650651
* @return void
651652
* @throws AuditingException
652653
*/
@@ -655,23 +656,18 @@ public function auditAttach(string $relationName, $id, array $attributes = [], $
655656
if (!method_exists($this, $relationName) || !method_exists($this->{$relationName}(), 'attach')) {
656657
throw new AuditingException('Relationship ' . $relationName . ' was not found or does not support method attach');
657658
}
658-
$this->auditEvent = 'attach';
659-
$this->isCustomEvent = true;
660-
$this->auditCustomOld = [
661-
$relationName => $this->{$relationName}()->get($columns)->toArray()
662-
];
659+
660+
$old = $this->{$relationName}()->get($columns);
663661
$this->{$relationName}()->attach($id, $attributes, $touch);
664-
$this->auditCustomNew = [
665-
$relationName => $this->{$relationName}()->get($columns)->toArray()
666-
];
667-
Event::dispatch(AuditCustom::class, [$this]);
668-
$this->isCustomEvent = false;
662+
$new = $this->{$relationName}()->get($columns);
663+
$this->dispatchRelationAuditEvent($relationName, 'attach', $old, $new);
669664
}
670665

671666
/**
672667
* @param string $relationName
673668
* @param mixed $ids
674669
* @param bool $touch
670+
* @param array $columns
675671
* @return int
676672
* @throws AuditingException
677673
*/
@@ -681,25 +677,19 @@ public function auditDetach(string $relationName, $ids = null, $touch = true, $c
681677
throw new AuditingException('Relationship ' . $relationName . ' was not found or does not support method detach');
682678
}
683679

684-
$this->auditEvent = 'detach';
685-
$this->isCustomEvent = true;
686-
$this->auditCustomOld = [
687-
$relationName => $this->{$relationName}()->get($columns)->toArray()
688-
];
680+
$old = $this->{$relationName}()->get($columns);
689681
$results = $this->{$relationName}()->detach($ids, $touch);
690-
$this->auditCustomNew = [
691-
$relationName => $this->{$relationName}()->get($columns)->toArray()
692-
];
693-
Event::dispatch(AuditCustom::class, [$this]);
694-
$this->isCustomEvent = false;
682+
$new = $this->{$relationName}()->get($columns);
683+
$this->dispatchRelationAuditEvent($relationName, 'detach', $old, $new);
684+
695685
return empty($results) ? 0 : $results;
696686
}
697687

698688
/**
699689
* @param $relationName
700690
* @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $ids
701691
* @param bool $detaching
702-
* @param bool $skipUnchanged
692+
* @param array $columns
703693
* @return array
704694
* @throws AuditingException
705695
*/
@@ -709,34 +699,22 @@ public function auditSync($relationName, $ids, $detaching = true, $columns = ['*
709699
throw new AuditingException('Relationship ' . $relationName . ' was not found or does not support method sync');
710700
}
711701

712-
$this->auditEvent = 'sync';
713-
714-
$this->auditCustomOld = [
715-
$relationName => $this->{$relationName}()->get($columns)->toArray()
716-
];
717-
702+
$old = $this->{$relationName}()->get($columns);
718703
$changes = $this->{$relationName}()->sync($ids, $detaching);
719-
720704
if (collect($changes)->flatten()->isEmpty()) {
721-
$this->auditCustomOld = [];
722-
$this->auditCustomNew = [];
705+
$old = $new = collect([]);
723706
} else {
724-
$this->auditCustomNew = [
725-
$relationName => $this->{$relationName}()->get($columns)->toArray()
726-
];
707+
$new = $this->{$relationName}()->get($columns);
727708
}
728-
729-
$this->isCustomEvent = true;
730-
Event::dispatch(AuditCustom::class, [$this]);
731-
$this->isCustomEvent = false;
709+
$this->dispatchRelationAuditEvent($relationName, 'sync', $old, $new);
732710

733711
return $changes;
734712
}
735713

736714
/**
737715
* @param string $relationName
738716
* @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $ids
739-
* @param bool $skipUnchanged
717+
* @param array $columns
740718
* @return array
741719
* @throws AuditingException
742720
*/
@@ -745,6 +723,32 @@ public function auditSyncWithoutDetaching(string $relationName, $ids, $columns =
745723
if (!method_exists($this, $relationName) || !method_exists($this->{$relationName}(), 'syncWithoutDetaching')) {
746724
throw new AuditingException('Relationship ' . $relationName . ' was not found or does not support method syncWithoutDetaching');
747725
}
726+
748727
return $this->auditSync($relationName, $ids, false, $columns);
749728
}
729+
730+
/**
731+
* @param string $relationName
732+
* @param string $event
733+
* @param \Illuminate\Support\Collection $old
734+
* @param \Illuminate\Support\Collection $new
735+
* @return void
736+
*/
737+
private function dispatchRelationAuditEvent($relationName, $event, $old, $new)
738+
{
739+
$this->auditCustomOld[$relationName] = $old->toArray();
740+
$this->auditCustomNew[$relationName] = $new->toArray();
741+
742+
if (
743+
empty($this->auditCustomOld[$relationName]) &&
744+
empty($this->auditCustomNew[$relationName])
745+
) {
746+
$this->auditCustomOld = $this->auditCustomNew = [];
747+
}
748+
749+
$this->auditEvent = $event;
750+
$this->isCustomEvent = true;
751+
Event::dispatch(AuditCustom::class, [$this]);
752+
$this->isCustomEvent = false;
753+
}
750754
}

tests/Functional/AuditingTest.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -594,11 +594,10 @@ public function itWillAuditCustomEventData()
594594

595595
$article->auditAttach('categories', $firstCategory);
596596
$article->auditAttach('categories', $secondCategory);
597+
$lastAttachedArticle = \end($article->audits->last()->getModified()['categories']['new']);
598+
597599
$this->assertSame($firstCategory->name, $article->categories->first()->name);
598-
$this->assertSame(
599-
$secondCategory->name,
600-
$article->audits->last()->getModified()['categories']['new'][1]['name']
601-
);
600+
$this->assertSame($secondCategory->name, $lastAttachedArticle['name']);
602601
}
603602

604603
/**
@@ -627,6 +626,33 @@ public function itWillAuditSync()
627626
$this->assertGreaterThan($no_of_audits_before, $no_of_audits_after);
628627
}
629628

629+
/**
630+
* @test
631+
* @return void
632+
*/
633+
public function itWillAuditDetach()
634+
{
635+
$firstCategory = factory(Category::class)->create();
636+
$secondCategory = factory(Category::class)->create();
637+
$article = factory(Article::class)->create();
638+
639+
$article->categories()->attach($firstCategory);
640+
$article->categories()->attach($secondCategory);
641+
642+
$no_of_audits_before = Audit::where('auditable_type', Article::class)->count();
643+
$categoryBefore = $article->categories()->first()->getKey();
644+
645+
$article->auditDetach('categories', [$firstCategory->getKey()]);
646+
647+
$no_of_audits_after = Audit::where('auditable_type', Article::class)->count();
648+
$categoryAfter = $article->categories()->first()->getKey();
649+
650+
$this->assertSame($firstCategory->getKey(), $categoryBefore);
651+
$this->assertSame($secondCategory->getKey(), $categoryAfter);
652+
$this->assertNotSame($categoryBefore, $categoryAfter);
653+
$this->assertGreaterThan($no_of_audits_before, $no_of_audits_after);
654+
}
655+
630656
/**
631657
* @test
632658
* @return void

0 commit comments

Comments
 (0)