ldc-boutique-cnc/app/Models/Range.php
2023-02-08 12:10:10 +01:00

154 lines
4.8 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\Facades\Cache;
use Illuminate\Support\Str;
class Range extends Model
{
use HasFactory;
use HasFlashStatus;
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'status' => 'bool',
];
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable = [
'name', 'description',
'picture', 'status',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
// * EVENT CLOSURES.
static::creating(function (self $model) {
// * ASSERTIONS
static::assertNameIsUnique($model->name);
ToolboxHelper::assertNoInternalBoUrls('description', $model->description);
// * TRANSFORMATIONS
$model->slug = Str::slug($model->name);
OldSlugable::removeSameSlugs($model, $model->slug);
$model->description = clean($model->description);
$model->description = FileStorageHelper::storeModelFilesFromHtml($model, $model->description);
if ($model->picture) {
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
}
});
static::created(function (self $model) {
self::flashStatus($model);
Cache::delete('PublishedRangesGlobal');
});
static::updating(function (self $model) {
// * ASSERTIONS
static::assertNameIsUnique($model->name, $model->id);
ToolboxHelper::assertNoInternalBoUrls('description', $model->description);
// * TRANSFORMATIONS
$model->slug = Str::slug($model->name);
if ($model->slug != $model->getOriginal('slug')) {
OldSlugable::removeSameSlugs($model, $model->getOriginal('slug'));
}
OldSlugable::insertSlug($model);
$model->description = clean($model->description);
$model->description = FileStorageHelper::storeModelFilesFromHtml($model, $model->description);
if ($model->picture) {
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
}
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'picture');
FileStorageHelper::removeOldModelFilesFromHtml($model->description, $model->getOriginal('description'));
self::flashStatus($model, true);
Cache::delete('PublishedRangesGlobal');
});
static::deleted(function (self $model) {
$model->oldSlugs()->deleteOrFail();
FileStorageHelper::removeFile($model, 'picture');
FileStorageHelper::removeModelFilesOfHtml($model->description);
});
}
// * RELATIONSHIPS
/**
* hasMany customer relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function products()
{
return $this->hasMany('App\Models\Product', 'range_id');
}
/**
* MorphMany old_slugable relation
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function oldSlugs()
{
return $this->morphMany(OldSlugable::class, 'old_slugable');
}
// * METHODS
/**
* Asserts using validation that the name is unique
*
* @param mixed $name
* @param integer|null $id
* @return void
* @throws \Illuminate\Validation\ValidationException If an actu name already exists.
*/
private static function assertNameIsUnique($name, ?int $id = null)
{
$table = (new self())->getTable();
ToolboxHelper::assertFieldIsUnique($table, 'name', $name, $id);
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($name), $id);
}
// * - Cache
/**
* Get all available published ranges from cache.
* If cache was reset, it will fetch them from DB.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function getCachedPublishedRanges()
{
if (!Cache::has('PublishedRangesGlobal')) {
self::setCachedPublishedRanges();
}
return Cache::get('PublishedRangesGlobal');
}
/**
* Put all published ranges into cache.
*
* @return void
*/
protected static function setCachedPublishedRanges(): void
{
Cache::put('PublishedRangesGlobal', self::where('status', true)->get());
}
}