ldc-gen-game-web/app/Models/Code.php

187 lines
4.6 KiB
PHP
Raw Normal View History

<?php
2026-10-02 12:09:46 +02:00
declare(strict_types=1);
namespace App\Models;
2026-10-02 12:09:46 +02:00
use App\Traits\Models\HasActivityLog;
2023-12-12 12:25:45 +01:00
use Illuminate\Database\Eloquent\Casts\Attribute;
2026-10-02 12:09:46 +02:00
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
2023-12-12 12:55:05 +01:00
use Illuminate\Database\Eloquent\Relations\HasMany;
2026-10-02 12:09:46 +02:00
use Illuminate\Support\Carbon;
2023-12-12 12:25:45 +01:00
use Illuminate\Support\Facades\Cache;
2026-10-02 12:09:46 +02:00
use Kwaadpepper\Serial\Exceptions\SerialCasterException;
use Kwaadpepper\Serial\SerialCaster;
/**
* Code.
*
2026-10-02 12:09:46 +02:00
* @property string $code_unique The unique code matching this code id.
* @property bool $code_exported Is the code exported ?
* @property Carbon $date_creation Code creation date.
2023-12-12 12:25:45 +01:00
* @property-read string|null $code_unique The unique code
2026-10-02 12:09:46 +02:00
* @property-read bool $has_been_used_too_many_times Has this code been used too many times ?
*
2024-01-31 09:03:18 +00:00
* @method static mixed intToSerial($int) Generate a serial from an int.
* @method static mixed serialToInt($serial) Get an int from a serial.
*
2026-10-02 12:09:46 +02:00
* @property-read Collection|Participation[] $participations
* Associated participations.
2024-02-15 15:53:36 +01:00
* @property-read \App\Models\ActivityLog $activityLogs Activities logs One-to-many relationship.
*/
class Code extends Model
{
2026-10-02 12:09:46 +02:00
use HasActivityLog;
2024-02-15 15:53:36 +01:00
2023-11-23 17:31:28 +01:00
/**
* Indicates if the IDs are auto-incrementing.
*
2026-10-02 12:09:46 +02:00
* @var bool
2023-11-23 17:31:28 +01:00
*/
2026-10-02 12:09:46 +02:00
#[\Override]
2023-11-23 17:31:28 +01:00
public $incrementing = false;
/**
* The attributes that are mass assignable.
*
2026-10-02 12:09:46 +02:00
* @var list<string>
*/
2026-10-02 12:09:46 +02:00
#[\Override]
protected $fillable = [
'code_exported',
2026-10-02 12:09:46 +02:00
'date_creation',
];
2021-08-18 17:08:02 +02:00
2023-11-23 17:31:28 +01:00
/**
* The attributes that should be cast.
*
2024-01-31 09:03:18 +00:00
* @var array<string, string>
2023-11-23 17:31:28 +01:00
*/
2026-10-02 12:09:46 +02:00
#[\Override]
2023-11-23 17:31:28 +01:00
protected $casts = [
'code_exported' => 'bool',
'date_creation' => 'date',
];
/**
* Indicates if the model should be timestamped.
*
2026-10-02 12:09:46 +02:00
* @var bool
*/
2026-10-02 12:09:46 +02:00
#[\Override]
public $timestamps = false;
2023-12-12 12:25:45 +01:00
// * EVENTS
/**
* Perform any actions required after the model boots.
*/
2026-10-02 12:09:46 +02:00
protected static function booted(): void
2023-12-12 12:25:45 +01:00
{
static::created(fn () => Cache::forget('codes.count'));
static::updated(fn () => Cache::forget('codes.count'));
static::deleted(fn () => Cache::forget('codes.count'));
}
// * RELATIONS
/**
* Has many Participation relation
*
2026-10-02 12:09:46 +02:00
* @return HasMany<Participation, $this>
*/
2023-12-12 12:55:05 +01:00
public function participations(): HasMany
2021-08-18 17:08:02 +02:00
{
return $this->hasMany(Participation::class, 'code_unique_id');
}
2024-02-15 15:53:36 +01:00
/**
* Activities logs One-to-many relationship.
*
2026-10-02 12:09:46 +02:00
* @return HasMany<ActivityLog, $this>
2024-02-15 15:53:36 +01:00
*/
2026-10-02 12:09:46 +02:00
public function activityLogs(): HasMany
2024-02-15 15:53:36 +01:00
{
return $this->hasMany(ActivityLog::class);
}
2023-12-12 12:25:45 +01:00
// * METHODS
/**
* Generate code_unique attribute
*/
2026-10-02 12:09:46 +02:00
protected function codeUnique(): Attribute
{
2023-12-12 12:25:45 +01:00
return Attribute::make(
get: function (): ?string {
2026-10-02 12:09:46 +02:00
if (! $this->id || $this->id < 1) {
2023-12-12 12:25:45 +01:00
return null;
}
2026-10-02 12:09:46 +02:00
2023-12-12 12:25:45 +01:00
return self::intToSerial($this->id);
}
);
}
2023-12-01 17:43:41 +01:00
/**
* Get the key used for serial int caster
*/
public static function serialKeyHash(): int
{
return \abs(\crc32(config('app.key')));
}
/**
* Generate a serial from an int.
*
* @return string The serial
2026-10-02 12:09:46 +02:00
*
* @throws SerialCasterException If an error happens du to config.
*/
public static function intToSerial(int $int): string
{
return SerialCaster::encode(
$int,
2023-12-01 17:43:41 +01:00
self::serialKeyHash(),
config('codes.codeLength'),
config('codes.codeCharList')
);
}
/**
* Get an int from a serial.
*
2026-10-02 12:09:46 +02:00
* @return int The ID represented
*
* @throws SerialCasterException If an error happens due to config or
* wrong serial is given.
*/
public static function serialToInt(string $serial): int
{
return SerialCaster::decode(
$serial,
2023-12-01 17:43:41 +01:00
self::serialKeyHash(),
config('codes.codeCharList')
);
}
2023-12-12 12:25:45 +01:00
// * GETTERS
/**
* Get has_been_used_too_namy_times attribute
*/
2026-10-02 12:09:46 +02:00
protected function hasBeenUsedTooManyTimes(): Attribute
2023-12-12 12:25:45 +01:00
{
2026-10-02 12:09:46 +02:00
/** @var Settings */
$settings = resolve(Settings::class);
2023-12-12 12:25:45 +01:00
return Attribute::make(
2026-10-02 12:09:46 +02:00
get: fn (): bool => $settings->nb_unique_code_use && Code::query()->where('id', $this->id)->whereHas(
'participations',
operator: '>=',
count: $settings->nb_unique_code_use
)->exists()
2023-12-12 12:25:45 +01:00
);
}
}