From dbccdeb5b7781c40fea23e3a6cdcdf058c5b2bd5 Mon Sep 17 00:00:00 2001 From: Kovah Date: Fri, 10 Jun 2022 14:48:58 +0200 Subject: [PATCH] Refactor history entry components --- app/Models/Setting.php | 2 +- app/Models/User.php | 2 +- app/View/Components/History/LinkEntry.php | 59 ++--------------- app/View/Components/History/ListEntry.php | 59 ++--------------- .../Components/History/ProcessesHistory.php | 59 +++++++++++++++++ app/View/Components/History/SettingsEntry.php | 13 +++- app/View/Components/History/TagEntry.php | 59 ++--------------- app/View/Components/History/UserEntry.php | 66 ++++--------------- 8 files changed, 102 insertions(+), 217 deletions(-) create mode 100644 app/View/Components/History/ProcessesHistory.php diff --git a/app/Models/Setting.php b/app/Models/Setting.php index 8aeb6ed6..0a9a98ef 100644 --- a/app/Models/Setting.php +++ b/app/Models/Setting.php @@ -49,7 +49,7 @@ class Setting extends Model implements Auditable 'updated', ]; - public static array $auditModifiers = [ + public array $auditModifiers = [ 'archive_backups_enabled' => BooleanModifier::class, 'archive_private_backups_enabled' => BooleanModifier::class, 'darkmode_setting' => DarkmodeSettingModifier::class, diff --git a/app/Models/User.php b/app/Models/User.php index 1e624e19..81c9f46d 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -64,7 +64,7 @@ class User extends Authenticatable implements Auditable 'email', ]; - public static array $auditModifiers = []; + public array $auditModifiers = []; /* * ======================================================================== diff --git a/app/View/Components/History/LinkEntry.php b/app/View/Components/History/LinkEntry.php index 8b17001b..1332922f 100644 --- a/app/View/Components/History/LinkEntry.php +++ b/app/View/Components/History/LinkEntry.php @@ -30,7 +30,12 @@ use OwenIt\Auditing\Models\Audit; */ class LinkEntry extends Component { - public function __construct(private Audit $entry, private array $changes = []) + use ProcessesHistory; + + private string $model = Link::class; + private string $translateBase = 'link'; + + public function __construct(private Audit $entry) { } @@ -53,56 +58,4 @@ class LinkEntry extends Component 'changes' => $this->changes, ]); } - - protected function processChange(string $field, array $changeData): void - { - $fieldName = trans('link.' . $field); - [$oldValue, $newValue] = $this->processValues($field, $changeData); - - if ($oldValue === null) { - $change = trans('linkace.history_added', [ - 'fieldname' => $fieldName, - 'newvalue' => htmlspecialchars($newValue), - ]); - } elseif ($newValue === null) { - $change = trans('linkace.history_removed', [ - 'fieldname' => $fieldName, - 'oldvalue' => htmlspecialchars($oldValue), - ]); - } else { - $change = trans('linkace.history_changed', [ - 'fieldname' => $fieldName, - 'oldvalue' => htmlspecialchars($oldValue), - 'newvalue' => htmlspecialchars($newValue), - ]); - } - - $this->changes[] = $change; - } - - /** - * Apply specialized methods for different fields to handle particular - * formatting needs of these fields. - * - * @param string $field - * @param array $changeData - * @return array - */ - protected function processValues(string $field, array $changeData): array - { - $oldValue = $changeData['old'] ?? null; - $newValue = $changeData['new'] ?? null; - - /** @var Link $model */ - $model = app($this->entry->auditable_type); - - if (isset($model->auditModifiers[$field])) { - $modifier = app($model->auditModifiers[$field]); - $oldValue = $modifier->modify($oldValue); - $newValue = $modifier->modify($newValue); - return [$oldValue, $newValue]; - } - - return [$oldValue, $newValue]; - } } diff --git a/app/View/Components/History/ListEntry.php b/app/View/Components/History/ListEntry.php index 485e056d..b770942e 100644 --- a/app/View/Components/History/ListEntry.php +++ b/app/View/Components/History/ListEntry.php @@ -8,7 +8,12 @@ use OwenIt\Auditing\Models\Audit; class ListEntry extends Component { - public function __construct(private Audit $entry, private array $changes = []) + use ProcessesHistory; + + private string $model = LinkList::class; + private string $translateBase = 'list'; + + public function __construct(private Audit $entry) { } @@ -31,56 +36,4 @@ class ListEntry extends Component 'changes' => $this->changes, ]); } - - protected function processChange(string $field, array $changeData): void - { - $fieldName = trans('list.' . $field); - [$oldValue, $newValue] = $this->processValues($field, $changeData); - - if ($oldValue === null) { - $change = trans('linkace.history_added', [ - 'fieldname' => $fieldName, - 'newvalue' => htmlspecialchars($newValue), - ]); - } elseif ($newValue === null) { - $change = trans('linkace.history_removed', [ - 'fieldname' => $fieldName, - 'oldvalue' => htmlspecialchars($oldValue), - ]); - } else { - $change = trans('linkace.history_changed', [ - 'fieldname' => $fieldName, - 'oldvalue' => htmlspecialchars($oldValue), - 'newvalue' => htmlspecialchars($newValue), - ]); - } - - $this->changes[] = $change; - } - - /** - * Apply specialized methods for different fields to handle particular - * formatting needs of these fields. - * - * @param string $field - * @param array $changeData - * @return array - */ - protected function processValues(string $field, array $changeData): array - { - $oldValue = $changeData['old'] ?? null; - $newValue = $changeData['new'] ?? null; - - /** @var LinkList $model */ - $model = app($this->entry->auditable_type); - - if (isset($model->auditModifiers[$field])) { - $modifier = app($model->auditModifiers[$field]); - $oldValue = $modifier->modify($oldValue); - $newValue = $modifier->modify($newValue); - return [$oldValue, $newValue]; - } - - return [$oldValue, $newValue]; - } } diff --git a/app/View/Components/History/ProcessesHistory.php b/app/View/Components/History/ProcessesHistory.php new file mode 100644 index 00000000..bbcdcb23 --- /dev/null +++ b/app/View/Components/History/ProcessesHistory.php @@ -0,0 +1,59 @@ +translateBase . '.' . $field); + [$oldValue, $newValue] = $this->processValues($field, $changeData); + + if ($oldValue === null) { + $change = trans('linkace.history_added', [ + 'fieldname' => $fieldName, + 'newvalue' => htmlspecialchars($newValue), + ]); + } elseif ($newValue === null) { + $change = trans('linkace.history_removed', [ + 'fieldname' => $fieldName, + 'oldvalue' => htmlspecialchars($oldValue), + ]); + } else { + $change = trans('linkace.history_changed', [ + 'fieldname' => $fieldName, + 'oldvalue' => htmlspecialchars($oldValue), + 'newvalue' => htmlspecialchars($newValue), + ]); + } + + $this->changes[] = $change; + } + + /** + * Apply specialized methods for different fields to handle particular + * formatting needs of these fields. + * + * @param string $field + * @param array $changeData + * @return array + */ + protected function processValues(string $field, array $changeData): array + { + $oldValue = $changeData['old'] ?? null; + $newValue = $changeData['new'] ?? null; + + $model = app($this->model); + + if (isset($model->auditModifiers[$field])) { + $modifier = app($model->auditModifiers[$field]); + $oldValue = $modifier->modify($oldValue); + $newValue = $modifier->modify($newValue); + return [$oldValue, $newValue]; + } + + return [$oldValue, $newValue]; + } +} diff --git a/app/View/Components/History/SettingsEntry.php b/app/View/Components/History/SettingsEntry.php index 28b48000..ee783f50 100644 --- a/app/View/Components/History/SettingsEntry.php +++ b/app/View/Components/History/SettingsEntry.php @@ -8,7 +8,12 @@ use OwenIt\Auditing\Models\Audit; class SettingsEntry extends Component { - public function __construct(private Audit $entry, private array $changes = []) + use ProcessesHistory; + + private string $model = Setting::class; + private string $translateBase = 'settings'; + + public function __construct(private Audit $entry) { } @@ -86,8 +91,10 @@ class SettingsEntry extends Component $field = 'share_service'; } - if (isset(Setting::$auditModifiers[$field])) { - $modifier = app(Setting::$auditModifiers[$field]); + $model = app(Setting::class); + + if (isset($model->auditModifiers[$field])) { + $modifier = app($model->auditModifiers[$field]); $oldValue = $modifier->modify($oldValue); $newValue = $modifier->modify($newValue); return [$oldValue, $newValue]; diff --git a/app/View/Components/History/TagEntry.php b/app/View/Components/History/TagEntry.php index 0bbcaea6..568f58ab 100644 --- a/app/View/Components/History/TagEntry.php +++ b/app/View/Components/History/TagEntry.php @@ -8,7 +8,12 @@ use OwenIt\Auditing\Models\Audit; class TagEntry extends Component { - public function __construct(private Audit $entry, private array $changes = []) + use ProcessesHistory; + + private string $model = Tag::class; + private string $translateBase = 'tag'; + + public function __construct(private Audit $entry) { } @@ -31,56 +36,4 @@ class TagEntry extends Component 'changes' => $this->changes, ]); } - - protected function processChange(string $field, array $changeData): void - { - $fieldName = trans('tag.' . $field); - [$oldValue, $newValue] = $this->processValues($field, $changeData); - - if ($oldValue === null) { - $change = trans('linkace.history_added', [ - 'fieldname' => $fieldName, - 'newvalue' => htmlspecialchars($newValue), - ]); - } elseif ($newValue === null) { - $change = trans('linkace.history_removed', [ - 'fieldname' => $fieldName, - 'oldvalue' => htmlspecialchars($oldValue), - ]); - } else { - $change = trans('linkace.history_changed', [ - 'fieldname' => $fieldName, - 'oldvalue' => htmlspecialchars($oldValue), - 'newvalue' => htmlspecialchars($newValue), - ]); - } - - $this->changes[] = $change; - } - - /** - * Apply specialized methods for different fields to handle particular - * formatting needs of these fields. - * - * @param string $field - * @param array $changeData - * @return array - */ - protected function processValues(string $field, array $changeData): array - { - $oldValue = $changeData['old'] ?? null; - $newValue = $changeData['new'] ?? null; - - /** @var Tag $model */ - $model = app($this->entry->auditable_type); - - if (isset($model->auditModifiers[$field])) { - $modifier = app($model->auditModifiers[$field]); - $oldValue = $modifier->modify($oldValue); - $newValue = $modifier->modify($newValue); - return [$oldValue, $newValue]; - } - - return [$oldValue, $newValue]; - } } diff --git a/app/View/Components/History/UserEntry.php b/app/View/Components/History/UserEntry.php index caeaaa91..3493fadb 100644 --- a/app/View/Components/History/UserEntry.php +++ b/app/View/Components/History/UserEntry.php @@ -8,7 +8,12 @@ use OwenIt\Auditing\Models\Audit; class UserEntry extends Component { - public function __construct(private Audit $entry, private array $changes = []) + use ProcessesHistory; + + private string $model = User::class; + private string $translateBase = 'user'; + + public function __construct(private Audit $entry) { } @@ -25,6 +30,13 @@ class UserEntry extends Component } else { foreach ($this->entry->getModified() as $field => $change) { $this->processChange($field, $change); + + $this->changes = array_map(function ($change) { + return trans('audit.user_history_entry', [ + 'id' => $this->entry->auditable->id, + 'change' => $change, + ]); + }, $this->changes); } } @@ -33,56 +45,4 @@ class UserEntry extends Component 'changes' => $this->changes, ]); } - - protected function processChange(string $field, array $changeData): void - { - $fieldName = trans('user.' . $field); - [$oldValue, $newValue] = $this->processValues($field, $changeData); - - if ($oldValue === null) { - $change = trans('linkace.history_added', [ - 'fieldname' => $fieldName, - 'newvalue' => htmlspecialchars($newValue), - ]); - } elseif ($newValue === null) { - $change = trans('linkace.history_removed', [ - 'fieldname' => $fieldName, - 'oldvalue' => htmlspecialchars($oldValue), - ]); - } else { - $change = trans('linkace.history_changed', [ - 'fieldname' => $fieldName, - 'oldvalue' => htmlspecialchars($oldValue), - 'newvalue' => htmlspecialchars($newValue), - ]); - } - - $this->changes[] = trans('audit.user_history_entry', [ - 'id' => $this->entry->auditable->id, - 'change' => $change, - ]); - } - - /** - * Apply specialized methods for different fields to handle particular - * formatting needs of these fields. - * - * @param string $field - * @param array $changeData - * @return array - */ - protected function processValues(string $field, array $changeData): array - { - $oldValue = $changeData['old'] ?? null; - $newValue = $changeData['new'] ?? null; - - if (isset(User::$auditModifiers[$field])) { - $modifier = app(User::$auditModifiers[$field]); - $oldValue = $modifier->modify($oldValue); - $newValue = $modifier->modify($newValue); - return [$oldValue, $newValue]; - } - - return [$oldValue, $newValue]; - } }