271 lines
7.6 KiB
PHP
271 lines
7.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Advices\AdviceTypeEnum;
|
|
use App\Enums\Advices\AdviceDifficultyEnum;
|
|
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\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Kwaadpepper\Enum\Traits\CastsEnums;
|
|
|
|
class Advice extends Authenticatable
|
|
{
|
|
use CastsEnums;
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'title',
|
|
'slug',
|
|
'seo_title',
|
|
'seo_description',
|
|
'type',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title',
|
|
'sticker',
|
|
'sticker_alt',
|
|
'sticker_title',
|
|
'sticker_sm',
|
|
'sticker_sm_alt',
|
|
'sticker_sm_title',
|
|
'preparation',
|
|
'advice',
|
|
'ingredients',
|
|
'guests_nb',
|
|
'youtube_title',
|
|
'youtube_video_id',
|
|
'cooking_time',
|
|
'preparation_time',
|
|
'difficulty',
|
|
'status'
|
|
];
|
|
|
|
/**
|
|
* Map attribute names to enum classes.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $enumCasts = [
|
|
'type' => AdviceTypeEnum::class,
|
|
'difficulty' => AdviceDifficultyEnum::class
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'ingredients' => 'array',
|
|
'status' => 'bool'
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
static::creating(function (self $model) {
|
|
self::manageFields($model);
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::manageFields($model);
|
|
self::assertLimitProducts($model);
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileStorageHelper::removeOldFile($model, 'picture');
|
|
FileStorageHelper::removeOldFile($model, 'sticker');
|
|
FileStorageHelper::removeOldFile($model, 'sticker_sm');
|
|
});
|
|
static::deleted(function (self $model) {
|
|
FileStorageHelper::removeFile($model, 'picture');
|
|
FileStorageHelper::removeFile($model, 'sticker');
|
|
FileStorageHelper::removeFile($model, 'sticker_sm');
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Manage fields on this model event.
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Model $model
|
|
* @return void
|
|
*/
|
|
private static function manageFields(Model $model)
|
|
{
|
|
self::assertTitleIsUnique($model->title, $model->id);
|
|
$model->slug = Str::slug($model->title);
|
|
|
|
self::assertLimitAdvices($model->advices()->count());
|
|
self::assertLimitTags($model->tags()->count());
|
|
|
|
self::setCookingtimePreparationtimeValues($model);
|
|
|
|
ToolboxHelper::assertNoInternalBoUrls('preparation', $model->preparation);
|
|
if ($model->advice != null) {
|
|
ToolboxHelper::assertNoInternalBoUrls('advice', $model->advice);
|
|
}
|
|
if ($model->youtube_video_id) {
|
|
ToolboxHelper::assertNoInternalBoUrls('youtube_video_id', $model->youtube_video_id);
|
|
$model->youtube_video_id = clean($model->youtube_video_id);
|
|
}
|
|
|
|
$model->preparation = clean($model->preparation);
|
|
$model->advice = clean($model->advice);
|
|
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture);
|
|
$model->sticker = FileStorageHelper::storeFile($model, $model->sticker);
|
|
$model->sticker_sm = FileStorageHelper::storeFile($model, $model->sticker_sm);
|
|
}
|
|
|
|
/**
|
|
* Limit tags associated.
|
|
*
|
|
* @param integer $nbTagsInBdd
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException Error exception.
|
|
*/
|
|
private static function assertLimitTags(int $nbTagsInBdd)
|
|
{
|
|
if ($nbTagsInBdd > 2) {
|
|
throw ValidationException::withMessages([
|
|
'Vous devez associer au maximum 2 tags.'
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Limit advices associated.
|
|
*
|
|
* @param integer $nbAdvicesInBdd
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException Error exception.
|
|
*/
|
|
private static function assertLimitAdvices(int $nbAdvicesInBdd)
|
|
{
|
|
if ($nbAdvicesInBdd > 6) {
|
|
throw ValidationException::withMessages([
|
|
'Vous devez associer au maximum 6 conseils.'
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Limit products associated.
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Model $model
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException Error exception.
|
|
*/
|
|
private static function assertLimitProducts(Model $model)
|
|
{
|
|
$nbProductsInBdd = $model->products()->count();
|
|
if ($model->type->value == AdviceTypeEnum::recipes()->value) {
|
|
if ($nbProductsInBdd < 1 || $nbProductsInBdd > 3) {
|
|
throw ValidationException::withMessages([
|
|
'Vous devez associer 1 à 3 produits.'
|
|
]);
|
|
}
|
|
} elseif ($model->type->value == AdviceTypeEnum::tips()->value) {
|
|
if ($nbProductsInBdd > 3) {
|
|
throw ValidationException::withMessages([
|
|
'Vous devez associer au maximum 3 produits.'
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Asserts using validation that the title is unique.
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Model $model
|
|
* @return void
|
|
*/
|
|
private static function setCookingtimePreparationtimeValues(Model $model)
|
|
{
|
|
if ($model->type->value != AdviceTypeEnum::recipes()->value) {
|
|
$model->cooking_time = null;
|
|
$model->preparation_time = null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Asserts using validation that the title is unique.
|
|
*
|
|
* @param mixed $title
|
|
* @param integer|null $id
|
|
* @return void
|
|
*/
|
|
private static function assertTitleIsUnique($title, ?int $id = null)
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, 'title', $title, $id);
|
|
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($title), $id);
|
|
}
|
|
|
|
/**
|
|
* Get associated advices
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Collection<\App\Models\Advice>
|
|
*/
|
|
public function recipes(): Collection
|
|
{
|
|
return $this->advices()->where('type', AdviceTypeEnum::recipes())->get();
|
|
}
|
|
|
|
/**
|
|
* Get associated tips
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Collection<\App\Models\Advice>
|
|
*/
|
|
public function tips(): Collection
|
|
{
|
|
return $this->advices()->where('type', AdviceTypeEnum::tips())->get();
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* BelongsToMany advices
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function advices()
|
|
{
|
|
return $this->belongsToMany(Advice::class, 'advice_advice', 'advice_id', 'advice_related_id');
|
|
}
|
|
|
|
/**
|
|
* BelongsToMany products
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function products()
|
|
{
|
|
return $this->belongsToMany(Product::class);
|
|
}
|
|
|
|
/**
|
|
* MorphToMany Tags
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
*/
|
|
public function tags()
|
|
{
|
|
return $this->morphToMany(Tag::class, 'taggable');
|
|
}
|
|
}
|