From 66498a9cfa23dc1b34601dfb74ef691772eca800 Mon Sep 17 00:00:00 2001 From: erikn69 Date: Wed, 20 Dec 2023 03:28:46 -0500 Subject: [PATCH] Fix queue auditing contracts breaking changes (#883) * Fix queue auditing contracts breaking changes * Remove Dispatchable trait(Lumen compatibility) --- src/Auditable.php | 6 +++++- src/AuditableObserver.php | 3 ++- src/Contracts/UserResolver.php | 2 +- src/Events/DispatchAudit.php | 3 --- src/Resolvers/UrlResolver.php | 2 +- src/Resolvers/UserResolver.php | 6 +----- tests/Unit/ProcessDispatchAuditTest.php | 4 ++-- 7 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/Auditable.php b/src/Auditable.php index c909fbb3..e86ab748 100644 --- a/src/Auditable.php +++ b/src/Auditable.php @@ -365,7 +365,11 @@ public function transformAudit(array $data): array * */ protected function resolveUser() - { + { + if (! empty($this->preloadedResolverData['user'] ?? null)) { + return $this->preloadedResolverData['user']; + } + $userResolver = Config::get('audit.user.resolver'); if (is_null($userResolver) && Config::has('audit.resolver') && !Config::has('audit.user.resolver')) { diff --git a/src/AuditableObserver.php b/src/AuditableObserver.php index d3b67ff1..92bd562b 100644 --- a/src/AuditableObserver.php +++ b/src/AuditableObserver.php @@ -105,6 +105,7 @@ protected function dispatchAudit(Auditable $model) return; } + $model->preloadResolverData(); if (!Config::get('audit.queue.enable', false)) { return Auditor::execute($model); } @@ -114,7 +115,7 @@ protected function dispatchAudit(Auditable $model) } // Unload the relations to prevent large amounts of unnecessary data from being serialized. - DispatchAudit::dispatch($model->preloadResolverData()->withoutRelations()); + app()->make('events')->dispatch(new DispatchAudit($model->withoutRelations())); } /** diff --git a/src/Contracts/UserResolver.php b/src/Contracts/UserResolver.php index 2db87fc0..366a0834 100644 --- a/src/Contracts/UserResolver.php +++ b/src/Contracts/UserResolver.php @@ -9,5 +9,5 @@ interface UserResolver * * @return \Illuminate\Contracts\Auth\Authenticatable|null */ - public static function resolve(Auditable $auditable); + public static function resolve(); } diff --git a/src/Events/DispatchAudit.php b/src/Events/DispatchAudit.php index 4ef0a383..28bf8d3c 100644 --- a/src/Events/DispatchAudit.php +++ b/src/Events/DispatchAudit.php @@ -3,12 +3,9 @@ namespace OwenIt\Auditing\Events; use OwenIt\Auditing\Contracts\Auditable; -use Illuminate\Foundation\Events\Dispatchable; class DispatchAudit { - use Dispatchable; - /** * The Auditable model. * diff --git a/src/Resolvers/UrlResolver.php b/src/Resolvers/UrlResolver.php index 8bc63373..0c7a7165 100644 --- a/src/Resolvers/UrlResolver.php +++ b/src/Resolvers/UrlResolver.php @@ -13,7 +13,7 @@ class UrlResolver implements \OwenIt\Auditing\Contracts\Resolver */ public static function resolve(Auditable $auditable): string { - if (! empty($auditable->preloadedResolverData['url'])) { + if (! empty($auditable->preloadedResolverData['url'] ?? null)) { return $auditable->preloadedResolverData['url']; } diff --git a/src/Resolvers/UserResolver.php b/src/Resolvers/UserResolver.php index 80c35f1f..bb04e085 100644 --- a/src/Resolvers/UserResolver.php +++ b/src/Resolvers/UserResolver.php @@ -11,12 +11,8 @@ class UserResolver implements \OwenIt\Auditing\Contracts\UserResolver /** * @return \Illuminate\Contracts\Auth\Authenticatable|null */ - public static function resolve(Auditable $auditable) + public static function resolve() { - if (! empty($auditable->preloadedResolverData['user'])) { - return $auditable->preloadedResolverData['user']; - } - $guards = Config::get('audit.user.guards', [ \config('auth.defaults.guard') ]); diff --git a/tests/Unit/ProcessDispatchAuditTest.php b/tests/Unit/ProcessDispatchAuditTest.php index 0ba4d7fa..80d292aa 100644 --- a/tests/Unit/ProcessDispatchAuditTest.php +++ b/tests/Unit/ProcessDispatchAuditTest.php @@ -38,7 +38,7 @@ public function itGetsProperlyQueued() $model = factory(Article::class)->create(); - DispatchAudit::dispatch($model); + app()->make('events')->dispatch(new DispatchAudit($model)); Queue::assertPushed(CallQueuedListener::class, function ($job) use ($model) { return $job->class == ProcessDispatchAudit::class @@ -60,7 +60,7 @@ public function itCanHaveConnectionAndQueueSet() $model = factory(Article::class)->create(); - DispatchAudit::dispatch($model); + app()->make('events')->dispatch(new DispatchAudit($model)); Queue::assertPushedOn('audits', CallQueuedListener::class, function ($job) use ($model) { $instantiatedJob = new $job->class;