ldc-tregalette/app/Models/Range.php
2023-12-15 17:49:43 +01:00

153 lines
5.7 KiB
PHP

<?php
namespace App\Models;
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use App\Traits\Models\IsCachedAsCollection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
/**
* @property string $label Label de la gamme.
* @property string $slug Slug à partir du label de la gamme.
* @property string $picture Path de l'image de la gamme (579px * 579px).
* @property string $picture_alt Attribut alt de l'image de la gamme.
* @property string $picture_title Attribut title de l'image de la gamme.
* @property string $picture_ambiance Path de l'image d'ambiance de la gamme (333px * 333px).
* @property string $picture_ambiance_alt Attribut alt de l'image d'ambiance de la gamme.
* @property string $picture_ambiance_title Attribut title de l'image d'ambiance de la gamme.
* @property string|null $picto1_picture Path de l'image du picto 1 (67px * 70px).
* @property string|null $picto1_picture_alt Attribut alt de l'image du picto 1.
* @property string|null $picto1_picture_title Attribut title de l'image du picto 1.
* @property string|null $picto1_line1 Première ligne du texte du picto1.
* @property string|null $picto1_line2 Deuxième ligne du texte du picto1.
* @property string|null $picto2_picture Path de l'image du picto 1 (67px * 70px).
* @property string|null $picto2_picture_alt Attribut alt de l'image du picto 1.
* @property string|null $picto2_picture_title Attribut title de l'image du picto 1.
* @property string|null $picto2_line1 Première ligne du texte du picto2.
* @property string|null $picto2_line2 Deuxième ligne du texte du picto2.
* @property-read \Carbon\Carbon $created_at Created date.
* @property-read \Carbon\Carbon $updated_at Updated date.
*
* @property-read \App\Models\Product $products Product Has Many relationship.
*/
class Range extends Model
{
use IsCachedAsCollection;
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'label',
'picture',
'picture_alt',
'picture_title',
'picture_ambiance',
'picture_ambiance_alt',
'picture_ambiance_title',
'picto1_picture',
'picto1_picture_alt',
'picto1_picture_title',
'picto1_line1',
'picto1_line2',
'picto2_picture',
'picto2_picture_alt',
'picto2_picture_title',
'picto2_line1',
'picto2_line2'
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function () {
throw new \RuntimeException("Creating a new range is not authorized");
});
static::created(fn () => self::removeCached());
static::updating(function (self $model) {
self::manageFields($model);
});
static::updated(function (self $model) {
self::removeCached();
FileStorageHelper::removeOldFile($model, 'picture');
FileStorageHelper::removeOldFile($model, 'picture_ambiance');
FileStorageHelper::removeOldFile($model, 'picto1_picture');
FileStorageHelper::removeOldFile($model, 'picto2_picture');
});
static::deleting(function () {
throw new \RuntimeException('Deleting a range is not authorized');
});
static::deleted(fn () => self::removeCached());
}
// * METHODS
/**
* Manage model fields.
*
* @param self $model
* @return void
*/
private static function manageFields(self $model): void
{
// * ASSERTIONS
self::assertFieldIsUnique('label', $model->label, $model->id);
self::assertFieldIsUnique('slug', Str::slug($model->label), $model->id);
// * TRANSFORMATIONS
self::setSlug($model, $model->label);
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
$model->picture_ambiance = FileStorageHelper::storeFile($model, $model->picture_ambiance, true);
$model->picto1_picture = FileStorageHelper::storeFile($model, $model->picto1_picture, true);
$model->picto2_picture = FileStorageHelper::storeFile($model, $model->picto2_picture, true);
}
/**
* Asserts using validation that the field is unique.
*
* @param string $nameField
* @param string $valueField
* @param integer|null $id
* @return void
*/
private static function assertFieldIsUnique(string $nameField, string $valueField, ?int $id = null): void
{
$table = (new self())->getTable();
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id);
}
/**
* Set the slug.
*
* @param self $model
* @param string $original
*
* @return void
*/
private static function setSlug(self $model, string $original): void
{
$model->slug = Str::slug($original);
}
// * RELATIONS
/**
* Get the range that owns the product.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function products(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(Product::class);
}
}