Files

63 lines
2.0 KiB
PHP
Raw Permalink Normal View History

2026-10-05 18:10:18 -04:00
<?php
declare(strict_types=1);
namespace Pterodactyl\Actions\Schedules;
2026-10-11 06:11:40 -04:00
use Illuminate\Support\Facades\Validator;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Contracts\Schedules\CreatesSchedules;
use Pterodactyl\Exceptions\DisplayException;
2026-10-11 06:11:40 -04:00
use Pterodactyl\Facades\Activity;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Schedule;
use Pterodactyl\Models\Server;
use Throwable;
final class CreateSchedule implements CreatesSchedules
{
2026-10-11 23:50:53 -04:00
/** @return NormalizedValidationRules */
2026-10-11 06:11:40 -04:00
public static function rules(): array
{
return [
'name' => ['required', 'string', 'max:191'],
'is_active' => ['filled', 'boolean'],
'minute' => ['required', 'string'],
'hour' => ['required', 'string'],
'day_of_month' => ['required', 'string'],
'month' => ['required', 'string'],
'day_of_week' => ['required', 'string'],
'only_when_online' => ['boolean'],
];
}
2026-10-11 23:41:04 -04:00
public function create(Server $server, array $input): Schedule
2026-10-05 18:10:18 -04:00
{
2026-10-11 23:41:04 -04:00
$validated = Validator::validate($input, self::rules());
$schedule = $server->schedules()->make([
'name' => $validated['name'],
'cron_minute' => $validated['minute'],
'cron_hour' => $validated['hour'],
'cron_day_of_month' => $validated['day_of_month'],
'cron_month' => $validated['month'],
'cron_day_of_week' => $validated['day_of_week'],
'is_active' => $validated['is_active'] ?? false,
'only_when_online' => $validated['only_when_online'] ?? false,
]);
2026-10-05 18:10:18 -04:00
try {
2026-10-11 23:41:04 -04:00
$schedule->next_run_at = $schedule->getNextRunDate();
2026-10-05 18:10:18 -04:00
} catch (Throwable) {
throw new DisplayException('The cron data provided does not evaluate to a valid expression.');
}
2026-10-11 23:41:04 -04:00
$schedule->save();
2026-10-11 06:11:40 -04:00
Activity::event('server:schedule.create')
2026-10-11 14:07:14 -04:00
->subject($server, $schedule)
2026-10-11 06:11:40 -04:00
->property('name', $schedule->name)
->log();
2026-10-05 18:10:18 -04:00
return $schedule->refresh();
}
}