Skip to content

Commit 4e477a6

Browse files
committed
Refactoring
1 parent 128f236 commit 4e477a6

File tree

4 files changed

+127
-104
lines changed

4 files changed

+127
-104
lines changed

src/Concerns/DetectsChanges.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Scrn\Journal\Concerns;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
trait DetectsChanges
8+
{
9+
/** @var array */
10+
protected $oldAttributes;
11+
12+
/**
13+
* Boot the detects changes trait for a model.
14+
*
15+
* @return void
16+
*/
17+
protected static function bootDetectsChanges()
18+
{
19+
static::updating(function (Model $model) {
20+
$oldModel = $model->replicate()->setRawAttributes($model->getOriginal());
21+
22+
$model->oldAttributes = $oldModel->getLoggedAttributeValues();
23+
});
24+
}
25+
26+
/**
27+
* Get the old attributes of the model.
28+
*
29+
* @return array
30+
*/
31+
public function getOldAttributes(): array
32+
{
33+
return $this->oldAttributes;
34+
}
35+
36+
/**
37+
* Get the values of the attributes that should be logged.
38+
*
39+
* @return array
40+
*/
41+
public function getLoggedAttributeValues(): array
42+
{
43+
$attributes = [];
44+
45+
foreach ($this->getLoggedAttributes() as $attribute) {
46+
$attributes[$attribute] = $this->getAttribute($attribute);
47+
}
48+
49+
return $attributes;
50+
}
51+
52+
/**
53+
* Get the attributes that should be logged.
54+
*
55+
* @return array
56+
*/
57+
public function getLoggedAttributes(): array
58+
{
59+
$attributes = $this->loggedAttributes ?? array_keys($this->attributes);
60+
61+
if (! $this->shouldLogTimestamps()) {
62+
$attributes = array_diff($attributes, [static::CREATED_AT, static::UPDATED_AT]);
63+
}
64+
65+
return array_diff($attributes, $this->getIgnoredAttributes());
66+
}
67+
68+
/**
69+
* Get the attributes that should never be logged.
70+
*
71+
* @return array
72+
*/
73+
public function getIgnoredAttributes(): array
74+
{
75+
return $this->ignoredAttributes ?? [];
76+
}
77+
78+
/**
79+
* Determine if timestamps should be logged.
80+
*
81+
* @return bool
82+
*/
83+
public function shouldLogTimestamps(): bool
84+
{
85+
return $this->logTimestamps ?? config()->get('journal.timestamps', false);
86+
}
87+
}

src/Concerns/LogsActivity.php

Lines changed: 13 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
use Illuminate\Database\Eloquent\Model;
66
use Illuminate\Database\Eloquent\Relations\MorphMany;
77
use Illuminate\Database\Eloquent\SoftDeletes;
8-
use Scrn\Journal\Events\ActivityPrepared;
98
use Scrn\Journal\Models\Activity;
109

