Files
Scheduler/app/Models/Bot.php
Oskar-Mikael 75be85e608
All checks were successful
Deploy App / deploy (push) Successful in 9s
Add user based bots and policy
2025-08-31 14:27:42 +02:00

55 lines
1.1 KiB
PHP

<?php
namespace App\Models;
use Cron\CronExpression;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Lorisleiva\CronTranslator\CronTranslator;
class Bot extends Model
{
protected $fillable = [
'name',
'class',
'config',
'schedule',
'enabled',
'user_id',
];
protected $casts = [
'config' => 'array',
];
public function nextDue(): Attribute
{
return Attribute::make(
get: function () {
$cron = new CronExpression($this->schedule);
return $cron->getNextRunDate()->format('Y-m-d H:i:s');
}
);
}
public function cronToHuman(): Attribute
{
return Attribute::make(
get: fn() => CronTranslator::translate($this->schedule)
);
}
public function className(): Attribute
{
return Attribute::make(
get: fn() => class_basename($this->class)
);
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}