120 lines
2.7 KiB
PHP
120 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Order\PaymentModeEnum;
|
|
use App\Traits\Models\HasFlashStatus;
|
|
use App\Traits\Models\MiniCrudModel;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Kwaadpepper\Enum\Traits\CastsEnums;
|
|
|
|
class PaymentMode extends Model
|
|
{
|
|
use CastsEnums;
|
|
use HasFactory;
|
|
use HasFlashStatus;
|
|
use MiniCrudModel;
|
|
|
|
/**
|
|
* 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 should be cast to native types.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'status' => 'bool',
|
|
'data' => 'encrypted:object'
|
|
];
|
|
|
|
/**
|
|
* Map attribute names to enum classes.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $enumCasts = [
|
|
'mode' => PaymentModeEnum::class
|
|
];
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'status', 'data',
|
|
];
|
|
|
|
|
|
/**
|
|
* 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) {
|
|
Cache::delete('PaymentModesGlobal');
|
|
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);
|
|
}
|
|
|
|
// * - Cache
|
|
/**
|
|
* Get all available payment modes from cache.
|
|
* If cache was reset, it will fetch them from DB.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
*/
|
|
public static function getCachedPaymentModes()
|
|
{
|
|
if (!Cache::has('PaymentModesGlobal')) {
|
|
self::setCachedPaymentModes();
|
|
}
|
|
return Cache::get('PaymentModesGlobal');
|
|
}
|
|
|
|
/**
|
|
* Put all available payment modes into cache.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function setCachedPaymentModes(): void
|
|
{
|
|
Cache::put('PaymentModesGlobal', self::all());
|
|
}
|
|
}
|