ldc-lionor/app/Models/Recipe.php

183 lines
5 KiB
PHP
Raw Normal View History

2023-02-24 15:20:58 +00:00
<?php
namespace App\Models;
use App\Enums\CookingModeEnum;
use App\Enums\DifficultyEnum;
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;
class Recipe extends Model
{
use HasFactory;
use HasFlashStatus;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'slug',
'seo_title',
'seo_description',
'picture',
'picture_alt',
'picture_title',
'sticker',
'sticker_alt',
'sticker_title',
'youtube_video_id',
'description',
'preparation',
'ingredients',
'portions',
'difficulty',
'cooking_mode',
'cooking_time',
'preparation_time',
'status',
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'ingredients' => 'array',
2023-08-28 08:46:47 +00:00
'status' => 'bool',
'cooking_mode' => CookingModeEnum::class,
'difficulty' => DifficultyEnum::class
2023-02-24 15:20:58 +00:00
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
// * EVENT CLOSURES.
static::creating(function (self $model) {
2023-08-28 08:46:47 +00:00
static::assertFieldIsUnique('name', $model->name);
2023-02-24 15:20:58 +00:00
self::manageFields($model);
});
static::created(function (self $model) {
self::flashStatus($model);
});
static::updating(function (self $model) {
2023-08-28 08:46:47 +00:00
static::assertFieldIsUnique('name', $model->name, $model->id);
2023-02-24 15:20:58 +00:00
self::manageFields($model);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'picture');
FileStorageHelper::removeOldFile($model, 'sticker');
self::flashStatus($model, true);
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'picture');
FileStorageHelper::removeFile($model, 'sticker');
});
}
// * METHODS
/**
* Manage fields on this model event.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
private static function manageFields(Model $model)
{
// * ASSERTIONS
ToolboxHelper::assertNoInternalBoUrls('description', $model->description);
ToolboxHelper::assertNoInternalBoUrls('preparation', $model->preparation ?? '');
if ($model->youtube_video_id) {
ToolboxHelper::assertNoInternalBoUrls('youtube_video_id', $model->youtube_video_id);
$model->youtube_video_id = clean($model->youtube_video_id);
}
// * TRANSFORMATIONS
$model->slug = Str::slug($model->name);
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
$model->sticker = FileStorageHelper::storeFile($model, $model->sticker, true);
}
/**
2023-08-28 08:46:47 +00:00
* Asserts using validation that the field is unique.
2023-02-24 15:20:58 +00:00
*
2023-08-28 08:46:47 +00:00
* @param string $nameField
* @param string $valueField
2023-02-24 15:20:58 +00:00
* @param integer|null $id
* @return void
*/
2023-08-28 08:46:47 +00:00
private static function assertFieldIsUnique(string $nameField, string $valueField, ?int $id = null)
2023-02-24 15:20:58 +00:00
{
$table = (new self())->getTable();
2023-08-28 08:46:47 +00:00
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id);
2023-02-24 15:20:58 +00:00
}
/**
* Convert database value (in minutes) to string "nnnH nnnM".
2023-03-10 15:33:15 +00:00
2023-02-24 15:20:58 +00:00
* @return string Text formated "nnnH nnnM".
*/
2023-03-10 15:33:15 +00:00
public function getCookingTimeLabelAttribute(): string
2023-02-24 15:20:58 +00:00
{
2023-03-10 15:33:15 +00:00
return self::convertMinutesToHourMinutes($this->cooking_time);
2023-02-24 15:20:58 +00:00
}
/**
* Convert database value (in minutes) to string "nnnH nnnM".
2023-03-10 15:33:15 +00:00
*
2023-02-24 15:20:58 +00:00
* @return string Text formated "nnnH nnnM".
*/
2023-03-10 15:33:15 +00:00
public function getPreparationTimeLabelAttribute(): string
2023-02-24 15:20:58 +00:00
{
2023-03-10 15:33:15 +00:00
return self::convertMinutesToHourMinutes($this->preparation_time);
2023-02-24 15:20:58 +00:00
}
/**
* Convert minutes to hour minutes string
*
* @param integer $minutes
* @return string
*/
private static function convertMinutesToHourMinutes(int $minutes): string
{
$nbHours = \intval($minutes / 60);
$nbMinutes = $minutes % 60;
return "{$nbHours}H {$nbMinutes}M";
}
// * RELATIONSHIPS
/**
* BelongsToMany recipes
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function recipes()
{
return $this->belongsToMany(Recipe::class, 'recipe_recipe', 'recipe_id', 'associated_recipe_id');
}
/**
* Get products for the recipe.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function products()
{
return $this->belongsToMany(Product::class, 'recipe_product');
}
}