Changed event history to type

This commit is contained in:
Jérémy Munsch 2022-07-25 11:37:01 +02:00
parent 01cef75e0c
commit 50a5aa5604
No known key found for this signature in database
GPG key ID: 18361AF3BFEC0FEC
5 changed files with 113 additions and 9 deletions

View file

@ -0,0 +1,29 @@
<?php
namespace App\Enums;
enum EventHistoryType: int
{
case created = 0;
case moved = 1;
case changed = 2;
case destroyed = 3;
case labelled = 4;
/**
* Get num label
*
* @return string
*/
public function label(): string
{
return match ($this) {
static::created => trans('Création'),
static::moved => trans('Déplacement'),
static::changed => trans('Modification'),
static::destroyed => trans('Suppression'),
static::labelled => trans('Etiquette'),
default => trans('MISSING LABEL')
};
}
}

View file

@ -2,6 +2,7 @@
namespace App\Listeners;
use App\Enums\EventHistoryType;
use App\Events\MediaLinkWithArcadia;
use App\Lib\ArcadiaLinkUtilities;
use App\Models\Historique;
@ -21,7 +22,7 @@ class CreateIntoArcadia
$media = $event->getMedia();
$historique = new Historique();
$historique->type = trans('Modification');
$historique->type = EventHistoryType::changed;
$historique->description = trans('Ajout de la liaison Arcadia');
$historique->media()->associate($media);
$historique->user()->associate($user);

View file

@ -2,6 +2,7 @@
namespace App\Listeners;
use App\Enums\EventHistoryType;
use App\Events\MediaChangedLabel;
use App\Events\MediaCreated;
use App\Events\MediaDestroyed;
@ -20,29 +21,27 @@ class HistorizeMediaEvent
*/
public function handle(MediaCreated $event)
{
$historique = new Historique();
// TODO: put enum in here.
$type = trans('Création');
$historique = new Historique();
$type = EventHistoryType::created;
$description = trans('Média ajouté');
if ($event instanceof MediaMoved) {
$type = trans('Déplacement');
$type = EventHistoryType::moved;
$description = $event->getMedia()->dossier_id > 0 ?
trans('Déplacement dans le dossier :libelle', [
'libelle' => $event->getMedia()->dossier->libelle
]) : trans('Média déplacé à la racine');
}
if ($event instanceof MediaUpdated) {
$type = trans('Modification');
$type = EventHistoryType::changed;
$description = trans('Le média a été mis à jour');
}
if ($event instanceof MediaDestroyed) {
$type = trans('Suppression');
$type = EventHistoryType::destroyed;
$description = trans('Le média a été supprimé');
}
if ($event instanceof MediaChangedLabel) {
$type = trans('Etiquette');
$type = EventHistoryType::labelled;
$description = sprintf(
'%s -> %s',
\optional($event->getOldLabel())->libelle ?? trans('aucune'),

View file

@ -2,6 +2,7 @@
namespace App\Models;
use App\Enums\EventHistoryType;
use Illuminate\Database\Eloquent\Model;
class Historique extends Model
@ -13,6 +14,15 @@ class Historique extends Model
*/
public $timestamps = false;
/**
* The attributes that should be cast.
*
* @var array
*/
public $casts = [
'type' => EventHistoryType::class
];
// * RELATIONSHIPS
/**

View file

@ -0,0 +1,65 @@
<?php
use App\Enums\EventHistoryType;
use App\Models\Historique;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Historique::all()->map(function (Historique $historique) {
if (in_array(strtolower($historique->getRawOriginal('type')), ['retouche'])) {
$historique->forceDelete();
return;
}
$historique->type = match (strtolower($historique->getRawOriginal('type'))) {
'création' => EventHistoryType::created,
'déplacement' => EventHistoryType::moved,
'modification' => EventHistoryType::changed,
'suppression' => EventHistoryType::destroyed,
'etiquette' => EventHistoryType::labelled,
default => throw new \RuntimeException($historique->getRawOriginal('type') . ' not handled')
};
$historique->setRawAttributes(array_merge($historique->toArray(), [
'type' => $historique->type->value
]));
return $historique;
})->filter()->map(function (Historique $historique) {
$historique->saveOrFail();
});
Schema::table('historiques', function (Blueprint $table) {
$table->unsignedInteger('type')->default(EventHistoryType::created->value)
->comment('Enum EventHistoryType')->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Historique::all()->map(function (Historique $historique) {
$historique->type = match ($historique->getRawOriginal('type')) {
EventHistoryType::created->value => 'Création',
EventHistoryType::changed->value => 'Modification',
EventHistoryType::destroyed->value => 'Suppression',
EventHistoryType::labelled->value => 'Etiquette',
EventHistoryType::moved->value => 'Déplacement',
default => throw new \RuntimeException($historique->getRawOriginal('type') . 'not handled')
};
});
Schema::table('historiques', function (Blueprint $table) {
$table->string('type')->default('')->change();
});
}
};