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

141 lines
4.7 KiB
PHP

<?php
namespace App\Models;
use App\Casts\RgbaColor;
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Uwil\Seo\Laravel\Interfaces\SeoInterface;
use Uwil\Seo\Laravel\Objects\SeoItem;
use Uwil\Seo\Laravel\Traits\SeoTrait;
/**
* @property integer $id Id.
* @property string $label Label.
* @property string $slug Slug of the title.
* @property string $seo_title SEO Title.
* @property string $seo_description SEO description.
* @property integer $color Color of the range.
* @property string $picture Path of a picture.
* @property string $picture_alt Picture's attribute alt.
* @property string $picture_title Picture's attribute title.
* @property string $banner Path of a banner.
* @property string $banner_alt Banner's attribute alt.
* @property string $banner_title Banner's attribute title.
* @property integer $order Order.
* @property-read \Illuminate\Support\Carbon $created_at Created date.
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
*
* @property-read \Illuminate\Database\Eloquent\Collection<\App\Models\Subrange> $subranges
* Subranges Has-Many relationship.
*/
class Range extends Model implements SeoInterface
{
use HasFactory;
use SeoTrait;
/**
* The attributes that are mass assignable.
*
* @var array<string>
*/
protected $fillable = [
'label',
'slug',
'seo_title',
'seo_description',
'color',
'picture',
'picture_alt',
'picture_title',
'picture_banner',
'picture_banner_alt',
'picture_banner_title',
'order',
];
/**
* The attributes that should be cast.
*
* @var array<string,string>
*/
protected $casts = [
'color' => RgbaColor::class,
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function () {
throw new \RuntimeException('Impossible de créer un range');
});
static::updating(function (self $model) {
self::assertLabelIsUnique($model->label, $model->id);
$model->slug = Str::slug($model->label);
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
$model->picture_banner = FileStorageHelper::storeFile($model, $model->picture_banner, true);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'picture');
FileStorageHelper::removeOldFile($model, 'picture_banner');
});
static::deleting(function () {
throw new \RuntimeException('Impossible de supprimer un range');
});
}
// * METHODS
/**
* Asserts using validation that the label is unique.
*
* @param string $label
* @param integer|null $id
* @return void
* @throws \Illuminate\Validation\ValidationException If an actu label already exists.
*/
private static function assertLabelIsUnique(string $label, ?int $id = null): void
{
$table = (new self())->getTable();
ToolboxHelper::assertFieldIsUnique($table, 'label', $label, $id);
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($label), $id);
}
// * RELATIONSHIPS
/**
* Subranges Has-Many relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function subranges(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(Subrange::class);
}
// * SEO
/**
* Transform to seoModel
*
* @return \Uwil\Seo\Laravel\Objects\SeoItem
*/
public function toSeoItem(): SeoItem
{
return new SeoItem(
\trans($this->getTable()),
$this->seo_title,
$this->seo_description,
true,
\route(\config('seo.bo_route_prefix') . $this->getTable() . '.edit', $this),
\route('fo.products.range-products', $this)
);
}
}