1110
trait LogsActivity
1211
{
12+
use DetectsChanges;
1313
use LogsRelatedActivity;
1414

1515
/**
@@ -36,31 +36,23 @@ protected static function bootLogsActivity()
3636

3737
list($old_data, $new_data) = method_exists($model, $attributeGetter) ? $model->$attributeGetter(...func_get_args()) : null;
3838

39-
$activity = $model->transformActivity(journal()->action($event)->on($model)->data($old_data, $new_data)->toActivity());
39+
$activity = journal()->action($event)->on($model)->data($old_data, $new_data)->toActivity();
4040

41-
event(new ActivityPrepared($activity));
41+
$model->transformActivity($activity)->save();
4242
});
4343
}
4444
}
4545

4646
/**
47-
* Get the attributes for a created event.
47+
* Get the attributes for the created event.
4848
*
4949
* @return array
5050
*/
5151
public function getCreatedEventAttributes(): array
5252
{
53-
$attributes = [];
54-
55-
foreach ($this->getAttributes() as $attribute => $value) {
56-
if ($this->shouldAttributeBeLogged($attribute)) {
57-
$attributes[$attribute] = $value;
58-
}
59-
}
60-
6153
return [
6254
null,
63-
$attributes,
55+
$this->getLoggedAttributeValues(),
6456
];
6557
}
6658

@@ -71,15 +63,13 @@ public function getCreatedEventAttributes(): array
7163
*/
7264
public function getUpdatedEventAttributes(): array
7365
{
74-
$old = [];
75-
$new = [];
66+
$old = $this->getOldAttributes();
67+
$new = $this->getLoggedAttributeValues();
7668

77-
foreach ($this->getDirty() as $attribute => $value) {
78-
if ($this->shouldAttributeBeLogged($attribute)) {
79-
$old[$attribute] = $this->getOriginal($attribute);
80-
$new[$attribute] = $this->getAttribute($attribute);
81-
}
82-
}
69+
$new = array_diff_uassoc($new, $old, function ($new, $old) {
70+
return $new <=> $old;
71+
});
72+
$old = array_intersect_key($old, $new);
8373

8474
return [
8575
$old,
@@ -94,22 +84,14 @@ public function getUpdatedEventAttributes(): array
9484
*/
9585
public function getDeletedEventAttributes(): array
9686
{
97-
$attributes = [];
98-
99-
foreach ($this->getAttributes() as $attribute => $value) {
100-
if ($this->shouldAttributeBeLogged($attribute)) {
101-
$attributes[$attribute] = $value;
102-
}
103-
}
104-
10587
return [
106-
$attributes,
88+
$this->getLoggedAttributeValues(),
10789
null,
10890
];
10991
}
11092

11193
/**
112-
* Get the attributes for the deleted event.
94+
* Get the attributes for the restored event.
11395
*
11496
* @return array
11597
*/
@@ -140,53 +122,6 @@ public function getLoggedEvents(): array
140122
return $events;
141123
}
142124

143-
/**
144-
* Determine if the attribute should be logged.
145-
*
146-
* @param string $attribute
147-
* @return bool
148-
*/
149-
public function shouldAttributeBeLogged(string $attribute): bool
150-
{
151-
return in_array($attribute, $this->getLoggedAttributes()) && ! in_array($attribute, $this->getIgnoredAttributes());
152-
}
153-
154-
/**
155-
* Get the attributes that should be logged.
156-
*
157-
* @return array
158-
*/
159-
public function getLoggedAttributes(): array
160-
{
161-
$attributes = $this->loggedAttributes ?? array_keys($this->attributes);
162-
163-
if (! $this->shouldLogTimestamps()) {
164-
$attributes = array_diff($attributes, [static::CREATED_AT, static::UPDATED_AT]);
165-
}
166-
167-
return $attributes;
168-
}
169-
170-
/**
171-
* Get the attributes that should never be logged.
172-
*
173-
* @return array
174-
*/
175-
public function getIgnoredAttributes(): array
176-
{
177-
return $this->ignoredAttributes ?? [];
178-
}
179-
180-
/**
181-
* Determine if timestamps should be logged.
182-
*
183-
* @return bool
184-
*/
185-
public function shouldLogTimestamps(): bool
186-
{
187-
return $this->logTimestamps ?? config()->get('journal.timestamps', false);
188-
}
189-
190125
/**
191126
* Determine if the event should be logged.
192127
*
@@ -204,31 +139,6 @@ public function shouldLogEvent(string $event): bool
204139
return true;
205140
}
206141

207-
/**
208-
* Get the models original relation values.
209-
*
210-
* @param string $key
211-
* @param null $default
212-
* @return mixed|array
213-
*/
214-
public function getOriginalRelation(string $key, $default = null)
215-
{
216-
return array_get($this->originalRelations, $key, $default);
217-
}
218-
219-
/**
220-
* Sync a single original relation with its current value.
221-
*
222-
* @param string $relation
223-
* @return $this
224-
*/
225-
public function syncOriginalRelation(string $relation)
226-
{
227-
$this->originalRelations[$relation] = $this->getRelationValue($relation);
228-
229-
return $this;
230-
}
231-
232142
/**
233143
* Resolve the attribute getter for the given event.
234144
*

src/Concerns/LogsRelatedActivity.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,31 @@ public static function bootLogsRelatedActivity()
2424
}
2525
}
2626

27+
/**
28+
* Get the models original relation values.
29+
*
30+
* @param string $key
31+
* @param null $default
32+
* @return mixed|array
33+
*/
34+
public function getOriginalRelation(string $key, $default = null)
35+
{
36+
return array_get($this->originalRelations, $key, $default);
37+
}
38+
39+
/**
40+
* Sync a single original relation with its current value.
41+
*
42+
* @param string $relation
43+
* @return $this
44+
*/
45+
public function syncOriginalRelation(string $relation)
46+
{
47+
$this->originalRelations[$relation] = $this->getRelationValue($relation);
48+
49+
return $this;
50+
}
51+
2752
/**
2853
* Get the attributes for the pivot attached event.
2954
*

src/Journal.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\Eloquent\Model;
66
use Illuminate\Support\Facades\Request;
7+
use Scrn\Journal\Events\ActivityPrepared;
78
use Scrn\Journal\Models\Activity;
89
use Scrn\Journal\Resolvers\UserResolver;
910

@@ -127,7 +128,7 @@ public function save(): Activity
127128
{
128129
$activity = $this->toActivity();
129130

130-
$activity->save();
131+
event(new ActivityPrepared($activity));
131132

132133
return $activity;
133134
}

0 commit comments

Comments
 (0)