ldc-boutique-cnc/app/Models/Shop/DeliveryMode.php

103 lines
2.4 KiB
PHP
Raw Normal View History

2022-12-02 11:43:33 +01:00
<?php
2023-03-01 11:02:42 +01:00
namespace App\Models\Shop;
2022-12-02 11:43:33 +01:00
use App\Enums\Order\DeliveryModeEnum;
2023-02-06 17:14:58 +01:00
use App\Traits\Models\HasFlashStatus;
2023-03-17 17:32:18 +01:00
use App\Traits\Models\IsCachedAsCollection;
2022-12-02 11:43:33 +01:00
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Kwaadpepper\Enum\Traits\CastsEnums;
2022-12-02 11:43:33 +01:00
/**
*
* @property-read \App\Enums\Order\DeliveryModeEnum $mode The delivery mode.
2022-12-02 11:43:33 +01:00
*
2023-05-16 08:13:10 +02:00
* @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.
2022-12-02 11:43:33 +01:00
* @package App\Models
*/
class DeliveryMode extends Model
{
use CastsEnums;
2022-12-02 11:43:33 +01:00
use HasFactory;
2023-02-06 17:14:58 +01:00
use HasFlashStatus;
2023-03-17 17:32:18 +01:00
use IsCachedAsCollection;
2022-12-02 11:43:33 +01:00
/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = 'mode';
2022-12-02 11:43:33 +01:00
/**
* Indicates if the IDs are auto-incrementing.
*
* @var boolean
*/
public $incrementing = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
2023-02-06 17:14:58 +01:00
'status', 'is_free',
2022-12-02 11:43:33 +01:00
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
2023-02-06 17:14:58 +01:00
'status' => 'bool',
2022-12-02 11:43:33 +01:00
'is_free' => 'bool',
];
/**
* Map attribute names to enum classes.
*
* @var array
*/
protected $enumCasts = [
'mode' => DeliveryModeEnum::class
];
2022-12-02 11:43:33 +01:00
/**
* 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);
2023-02-06 17:14:58 +01:00
});
2022-12-02 14:55:16 +01:00
static::updated(function (self $model) {
2023-03-24 15:06:31 +01:00
self::removeCached();
2023-02-06 17:14:58 +01:00
self::flashStatus($model, true);
2022-12-02 14:55:16 +01:00
});
2022-12-02 11:43:33 +01:00
static::deleting(function (self $model) {
throw new \RuntimeException("Deleting {$model->mode} is not allowed", 1);
2022-12-02 11:43:33 +01:00
});
}
// * METHODS
/**
* Query available delivery Methods
*
* @return \Illuminate\Database\Eloquent\Builder
2022-12-02 11:43:33 +01:00
*/
public static function availables(): Builder
{
2023-02-08 12:12:42 +01:00
return self::where('status', true);
2022-12-02 11:43:33 +01:00
}
}