Skip to content

Commit

Permalink
ref: LogsModel trait
Browse files Browse the repository at this point in the history
  • Loading branch information
fzldn committed Sep 20, 2024
1 parent 7bab53a commit e160237
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
9 changes: 5 additions & 4 deletions app/Models/PermissionRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use App\Models\Traits\LogsModel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\Pivot;

Expand All @@ -20,24 +21,24 @@ public function getTable()
return config('permission.table_names.role_has_permissions', parent::getTable());
}

protected function getLogFirstSubject(): object
protected function getLogFirstSubject(): Model
{
return $this->permission;
}

protected function getLogFirstSubjectName(): string
{
return $this->permission->label;
return $this->getLogFirstSubject()->label;
}

protected function getLogSecondSubject(): object
protected function getLogSecondSubject(): Model
{
return $this->role;
}

protected function getLogSecondSubjectName(): string
{
return $this->role->name;
return $this->getLogSecondSubject()->name;
}

/**
Expand Down
9 changes: 5 additions & 4 deletions app/Models/RoleUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use App\Models\Traits\LogsModel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphPivot;
use Illuminate\Database\Eloquent\Relations\MorphTo;
Expand All @@ -21,24 +22,24 @@ public function getTable()
return config('permission.table_names.model_has_roles', parent::getTable());
}

protected function getLogFirstSubject(): object
protected function getLogFirstSubject(): Model
{
return $this->role;
}

protected function getLogFirstSubjectName(): string
{
return $this->role->name;
return $this->getLogFirstSubject()->name;
}

protected function getLogSecondSubject(): object
protected function getLogSecondSubject(): Model
{
return $this->user;
}

protected function getLogSecondSubjectName(): string
{
return $this->user->name;
return $this->getLogSecondSubject()->name;
}

/**
Expand Down
12 changes: 6 additions & 6 deletions app/Models/Traits/LogsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use App\Support\LogMasksAttribute;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Support\HtmlString;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;

Expand Down Expand Up @@ -57,7 +57,7 @@ protected function getLogCauserName(): string
return __('System');
}

protected function getLogFirstSubject(): object
protected function getLogFirstSubject(): Model
{
return $this;
}
Expand All @@ -67,7 +67,7 @@ protected function getLogFirstSubjectName(): string
return $this->name;
}

protected function getLogSecondSubject(): object
protected function getLogSecondSubject(): Model
{
return $this;
}
Expand All @@ -80,7 +80,7 @@ protected function getLogSecondSubjectName(): string
protected function logDescription(string $eventName): string
{
return __(':subject.type :subject.name was :event by :causer.name', [
'subject.type' => str(class_basename(get_class($this)))->headline(),
'subject.type' => str(class_basename($this))->headline(),
'subject.name' => str(e($this->getLogSubjectName()))->wrapHtmlTag('strong'),
'event' => str($eventName)->wrapHtmlTag('em')->wrapHtmlTag('strong'),
'causer.name' => str(e($this->getLogCauserName()))->wrapHtmlTag('strong'),
Expand All @@ -90,7 +90,7 @@ protected function logDescription(string $eventName): string
protected function logDescriptionPivot(string $eventName): string
{
return __(':subject.first.type :subject.first.name was :event :to :subject.second.type :subject.second.name by :causer.name', [
'subject.first.type' => str(class_basename(get_class($this->getLogFirstSubject())))->headline(),
'subject.first.type' => str(class_basename($this->getLogFirstSubject()))->headline(),
'subject.first.name' => str(e($this->getLogFirstSubjectName()))->wrapHtmlTag('strong'),
'event' => str(match ($eventName) {
'created' => __('attached'),
Expand All @@ -102,7 +102,7 @@ protected function logDescriptionPivot(string $eventName): string
'deleted' => __('from'),
default => __('for'),
},
'subject.second.type' => str(class_basename(get_class($this->getLogSecondSubject())))->headline(),
'subject.second.type' => str(class_basename($this->getLogSecondSubject()))->headline(),
'subject.second.name' => str(e($this->getLogSecondSubjectName()))->wrapHtmlTag('strong'),
'causer.name' => str(e($this->getLogCauserName()))->wrapHtmlTag('strong'),
]);
Expand Down

0 comments on commit e160237

Please sign in to comment.