From a189468f60d01cb4b936ae135b553a540d07e893 Mon Sep 17 00:00:00 2001 From: Ahmed abdelaal Date: Fri, 15 May 2020 16:05:00 +0200 Subject: [PATCH 1/5] rearange attributeValuesToBeLogged to be run before getDescriptionForEvent and also pass attributeValuesToBeLogged to getDescriptionForEvent --- src/Traits/LogsActivity.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Traits/LogsActivity.php b/src/Traits/LogsActivity.php index f3bf1d5d..139af9bc 100644 --- a/src/Traits/LogsActivity.php +++ b/src/Traits/LogsActivity.php @@ -21,11 +21,15 @@ protected static function bootLogsActivity() { static::eventsToBeRecorded()->each(function ($eventName) { return static::$eventName(function (Model $model) use ($eventName) { - if (! $model->shouldLogEvent($eventName)) { + if (!$model->shouldLogEvent($eventName)) { return; } + $attrs = $model->attributeValuesToBeLogged($eventName); - $description = $model->getDescriptionForEvent($eventName); + if ($model->isLogEmpty($attrs) && !$model->shouldSubmitEmptyLogs()) { + return; + } + $description = $model->getDescriptionForEvent($eventName, $attrs); $logName = $model->getLogNameToUse($eventName); @@ -33,11 +37,7 @@ protected static function bootLogsActivity() return; } - $attrs = $model->attributeValuesToBeLogged($eventName); - if ($model->isLogEmpty($attrs) && ! $model->shouldSubmitEmptyLogs()) { - return; - } $logger = app(ActivityLogger::class) ->useLog($logName) @@ -55,7 +55,7 @@ protected static function bootLogsActivity() public function shouldSubmitEmptyLogs(): bool { - return ! isset(static::$submitEmptyLogs) ? true : static::$submitEmptyLogs; + return !isset(static::$submitEmptyLogs) ? true : static::$submitEmptyLogs; } public function isLogEmpty($attrs): bool @@ -120,7 +120,7 @@ protected static function eventsToBeRecorded(): Collection public function attributesToBeIgnored(): array { - if (! isset(static::$ignoreChangedAttributes)) { + if (!isset(static::$ignoreChangedAttributes)) { return []; } @@ -131,11 +131,11 @@ protected function shouldLogEvent(string $eventName): bool { $logStatus = app(ActivityLogStatus::class); - if (! $this->enableLoggingModelsEvents || $logStatus->disabled()) { + if (!$this->enableLoggingModelsEvents || $logStatus->disabled()) { return false; } - if (! in_array($eventName, ['created', 'updated'])) { + if (!in_array($eventName, ['created', 'updated'])) { return true; } From f85a9525e16d03331281eea5fca07c932a14dc9a Mon Sep 17 00:00:00 2001 From: Ahmed abdelaal Date: Fri, 15 May 2020 16:28:00 +0200 Subject: [PATCH 2/5] resolve indentation --- src/Traits/LogsActivity.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Traits/LogsActivity.php b/src/Traits/LogsActivity.php index 139af9bc..cea38133 100644 --- a/src/Traits/LogsActivity.php +++ b/src/Traits/LogsActivity.php @@ -21,12 +21,12 @@ protected static function bootLogsActivity() { static::eventsToBeRecorded()->each(function ($eventName) { return static::$eventName(function (Model $model) use ($eventName) { - if (!$model->shouldLogEvent($eventName)) { + if (! $model->shouldLogEvent($eventName)) { return; } $attrs = $model->attributeValuesToBeLogged($eventName); - if ($model->isLogEmpty($attrs) && !$model->shouldSubmitEmptyLogs()) { + if ($model->isLogEmpty($attrs) && ! $model->shouldSubmitEmptyLogs()) { return; } $description = $model->getDescriptionForEvent($eventName, $attrs); @@ -55,7 +55,7 @@ protected static function bootLogsActivity() public function shouldSubmitEmptyLogs(): bool { - return !isset(static::$submitEmptyLogs) ? true : static::$submitEmptyLogs; + return ! isset(static::$submitEmptyLogs) ? true : static::$submitEmptyLogs; } public function isLogEmpty($attrs): bool @@ -120,7 +120,7 @@ protected static function eventsToBeRecorded(): Collection public function attributesToBeIgnored(): array { - if (!isset(static::$ignoreChangedAttributes)) { + if (! isset(static::$ignoreChangedAttributes)) { return []; } @@ -131,11 +131,11 @@ protected function shouldLogEvent(string $eventName): bool { $logStatus = app(ActivityLogStatus::class); - if (!$this->enableLoggingModelsEvents || $logStatus->disabled()) { + if (! $this->enableLoggingModelsEvents || $logStatus->disabled()) { return false; } - if (!in_array($eventName, ['created', 'updated'])) { + if (! in_array($eventName, ['created', 'updated'])) { return true; } From 12652e640a06944f30c4c57544c15a8167858f1d Mon Sep 17 00:00:00 2001 From: Ahmed abdelaal Date: Fri, 15 May 2020 16:29:13 +0200 Subject: [PATCH 3/5] resolve indentation --- src/Traits/LogsActivity.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Traits/LogsActivity.php b/src/Traits/LogsActivity.php index cea38133..659c37a9 100644 --- a/src/Traits/LogsActivity.php +++ b/src/Traits/LogsActivity.php @@ -37,8 +37,6 @@ protected static function bootLogsActivity() return; } - - $logger = app(ActivityLogger::class) ->useLog($logName) ->performedOn($model) From 622a28df7f981ee3e014cfcec222bd5ddc7acb66 Mon Sep 17 00:00:00 2001 From: Ahmed abdelaal Date: Tue, 19 May 2020 00:56:53 +0200 Subject: [PATCH 4/5] #solve signature of getDescriptionForEvent in LogActivityTest and LogActivityTrait --- src/Traits/LogsActivity.php | 14 +++++++------- tests/LogsActivityTest.php | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Traits/LogsActivity.php b/src/Traits/LogsActivity.php index 659c37a9..061a091a 100644 --- a/src/Traits/LogsActivity.php +++ b/src/Traits/LogsActivity.php @@ -21,12 +21,12 @@ protected static function bootLogsActivity() { static::eventsToBeRecorded()->each(function ($eventName) { return static::$eventName(function (Model $model) use ($eventName) { - if (! $model->shouldLogEvent($eventName)) { + if (!$model->shouldLogEvent($eventName)) { return; } $attrs = $model->attributeValuesToBeLogged($eventName); - if ($model->isLogEmpty($attrs) && ! $model->shouldSubmitEmptyLogs()) { + if ($model->isLogEmpty($attrs) && !$model->shouldSubmitEmptyLogs()) { return; } $description = $model->getDescriptionForEvent($eventName, $attrs); @@ -53,7 +53,7 @@ protected static function bootLogsActivity() public function shouldSubmitEmptyLogs(): bool { - return ! isset(static::$submitEmptyLogs) ? true : static::$submitEmptyLogs; + return !isset(static::$submitEmptyLogs) ? true : static::$submitEmptyLogs; } public function isLogEmpty($attrs): bool @@ -80,7 +80,7 @@ public function activities(): MorphMany return $this->morphMany(ActivitylogServiceProvider::determineActivityModel(), 'subject'); } - public function getDescriptionForEvent(string $eventName): string + public function getDescriptionForEvent(string $eventName, array $attributes): string { return $eventName; } @@ -118,7 +118,7 @@ protected static function eventsToBeRecorded(): Collection public function attributesToBeIgnored(): array { - if (! isset(static::$ignoreChangedAttributes)) { + if (!isset(static::$ignoreChangedAttributes)) { return []; } @@ -129,11 +129,11 @@ protected function shouldLogEvent(string $eventName): bool { $logStatus = app(ActivityLogStatus::class); - if (! $this->enableLoggingModelsEvents || $logStatus->disabled()) { + if (!$this->enableLoggingModelsEvents || $logStatus->disabled()) { return false; } - if (! in_array($eventName, ['created', 'updated'])) { + if (!in_array($eventName, ['created', 'updated'])) { return true; } diff --git a/tests/LogsActivityTest.php b/tests/LogsActivityTest.php index b6100c1e..70709df0 100644 --- a/tests/LogsActivityTest.php +++ b/tests/LogsActivityTest.php @@ -261,7 +261,7 @@ public function it_will_not_fail_if_asked_to_replace_from_empty_attribute() use LogsActivity; use SoftDeletes; - public function getDescriptionForEvent(string $eventName): string + public function getDescriptionForEvent(string $eventName ,array $attributes): string { return ":causer.name $eventName"; } From cf0f6dc5325a5ab0497b33fd4dbd0b8b4c9835c2 Mon Sep 17 00:00:00 2001 From: Ahmed abdelaal Date: Tue, 19 May 2020 01:05:11 +0200 Subject: [PATCH 5/5] fix indentation --- src/Traits/LogsActivity.php | 12 ++++++------ tests/LogsActivityTest.php | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Traits/LogsActivity.php b/src/Traits/LogsActivity.php index 061a091a..897dc2fd 100644 --- a/src/Traits/LogsActivity.php +++ b/src/Traits/LogsActivity.php @@ -21,12 +21,12 @@ protected static function bootLogsActivity() { static::eventsToBeRecorded()->each(function ($eventName) { return static::$eventName(function (Model $model) use ($eventName) { - if (!$model->shouldLogEvent($eventName)) { + if (! $model->shouldLogEvent($eventName)) { return; } $attrs = $model->attributeValuesToBeLogged($eventName); - if ($model->isLogEmpty($attrs) && !$model->shouldSubmitEmptyLogs()) { + if ($model->isLogEmpty($attrs) && ! $model->shouldSubmitEmptyLogs()) { return; } $description = $model->getDescriptionForEvent($eventName, $attrs); @@ -53,7 +53,7 @@ protected static function bootLogsActivity() public function shouldSubmitEmptyLogs(): bool { - return !isset(static::$submitEmptyLogs) ? true : static::$submitEmptyLogs; + return ! isset(static::$submitEmptyLogs) ? true : static::$submitEmptyLogs; } public function isLogEmpty($attrs): bool @@ -118,7 +118,7 @@ protected static function eventsToBeRecorded(): Collection public function attributesToBeIgnored(): array { - if (!isset(static::$ignoreChangedAttributes)) { + if (! isset(static::$ignoreChangedAttributes)) { return []; } @@ -129,11 +129,11 @@ protected function shouldLogEvent(string $eventName): bool { $logStatus = app(ActivityLogStatus::class); - if (!$this->enableLoggingModelsEvents || $logStatus->disabled()) { + if (! $this->enableLoggingModelsEvents || $logStatus->disabled()) { return false; } - if (!in_array($eventName, ['created', 'updated'])) { + if (! in_array($eventName, ['created', 'updated'])) { return true; } diff --git a/tests/LogsActivityTest.php b/tests/LogsActivityTest.php index 70709df0..68150291 100644 --- a/tests/LogsActivityTest.php +++ b/tests/LogsActivityTest.php @@ -261,7 +261,7 @@ public function it_will_not_fail_if_asked_to_replace_from_empty_attribute() use LogsActivity; use SoftDeletes; - public function getDescriptionForEvent(string $eventName ,array $attributes): string + public function getDescriptionForEvent(string $eventName, array $attributes): string { return ":causer.name $eventName"; }