ldc-gen-game-web/app/Models/ActivityLog.php
2026-10-20 12:27:49 +02:00

260 lines
7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use App\Enums\ActivityLogs\ActivityLogsEventEnum;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Carbon;
/**
* @property int $id
* @property string|null $user_id
* @property bool $is_anonymous If there is no user connected when action was realised.
* @property bool $is_console If the action was realised in console.
* @property string $model_class Target model.
* @property string $model_id Id of the target model.
* @property ActivityLogsEventEnum $event Event of this activity (ActivityLogsEventEnum).
* @property array<array-key, mixed>|null $data List of changes (old and new values).
* @property Carbon $created_at The date on which the user was published.
*
* @property-read \App\Models\User $user User BelongsTo relation.
* @property-read \App\Models\StaticPage $staticPage StaticPage BelongsTo relation.
* @property-read \App\Models\Settings $settings Settings BelongsTo relation.
* @property-read \App\Models\Range $range Range BelongsTo relation.
* @property-read \App\Models\Product $product Product BelongsTo relation.
* @property-read \App\Models\Prize $prize Prize BelongsTo relation.
* @property-read \App\Models\Participation $participation Participation BelongsTo relation.
* @property-read \App\Models\InstantWin $instantWin InstantWin BelongsTo relation.
* @property-read \App\Models\FaqTheme $faqTheme FaqTheme BelongsTo relation.
* @property-read \App\Models\FaqQuestion $faqQuestion FaqQuestion BelongsTo relation.
* @property-read \App\Models\Customer $customer Customer BelongsTo relation.
* @property-read \App\Models\Code $code Code BelongsTo relation.
* @property-read \App\Models\BannedIp $bannedIp BannedIp BelongsTo relation.
*/
class ActivityLog extends Model
{
use HasFactory;
use Notifiable;
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
#[\Override]
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
#[\Override]
protected $fillable = [
'is_anonymous',
'model_class',
'model_id',
'event',
'data',
'created_at',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
#[\Override]
protected $casts = [
'is_anonymous' => 'bool',
'is_console' => 'bool',
'event' => ActivityLogsEventEnum::class,
'data' => 'json',
'created_at' => 'datetime',
];
// * METHODS
/**
* Add new activity to the activity logs list.
*/
public static function addActivity(Model $model, ActivityLogsEventEnum $eventEnum): void
{
$isAnonymous = true;
/** @var Model|null */
$userModel = auth('backend')->user();
$user_id = optional($userModel)->id;
if ($userModel instanceof Model) {
$user_id = $userModel->getKeyForSelectQuery();
$isAnonymous = false;
}
$activity = new self();
$activity->user_id = $user_id;
$activity->is_console = app()->runningInConsole();
$activity->is_anonymous = $isAnonymous;
$activity->model_class = $model::class;
$activity->model_id = $model->getKey();
$activity->event = $eventEnum;
$activity->data = static::getChangedColumns($activity, $model, $eventEnum);
$activity->setAttribute('created_at', now());
$activity->saveOrFail();
}
/**
* Get old, new columns that has changed.
*
* @return array<string, string>|null
*/
public static function getChangedColumns(
self $activity,
Model $model,
ActivityLogsEventEnum $eventEnum
): ?array {
if ($eventEnum !== ActivityLogsEventEnum::updated) {
return null;
}
$casts = $model->getCasts();
$changes = $model->getChanges();
return collect($changes)
->map(fn ($val, $key) => $casts[$key] ?? 'string')
->toArray();
}
// * RELATIONS
/**
* User BelongsTo relation.
*
* @return BelongsTo<User, $this>
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* StaticPage BelongsTo relation.
*
* @return BelongsTo<StaticPage, $this>
*/
public function staticPage(): BelongsTo
{
return $this->belongsTo(StaticPage::class);
}
/**
* Settings BelongsTo relation.
*
* @return BelongsTo<Settings, $this>
*/
public function settings(): BelongsTo
{
return $this->belongsTo(Settings::class);
}
/**
* Range BelongsTo relation.
*
* @return BelongsTo<Range, $this>
*/
public function range(): BelongsTo
{
return $this->belongsTo(Range::class);
}
/**
* Product BelongsTo relation.
*
* @return BelongsTo<Product, $this>
*/
public function product(): BelongsTo
{
return $this->belongsTo(Product::class);
}
/**
* Prize BelongsTo relation.
*
* @return BelongsTo<Prize, $this>
*/
public function prize(): BelongsTo
{
return $this->belongsTo(Prize::class);
}
/**
* Participation BelongsTo relation.
*
* @return BelongsTo<Participation, $this>
*/
public function participation(): BelongsTo
{
return $this->belongsTo(Participation::class);
}
/**
* InstantWin BelongsTo relation.
*
* @return BelongsTo<InstantWin, $this>
*/
public function instantWin(): BelongsTo
{
return $this->belongsTo(InstantWin::class);
}
/**
* FaqTheme BelongsTo relation.
*
* @return BelongsTo<FaqTheme, $this>
*/
public function faqTheme(): BelongsTo
{
return $this->belongsTo(FaqTheme::class);
}
/**
* FaqQuestion BelongsTo relation.
*
* @return BelongsTo<FaqQuestion, $this>
*/
public function faqQuestion(): BelongsTo
{
return $this->belongsTo(FaqQuestion::class);
}
/**
* Customer BelongsTo relation.
*
* @return BelongsTo<Customer, $this>
*/
public function customer(): BelongsTo
{
return $this->belongsTo(Customer::class);
}
/**
* Code BelongsTo relation.
*
* @return BelongsTo<Code, $this>
*/
public function code(): BelongsTo
{
return $this->belongsTo(Code::class);
}
/**
* BannedIp BelongsTo relation.
*
* @return BelongsTo<BannedIp, $this>
*/
public function bannedIp(): BelongsTo
{
return $this->belongsTo(BannedIp::class);
}
}