Skip to content

Commit 55baabe

Browse files
committed
Refactor IsRevisionable.php for readability and consistency
1 parent e22c4a3 commit 55baabe

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

src/Concerns/IsRevisionable.php

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ trait IsRevisionable
2828

2929
public static function bootIsRevisionable()
3030
{
31-
static::saving(function ($model) {
31+
static::saving(function($model) {
3232
$model->preSave();
3333
});
3434

35-
static::updated(function ($model) {
35+
static::updated(function($model) {
3636
$model->postUpdate();
3737
});
3838

39-
static::created(function ($model) {
39+
static::created(function($model) {
4040
$model->postCreate();
4141
});
4242

43-
static::deleted(function ($model) {
43+
static::deleted(function($model) {
4444
$model->preSave();
4545
$model->postDelete();
4646
$model->postForceDelete();
@@ -121,10 +121,10 @@ public function postUpdate(): void
121121

122122
if (count($revisions) && $maxRevisionCountReached && ($this->revisionCleanup ?? false)) {
123123
foreach ($this->revisionHistory()
124-
->orderBy('id')
125-
->offset($this->historyLimit - 1)
126-
->limit(1000)
127-
->cursor() as $revision) {
124+
->orderBy('id')
125+
->offset($this->historyLimit - 1)
126+
->limit(1000)
127+
->cursor() as $revision) {
128128
$revision->delete();
129129
}
130130
}
@@ -158,7 +158,7 @@ private function changedRevisionableFields(): array
158158

159159
if (! array_key_exists($key, $this->originalData) || $oldValue != $newValue) {
160160
$relevantChanges[] = [
161-
'key' => $key,
161+
'key' => $key,
162162
'old_value' => $oldValue,
163163
'new_value' => $newValue,
164164
];
@@ -200,21 +200,24 @@ private function insertRevisions(array $revisions, string $event): void
200200

201201
$default = [
202202
'revisionable_type' => $this->getMorphClass(),
203-
'revisionable_id' => $this->getKey(),
204-
'revision' => now()->microsecond,
205-
'process' => PHP_PROCESS_UID,
206-
'key' => null,
207-
'old_value' => null,
208-
'new_value' => null,
209-
'user_id' => auth()->id(),
210-
'created_at' => now(),
203+
'revisionable_id' => $this->getKey(),
204+
'revision' => now()->microsecond,
205+
'process' => PHP_PROCESS_UID,
206+
'key' => null,
207+
'old_value' => null,
208+
'new_value' => null,
209+
'user_id' => auth()->id(),
210+
'created_at' => now(),
211211
];
212212

213213
foreach ($revisions as &$revision) {
214214
$revision = array_merge($default, $revision);
215215
}
216+
unset($revision);
216217

217-
Revision::create($revisions);
218+
foreach ($revisions as $revision) {
219+
Revision::create($revision);
220+
}
218221

219222
Event::dispatch('revisionable.'.$event, ['model' => $this, 'revisions' => $revisions]);
220223
}
@@ -231,7 +234,7 @@ public function postCreate(): void
231234

232235
if ((! isset($this->revisionEnabled) || $this->revisionEnabled)) {
233236
$revisions[] = [
234-
'key' => self::CREATED_AT,
237+
'key' => self::CREATED_AT,
235238
'old_value' => null,
236239
'new_value' => $this->{self::CREATED_AT},
237240
];
@@ -251,7 +254,7 @@ public function postDelete(): void
251254
&& $this->isRevisionable($this->getDeletedAtColumn())
252255
) {
253256
$revisions[] = [
254-
'key' => $this->getDeletedAtColumn(),
257+
'key' => $this->getDeletedAtColumn(),
255258
'old_value' => null,
256259
'new_value' => $this->{$this->getDeletedAtColumn()},
257260
];
@@ -282,12 +285,12 @@ public function postForceDelete()
282285
&& (($this->isSoftDelete() && $this->isForceDeleting()) || ! $this->isSoftDelete())) {
283286
$revisions[] = [
284287
'revisionable_type' => $this->getMorphClass(),
285-
'revisionable_id' => $this->getKey(),
286-
'key' => self::CREATED_AT,
287-
'old_value' => $this->{self::CREATED_AT},
288-
'new_value' => null,
289-
'user_id' => $this->getSystemUserId(),
290-
'created_at' => new \DateTime(),
288+
'revisionable_id' => $this->getKey(),
289+
'key' => self::CREATED_AT,
290+
'old_value' => $this->{self::CREATED_AT},
291+
'new_value' => null,
292+
'user_id' => $this->getSystemUserId(),
293+
'created_at' => new \DateTime(),
291294
];
292295

293296
foreach ($revisions as $revision) {

0 commit comments

Comments
 (0)