ldc-boutique-cnc/app/Models/Shop/DeliveryMode.php
Jérémy Munsch 6472f29fdf
More Fixes
2023-05-16 08:13:10 +02:00

102 lines
2.4 KiB
PHP

<?php
namespace App\Models\Shop;
use App\Enums\Order\DeliveryModeEnum;
use App\Traits\Models\HasFlashStatus;
use App\Traits\Models\IsCachedAsCollection;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Kwaadpepper\Enum\Traits\CastsEnums;
/**
*
* @property-read \App\Enums\Order\DeliveryModeEnum $mode The delivery mode.
*
* @property \App\Enums\Order\DeliveryModeEnum $mode The delivery mode.
* @property boolean $status Delivery mode is available to make order.
* @property boolean $is_free The mode is free of charges.
* @package App\Models
*/
class DeliveryMode extends Model
{
use CastsEnums;
use HasFactory;
use HasFlashStatus;
use IsCachedAsCollection;
/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = 'mode';
/**
* Indicates if the IDs are auto-incrementing.
*
* @var boolean
*/
public $incrementing = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'status', 'is_free',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'status' => 'bool',
'is_free' => 'bool',
];
/**
* Map attribute names to enum classes.
*
* @var array
*/
protected $enumCasts = [
'mode' => DeliveryModeEnum::class
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
// * EVENT CLOSURES.
static::creating(function (self $model) {
throw new \RuntimeException("Creating {$model->mode} is not allowed", 1);
});
static::updated(function (self $model) {
self::removeCached();
self::flashStatus($model, true);
});
static::deleting(function (self $model) {
throw new \RuntimeException("Deleting {$model->mode} is not allowed", 1);
});
}
// * METHODS
/**
* Query available delivery Methods
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function availables(): Builder
{
return self::where('status', true);
}
}