ldc-lionor/app/Models/Range.php
noah.lancien 8278419606 Phpstan
2024-09-06 08:30:55 +00:00

148 lines
5.5 KiB
PHP

<?php
namespace App\Models;
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use App\Traits\Models\HasFlashStatus;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
/**
* Gamme
*
* @property string $name The name.
* @property string $slug The slug.
* @property string $subtitle The subtitle.
* @property string $seo_title The seo title for referencement.
* @property string $seo_description The seo description.
* @property string $banner The banner path.
* @property string $banner_alt The banner alternative text.
* @property string $banner_title The banner alternative title.
* @property string $picture The picture path.
* @property string $picture_alt The picture alternative text.
* @property string $picture_title The picture alternative title.
* @property string $sticker The sticker path.
* @property string $sticker_alt The sticker alternative text.
* @property string $sticker_title The sticker alternative title.
* @property string $description The description html text.
* @property string|null $preparation The preparation html text.
* @property string $ingredients The ingredients html text.
* @property string $info_cooking The cooking info label.
* @property string $info_consumption The consumption info label.
* @property \App\Enums\CookingModeEnum $cooking_mode The cooking mode enum for svg sticker.
* @property \App\Enums\NutriscoreEnum $nutriscore The nutriscore enum for svg sticker.
* @property object<string[]> $nutritional_values The nutritional values as a value-label list.
* @property boolean $status The publication status.
* @property integer $range_id The range associated.
*
* @package App\Models
*/
class Range extends Model
{
use HasFactory;
use HasFlashStatus;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'slug',
'subtitle',
'seo_title',
'seo_description',
'picture',
'picture_alt',
'picture_title',
'banner',
'banner_alt',
'banner_title',
'sticker',
'sticker_alt',
'sticker_title',
'status'
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function () {
throw new \RuntimeException('Impossible de créer une gamme');
});
static::updating(function (self $model) {
// * ASSERTIONS
self::assertFieldIsUnique('name', $model->name, $model->id);
self::assertAtLeastOneIsPublished($model);
// * TRANSFORMATIONS
$model->slug = Str::slug($model->name);
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
$model->banner = FileStorageHelper::storeFile($model, $model->banner, true);
$model->sticker = FileStorageHelper::storeFile($model, $model->sticker, true);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'picture');
FileStorageHelper::removeOldFile($model, 'banner');
FileStorageHelper::removeOldFile($model, 'sticker');
static::flashStatus($model, true);
});
static::deleting(function () {
throw new \RuntimeException('Impossible de supprimer une gamme');
});
}
// * METHODS
/**
* 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)
{
$table = (new self())->getTable();
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id);
}
/**
* Asserts using validation that the name is unique
*
* @param self $model
* @return void
* @throws \Illuminate\Validation\ValidationException If no model type is published.
*/
private static function assertAtLeastOneIsPublished(self $model)
{
if (!$model->status and self::query()->where('status', true)->count() < 2) {
throw ValidationException::withMessages([
'error' => trans('Au moins un(e) :model doit être publié, impossible de dépublier', [
'model' => trans('models.classes.' . $model->getTable())
])
]);
}
}
// * RELATIONSHIPS
/**
* BelongsToMany certifications relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function products()
{
return $this->hasMany(Product::class);
}
}