Skip to content

Commit

Permalink
Refactor auditSync, auditDetach, auditAttach methods
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 committed Oct 4, 2023
1 parent 7d929c0 commit 0a80c2f
Showing 1 changed file with 42 additions and 38 deletions.
80 changes: 42 additions & 38 deletions src/Auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ public function transitionTo(Contracts\Audit $audit, bool $old = false): Contrac
* @param mixed $id
* @param array $attributes
* @param bool $touch
* @param array $columns
* @return void
* @throws AuditingException
*/
Expand All @@ -655,23 +656,18 @@ public function auditAttach(string $relationName, $id, array $attributes = [], $
if (!method_exists($this, $relationName) || !method_exists($this->{$relationName}(), 'attach')) {
throw new AuditingException('Relationship ' . $relationName . ' was not found or does not support method attach');
}
$this->auditEvent = 'attach';
$this->isCustomEvent = true;
$this->auditCustomOld = [
$relationName => $this->{$relationName}()->get($columns)->toArray()
];

$old = $this->{$relationName}()->get($columns);
$this->{$relationName}()->attach($id, $attributes, $touch);
$this->auditCustomNew = [
$relationName => $this->{$relationName}()->get($columns)->toArray()
];
Event::dispatch(AuditCustom::class, [$this]);
$this->isCustomEvent = false;
$new = $this->{$relationName}()->get($columns);
$this->dispatchRelationAuditEvent($relationName, 'attach', $old, $new);
}

/**
* @param string $relationName
* @param mixed $ids
* @param bool $touch
* @param array $columns
* @return int
* @throws AuditingException
*/
Expand All @@ -681,25 +677,19 @@ public function auditDetach(string $relationName, $ids = null, $touch = true, $c
throw new AuditingException('Relationship ' . $relationName . ' was not found or does not support method detach');
}

$this->auditEvent = 'detach';
$this->isCustomEvent = true;
$this->auditCustomOld = [
$relationName => $this->{$relationName}()->get($columns)->toArray()
];
$old = $this->{$relationName}()->get($columns);
$results = $this->{$relationName}()->detach($ids, $touch);
$this->auditCustomNew = [
$relationName => $this->{$relationName}()->get($columns)->toArray()
];
Event::dispatch(AuditCustom::class, [$this]);
$this->isCustomEvent = false;
$new = $this->{$relationName}()->get($columns);
$this->dispatchRelationAuditEvent($relationName, 'detach', $old, $new);

return empty($results) ? 0 : $results;
}

/**
* @param $relationName
* @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $ids
* @param bool $detaching
* @param bool $skipUnchanged
* @param array $columns
* @return array
* @throws AuditingException
*/
Expand All @@ -709,34 +699,22 @@ public function auditSync($relationName, $ids, $detaching = true, $columns = ['*
throw new AuditingException('Relationship ' . $relationName . ' was not found or does not support method sync');
}

$this->auditEvent = 'sync';

$this->auditCustomOld = [
$relationName => $this->{$relationName}()->get($columns)->toArray()
];

$old = $this->{$relationName}()->get($columns);
$changes = $this->{$relationName}()->sync($ids, $detaching);

if (collect($changes)->flatten()->isEmpty()) {
$this->auditCustomOld = [];
$this->auditCustomNew = [];
$old = $new = collect([]);
} else {
$this->auditCustomNew = [
$relationName => $this->{$relationName}()->get($columns)->toArray()
];
$new = $this->{$relationName}()->get($columns);
}

$this->isCustomEvent = true;
Event::dispatch(AuditCustom::class, [$this]);
$this->isCustomEvent = false;
$this->dispatchRelationAuditEvent($relationName, 'sync', $old, $new);

return $changes;
}

/**
* @param string $relationName
* @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $ids
* @param bool $skipUnchanged
* @param array $columns
* @return array
* @throws AuditingException
*/
Expand All @@ -745,6 +723,32 @@ public function auditSyncWithoutDetaching(string $relationName, $ids, $columns =
if (!method_exists($this, $relationName) || !method_exists($this->{$relationName}(), 'syncWithoutDetaching')) {
throw new AuditingException('Relationship ' . $relationName . ' was not found or does not support method syncWithoutDetaching');
}

return $this->auditSync($relationName, $ids, false, $columns);
}

/**
* @param string $relationName
* @param string $event
* @param \Illuminate\Support\Collection $old
* @param \Illuminate\Support\Collection $new
* @return void
*/
private function dispatchRelationAuditEvent($relationName, $event, $old, $new)
{
$this->auditCustomOld[$relationName] = $old->toArray();
$this->auditCustomNew[$relationName] = $new->toArray();

if (
empty($this->auditCustomOld[$relationName]) &&
empty($this->auditCustomNew[$relationName])
) {
$this->auditCustomOld = $this->auditCustomNew = [];
}

$this->auditEvent = $event;
$this->isCustomEvent = true;
Event::dispatch(AuditCustom::class, [$this]);
$this->isCustomEvent = false;
}
}

0 comments on commit 0a80c2f

Please sign in to comment.