ldc-tregalette/app/Models/Reduction.php
2023-12-22 15:09:55 +01:00

80 lines
2.3 KiB
PHP

<?php
namespace App\Models;
use App\Lib\Helpers\FileStorageHelper;
use App\Traits\Models\ActivityLog;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @property string $picture_packshot Path de l'image du packshot. A prendre depuis MesBR, sinon
* image (266px * 266px).
* @property string $picture_packshot_alt Attribut alt de l'image du packshot.
* @property string $picture_packshot_title Attribut title de l'image du packshot.
* @property integer $amount Non administrable, récupéré depuis MesBR.
* @property string $validity A récupérer dans MesBR, sinon texte administrable.
* @property-read \Carbon\Carbon $created_at Created date.
* @property-read \Carbon\Carbon $updated_at Updated date.
*/
class Reduction extends Model
{
use ActivityLog;
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'picture_packshot',
'picture_packshot_alt',
'picture_packshot_title',
'validity',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'published' => 'boolean',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (self $model) {
self::manageFields($model);
});
static::updating(function (self $model) {
self::manageFields($model);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'picture_packshot');
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'picture_packshot');
});
}
// * METHODS
/**
* Manage model fields.
*
* @param self $model
* @return void
*/
private static function manageFields(self $model): void
{
$model->picture_packshot = FileStorageHelper::storeFile($model, $model->picture_packshot, true);
}
}