ldc-gen-game-web/app/Models/Codes.php
Jérémy Munsch 9ee2123928
- composer require kwaadpepper/serial-int-caster
- add code parameters into config
- add code length parameter into settings
- add total possible codes to generate in generation page
- add GenerateRequest validation
- changed job ProcessUniqueCode to increase performances using mass insert and pagination
- changed singletoned Settings instance to increase performances
- Fix process export code put datetime in file name
- aded storage to gitignore file
- fixed export to guess export codes, also non recursive now
- changed refactorize a bit EnterCodeController::participate + add request
- Add request to export and generate codes requests
2022-01-07 19:03:25 +01:00

85 lines
1.9 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Kwaadpepper\Serial\SerialCaster;
class Codes extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'code_used', 'code_exported',
'date_creation'
];
/**
* Indicates if the model should be timestamped.
*
* @var boolean
*/
public $timestamps = false;
// * RELATIONS
/**
* Has many Participation relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function participations()
{
return $this->hasMany(Participation::class, 'code_unique_id');
}
// * FUNCTIONS
/**
* Generate code_unique attribute
*
* @return string|null
*/
public function getCodeUniqueAttribute()
{
if (!$this->id or !\is_integer($this->id) or $this->id < 1) {
return null;
}
return self::intToSerial($this->id);
}
/**
* Generate a serial from an int.
*
* @param integer $int
* @return string The serial
* @throws SerialCasterException If an error happens du to config.
*/
public static function intToSerial(int $int): string
{
return SerialCaster::encode(
$int,
\abs(\crc32(config('app.key'))),
app(Settings::class)->code_length,
config('codes.codeCharList')
);
}
/**
* Generate a serial from an int.
*
* @param string $serial
* @return integer 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,
\abs(\crc32(config('app.key'))),
config('codes.codeCharList')
);
}
}