ldc-legaulois/app/Models/MenuNodeData.php
2024-10-08 11:57:56 +02:00

131 lines
4.6 KiB
PHP

<?php
namespace App\Models;
use App\Casts\HtmlColor;
use App\Enums\Settings\MenuNodeType;
use App\Lib\Helpers\FileStorageHelper;
use App\Traits\Models\IsCachedAsCollection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @property-read \App\Enums\Settings\MenuNodeType $type
* @property string $sub_label (Left part) Link label.
* @property string $title (Right part) Sub title.
* @property string $text (Right part) Sub text.
* @property string $text_color (Right part) Sub text color.
* @property string $link_url (Right part) Link url.
* @property string $link_title (Right part) Link title.
* @property boolean $link_target (Right part) Link target.
* @property boolean $link_status (Right part) Link status.
* @property string $picture (Right part) Picture.
* @property string $picture_alt (Right part) Picture alt text.
* @property string $picture_title (Right part) Picture title text.
* @property boolean $picture_status (Right part) Picture status.
* @property string $background_picture (Right part) Menu picture.
* @property string $background_picture_alt (Right part) Menu picture alt text.
* @property string $background_picture_title (Right part) Menu picture title text.
*
* @package App\Models
*/
class MenuNodeData extends Model
{
use HasFactory;
use IsCachedAsCollection;
/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = 'type';
/**
* The "type" of the primary key ID.
*
* @var string
*/
protected $keyType = MenuNodeType::class;
/**
* Indicates if the IDs are auto-incrementing.
*
* @var boolean
*/
public $incrementing = false;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'sub_label',
'title',
'text',
'text_color',
'link_url',
'link_title',
'link_target',
'link_status',
'picture',
'picture_alt',
'picture_title',
'picture_status',
'background_picture',
'background_picture_alt',
'background_picture_title',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'type' => MenuNodeType::class,
'text_color' => HtmlColor::class,
'link_target' => 'boolean',
'link_status' => 'boolean',
'picture_status' => 'boolean',
];
/**
* Perform any actions required after the model boots.
*
* @return void
* @throws \ErrorException Not allowed to create an instance of this.
*/
protected static function booted(): void
{
static::creating(fn () => throw new \ErrorException('Not allowed to create an instance of this'));
static::updating(function (self $model) {
self::denyHandlingSomeTypes($model);
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
$model->background_picture = FileStorageHelper::storeFile($model, $model->background_picture, true);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'picture');
FileStorageHelper::removeOldFile($model, 'background_picture');
self::removeCached();
});
static::deleting(fn () => throw new \ErrorException('Not allowed to create an instance of this'));
}
// * METHODS
/**
* Deny Handling some of the MenuNodeData.
*
* @param self $model
* @return void
* @throws \RuntimeException Database operations are not allowed for some types.
*/
private static function denyHandlingSomeTypes(self $model): void
{
if (in_array($model->type, [MenuNodeType::siteLogo, MenuNodeType::link])) {
throw new \RuntimeException('Database operations are not allowed the type ' . $model->type->label());
}
}
}