ldc-le-gaulois-pro/app/Models/Product.php
2023-01-17 19:12:45 +01:00

225 lines
6.1 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Advices\AdviceTypeEnum;
use App\Enums\NutriscoreEnum;
use App\Enums\Products\ProductSpeciesEnum;
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Str;
use Kwaadpepper\Enum\Traits\CastsEnums;
class Product extends Model
{
use CastsEnums;
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'label',
'slug',
'seo_title',
'seo_description',
'subrange_id',
'species',
'nutriscore',
'ingredients',
'advantage',
'preparation',
'sticker_sm',
'sticker_sm_alt',
'sticker_sm_title',
'slider_pictures',
'packaging',
'caliber',
'nutritionnal_values',
'is_frenchpoultry',
'is_vip',
'is_egalim',
'status'
];
/**
* Map attribute names to enum classes.
*
* @var array
*/
protected $enumCasts = [
'species' => ProductSpeciesEnum::class,
'nutriscore' => NutriscoreEnum::class
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'slider_pictures' => 'array',
'packaging' => 'array',
'caliber' => 'array',
'nutritionnal_values' => 'array',
'is_frenchpoultry' => 'bool',
'is_vip' => 'bool',
'is_egalim' => 'bool',
'status' => 'bool'
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
// * EVENT CLOSURES.
static::creating(function (self $model) {
self::manageFields($model);
$model->order = \intval(self::query()->max('order')) + 1;
});
static::updating(function (self $model) {
self::manageFields($model);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'sticker_sm');
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'sticker_sm');
});
}
// * METHODS
/**
* Undocumented function
*
* @param Model $model
* @return void
*/
private static function manageFields(Model $model)
{
// * ASSERTIONS
static::assertLabelIsUnique($model->label, $model->id);
ToolboxHelper::assertNoInternalBoUrls('ingredients', $model->ingredients);
ToolboxHelper::assertNoInternalBoUrls('advantage', $model->advantage);
ToolboxHelper::assertNoInternalBoUrls('preparation', $model->preparation);
foreach ($model->packaging as $pack) {
ToolboxHelper::assertNoInternalBoUrls('packaging', $pack['value']);
}
foreach ($model->caliber as $calib) {
ToolboxHelper::assertNoInternalBoUrls('caliber', $calib['value']);
}
foreach ($model->nutritionnal_values as $nutritionnal_value) {
if ($model->nutritionnal_value) {
ToolboxHelper::assertNoInternalBoUrls('nutritionnal_values', $nutritionnal_value['value']);
}
}
// * TRANSFORMATIONS
$model->slug = Str::slug($model->label);
$model->sticker_sm = FileStorageHelper::storeFile($model, $model->sticker_sm, true);
$model->ingredients = clean($model->ingredients);
$model->advantage = clean($model->advantage);
$model->preparation = clean($model->preparation);
$model->slider_pictures = clean($model->slider_pictures);
$model->packaging = clean($model->packaging);
$model->caliber = clean($model->caliber);
$model->nutritionnal_values = clean($model->nutritionnal_values);
}
/**
* Asserts using validation that the label is unique
*
* @param mixed $label
* @param integer|null $id
* @return void
*/
private static function assertLabelIsUnique($label, ?int $id = null)
{
$table = (new self())->getTable();
ToolboxHelper::assertFieldIsUnique($table, 'label', $label, $id);
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($label), $id);
}
/**
* Get associated tips
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function tipsQuery(): BelongsToMany
{
return $this->advices()->where('type', AdviceTypeEnum::tips());
}
/**
* Get associated recipes
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function recipesQuery(): BelongsToMany
{
return $this->advices()->where('type', AdviceTypeEnum::tips());
}
/**
* Get associated tips
*
* @return \Illuminate\Database\Eloquent\Collection<\App\Models\Advice>
*/
public function tips(): Collection
{
return $this->tipsQuery()->get();
}
/**
* Get associated recipes
*
* @return \Illuminate\Database\Eloquent\Collection<\App\Models\Advice>
*/
public function recipes(): Collection
{
return $this->recipesQuery()->get();
}
// * RELATIONSHIPS
/**
* HasOne Subrange relation
*
* @return \Illuminate\Database\Eloquent\Relations\belongsTo
*/
public function subrange()
{
return $this->belongsTo(Subrange::class);
}
/**
* BelongsToMany advices relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function advices()
{
return $this->belongsToMany(Advice::class, 'advice_product');
}
/**
* BelongsToMany advice relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function news()
{
return $this->belongsToMany(News::class);
}
}