From c8e0f715936586468d79fe3eef5b30e707357009 Mon Sep 17 00:00:00 2001 From: Noah Lancien Date: Tue, 2 Jul 2024 12:56:11 +0200 Subject: [PATCH] fix: updated activity-logs from le-gaulois --- app/Models/ActivityLog.php | 77 ++++++++++++++++++++++++++++++-------- composer.json | 2 +- 2 files changed, 63 insertions(+), 16 deletions(-) diff --git a/app/Models/ActivityLog.php b/app/Models/ActivityLog.php index becd2d1..ec84151 100644 --- a/app/Models/ActivityLog.php +++ b/app/Models/ActivityLog.php @@ -5,8 +5,7 @@ namespace App\Models; use App\Enums\ActivityLogs\ActivityLogsEventEnum; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; -use Illuminate\Notifications\Notifiable; -use Illuminate\Support\Facades\Schema; +use Illuminate\Support\Str; /** * @property string $model Target model. @@ -33,7 +32,6 @@ use Illuminate\Support\Facades\Schema; class ActivityLog extends Model { use HasFactory; - use Notifiable; /** * Indicates if the model should be timestamped. @@ -62,7 +60,7 @@ class ActivityLog extends Model * @var array */ protected $casts = [ - 'is_anonymous' => 'bool', + 'is_anonymous' => 'boolean', 'event' => ActivityLogsEventEnum::class, 'data' => 'json', 'created_at' => 'datetime', @@ -111,22 +109,71 @@ class ActivityLog extends Model { $modelClassName = $activity->model_class; /** Get type of each fields of the target model */ + /** @var \Illuminate\Database\Eloquent\Model|null $targetModel */ $targetModel = $activity->model_class::where( (new $modelClassName())->getRouteKeyName(), $activity->model_id )->first(); - if ($targetModel != null) { - // phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed - $targetModelTypes = collect($targetModel->toArray()) - ->map(function ($field, $fieldIndex) use ($targetModel) { - return Schema::getColumnType($targetModel->getTable(), $fieldIndex); - })->toArray(); - // phpcs:enable - /** Return old, new and type of values changed */ - return ($eventEnum === ActivityLogsEventEnum::updated) ? - array_intersect_key($targetModelTypes, $model->getChanges()) : []; + + if (\is_null($targetModel)) { + return []; } - return []; + $targetModelTypes = collect($targetModel->toArray()) + ->map(fn ($value) => self::getValueType($value)) + ->toArray(); + /** Return old, new and type of values changed */ + return match ($eventEnum) { + ActivityLogsEventEnum::created => array_intersect_key($targetModelTypes, $model->toArray()), + ActivityLogsEventEnum::duplicated => array_intersect_key($targetModelTypes, $model->toArray()), + ActivityLogsEventEnum::updated => array_intersect_key($targetModelTypes, $model->getChanges()), + default => [] + }; + } + + + /** + * Get a value type as a string + * ! Use this for display purpose only + * + * @param mixed $value + * @return string + * @throws \RuntimeException If type is unhlanded. + * @phpcs:disable Generic.Metrics.CyclomaticComplexity.MaxExceeded + */ + private static function getValueType(mixed $value): string + { + // phpcs:enable + $type = \gettype($value); + switch ($type) { + // phpcs:ignore PSR2.ControlStructures.SwitchDeclaration.TerminatingComment + case 'string': + return match (true) { + Str::length(\strip_tags($value)) !== Str::length($value) => 'html', + Str::startsWith($value, 'storage/modelfiles') => 'file', + Str::isUuid($value) => 'uuid', + Str::isUlid($value) => 'ulid', + Str::isUrl($value) => 'url', + Str::isMatch('/^[0-9A-Za-z_-]{10}[048AEIMQUYcgkosw]$/', $value) => 'youtube_video_id', + \is_numeric($value) => 'numeric', + Str::isJson($value) and + (Str::startsWith($value, '[') or Str::startsWith($value, '{')) => 'json', + default => 'string' + }; + case 'NULL': + case 'object': + case 'array': + case 'integer': + case 'boolean': + return $type; + case 'double': + return is_float($value) ? 'float' : 'double'; + case 'resource (closed)': + case 'resource': + throw new \RuntimeException('Laravel model properties should not contain resources !'); + case 'unknown type': + default: + throw new \RuntimeException('Unhandled Type ' . \gettype($value)); + } //end switch } // * RELATIONS diff --git a/composer.json b/composer.json index 7121112..0b87401 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "uwil/gen-game-web", - "version": "2.4.1", + "version": "2.4.2", "type": "project", "description": "Game generator", "support": {