ldc-le-gaulois-pro/app/Models/ExpertiseProduct.php
Jérémy Munsch 3a0d4e9d20
PHPstan OK
2024-02-05 09:33:08 +01:00

126 lines
4.2 KiB
PHP

<?php
namespace App\Models;
use App\Enums\ExpertiseProducts\ExpertiseProductPageEnum;
use App\Enums\NutriscoreEnum;
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Validation\ValidationException;
/**
* @property integer $id Id.
* @property \App\Enums\ExpertiseProducts\ExpertiseProductPageEnum $page Title.
* @property string $label Label of the title.
* @property string $title Title.
* @property string $subtitle Subtitle.
* @property array $points Type of the advice.
* @property boolean $is_frenchpoultry Is frenchpoultry or not.
* @property boolean $is_natureeleveurs Is natureeleveurs or not.
* @property \App\Enums\NutriscoreEnum $nutriscore Nutriscore.
* @property-read \Illuminate\Support\Carbon $created_at Created date.
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
*/
class ExpertiseProduct extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<string>
*/
protected $fillable = [
'page',
'label',
'title',
'subtitle',
'points',
'is_frenchpoultry',
'is_natureeleveurs',
'nutriscore',
];
/**
* The attributes that should be cast.
*
* @var array<string,string>
*/
protected $casts = [
'points' => 'array',
'is_frenchpoultry' => 'bool',
'is_natureeleveurs' => 'bool',
'page' => ExpertiseProductPageEnum::class,
'nutriscore' => NutriscoreEnum::class
];
/**
* 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');
FileStorageHelper::removeOldFile($model, 'picture2');
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'picture');
FileStorageHelper::removeFile($model, 'picture2');
});
}
// * METHODS
/**
* Common methods function.
*
* @param self $model
* @return void
*/
private static function manageFields(self $model): void
{
self::assertFieldIsUnique('label', $model->label, $model->id);
self::assertFieldIsUnique('title', $model->title, $model->id);
self::assertNumberOfProductsVip($model->page->value);
}
/**
* Asserts using validation that the label is unique.
*
* @param string $name
* @param mixed $field
* @param integer|null $id
* @return void
*/
private static function assertFieldIsUnique(string $name, mixed $field, ?int $id = null): void
{
$table = (new self())->getTable();
ToolboxHelper::assertFieldIsUnique($table, $name, $field, $id);
}
/**
* Asserts the number of products in the specie.
*
* @param integer $specie
* @return void
* @throws \Illuminate\Validation\ValidationException If there are more than 3 products.
*/
private static function assertNumberOfProductsVip(int $specie): void
{
if (count(self::where('page', '=', $specie)->get()) >= 3) {
throw ValidationException::withMessages([
'Opération impossible, maximum 3 produits peuvent être présents.'
]);
}
}
}