Skip to content

Commit e4776b1

Browse files
committed
Trigger an event before saving an activity
1 parent 592d136 commit e4776b1

File tree

4 files changed

+51
-15
lines changed

4 files changed

+51
-15
lines changed

src/Concerns/LogsActivity.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
use Illuminate\Database\Eloquent\Model;
66
use Illuminate\Database\Eloquent\Relations\MorphMany;
77
use Illuminate\Database\Eloquent\SoftDeletes;
8-
use Illuminate\Support\Arr;
9-
use Illuminate\Support\Facades\Config;
8+
use Scrn\Journal\Events\ActivityPrepared;
109
use Scrn\Journal\Models\Activity;
1110

1211
trait LogsActivity
@@ -37,7 +36,9 @@ protected static function bootLogsActivity()
3736

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

40-
$model->transformActivity(journal()->action($event)->on($model)->data($old_data, $new_data)->toActivity())->save();
39+
$activity = $model->transformActivity(journal()->action($event)->on($model)->data($old_data, $new_data)->toActivity());
40+
41+
event(new ActivityPrepared($activity));
4142
});
4243
}
4344
}
@@ -51,7 +52,7 @@ public function getCreatedEventAttributes(): array
5152
{
5253
$attributes = [];
5354

54-
foreach ($this->attributes as $attribute => $value) {
55+
foreach ($this->getAttributes() as $attribute => $value) {
5556
if ($this->shouldAttributeBeLogged($attribute)) {
5657
$attributes[$attribute] = $value;
5758
}
@@ -73,7 +74,7 @@ public function getUpdatedEventAttributes(): array
7374
$old = [];
7475
$new = [];
7576

76-
foreach (array_merge($this->getDirty(), $this->getChanges()) as $attribute => $value) {
77+
foreach ($this->getDirty() as $attribute => $value) {
7778
if ($this->shouldAttributeBeLogged($attribute)) {
7879
$old[$attribute] = $this->getOriginal($attribute);
7980
$new[$attribute] = $this->getAttribute($attribute);
@@ -95,7 +96,7 @@ public function getDeletedEventAttributes(): array
9596
{
9697
$attributes = [];
9798

98-
foreach ($this->attributes as $attribute => $value) {
99+
foreach ($this->getAttributes() as $attribute => $value) {
99100
if ($this->shouldAttributeBeLogged($attribute)) {
100101
$attributes[$attribute] = $value;
101102
}
@@ -124,7 +125,7 @@ public function getRestoredEventAttributes(): array
124125
*/
125126
public function getLoggedEvents(): array
126127
{
127-
$events = Config::get('journal.events', ['created', 'updated', 'deleted']);
128+
$events = config()->get('journal.events', ['created', 'updated', 'deleted']);
128129

129130
if (in_array(SoftDeletes::class, class_uses_recursive(static::class))) {
130131
$events[] = 'restored';
@@ -134,9 +135,7 @@ public function getLoggedEvents(): array
134135
$events = array_merge($events, ['pivotAttached', 'pivotDetached', 'pivotUpdated']);
135136
}
136137

137-
if (isset($this->logged)) {
138-
$events = array_merge($events, $this->logged ?? []);
139-
}
138+
$events = array_merge($events, $this->logged ?? []);
140139

141140
return $events;
142141
}
@@ -185,7 +184,7 @@ public function getIgnoredAttributes(): array
185184
*/
186185
public function shouldLogTimestamps(): bool
187186
{
188-
return $this->logTimestamps ?? Config::get('journal.timestamps', false);
187+
return $this->logTimestamps ?? config()->get('journal.timestamps', false);
189188
}
190189

191190
/**
@@ -214,7 +213,7 @@ public function shouldLogEvent(string $event): bool
214213
*/
215214
public function getOriginalRelation(string $key, $default = null)
216215
{
217-
return Arr::get($this->originalRelations, $key, $default);
216+
return array_get($this->originalRelations, $key, $default);
218217
}
219218

220219
/**

src/Events/ActivityPrepared.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Scrn\Journal\Events;
4+
5+
use Scrn\Journal\Models\Activity;
6+
7+
class ActivityPrepared
8+
{
9+
protected $activity;
10+
11+
public function __construct(Activity $activity)
12+
{
13+
$this->activity = $activity;
14+
}
15+
16+
public function getActivity(): Activity
17+
{
18+
return $this->activity;
19+
}
20+
}

src/JournalServiceProvider.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Illuminate\Support\Facades\Event;
66
use Illuminate\Support\ServiceProvider;
7+
use Scrn\Journal\Events\ActivityPrepared;
8+
use Scrn\Journal\Listeners\SaveActivity;
79

810
class JournalServiceProvider extends ServiceProvider
911
{
@@ -15,18 +17,20 @@ class JournalServiceProvider extends ServiceProvider
1517
public function boot()
1618
{
1719
$this->publishes([
18-
__DIR__ . '/../config/journal.php' => config_path('journal.php'),
20+
__DIR__.'/../config/journal.php' => config_path('journal.php'),
1921
], 'config');
2022

2123
$this->publishes([
22-
__DIR__ . '/../migrations/' => database_path('migrations'),
24+
__DIR__.'/../migrations/' => database_path('migrations'),
2325
], 'migrations');
2426

2527
Event::subscribe(JournalEventSubscriber::class);
28+
29+
Event::listen(ActivityPrepared::class, SaveActivity::class);
2630
}
2731

2832
public function register()
2933
{
30-
$this->mergeConfigFrom(__DIR__ . '/../config/journal.php', 'journal');
34+
$this->mergeConfigFrom(__DIR__.'/../config/journal.php', 'journal');
3135
}
3236
}

src/Listeners/SaveActivity.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Scrn\Journal\Listeners;
4+
5+
use Scrn\Journal\Events\ActivityPrepared;
6+
7+
class SaveActivity
8+
{
9+
public function handle(ActivityPrepared $event)
10+
{
11+
$event->getActivity()->save();
12+
}
13+
}

0 commit comments

Comments
 (0)