Skip to content

Commit 41d9691

Browse files
committed
Refactor auditSync, auditDetach, auditAttach methods
1 parent 7d929c0 commit 41d9691

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
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->auditCustomOld = [];
747+
}
748+
749+
$this->auditEvent = $event;
750+
$this->isCustomEvent = true;
751+
Event::dispatch(AuditCustom::class, [$this]);
752+
$this->isCustomEvent = false;
753+
}
750754
}

0 commit comments

Comments
 (0)