mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-01 05:56:16 +00:00
69 lines
1.7 KiB
PHP
Executable File
69 lines
1.7 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Actions\CronJob;
|
|
|
|
use App\Enums\CronjobStatus;
|
|
use App\Exceptions\SSHError;
|
|
use App\Models\CronJob;
|
|
use App\Models\Server;
|
|
use App\ValidationRules\CronRule;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class EditCronJob
|
|
{
|
|
/**
|
|
* @param array<string, mixed> $input
|
|
*
|
|
* @throws SSHError
|
|
*/
|
|
public function edit(Server $server, CronJob $cronJob, array $input): CronJob
|
|
{
|
|
Validator::make($input, self::rules($input, $server))->validate();
|
|
|
|
$cronJob->update([
|
|
'user' => $input['user'],
|
|
'command' => $input['command'],
|
|
'frequency' => $input['frequency'] == 'custom' ? $input['custom'] : $input['frequency'],
|
|
'status' => CronjobStatus::UPDATING,
|
|
]);
|
|
$cronJob->save();
|
|
|
|
$server->cron()->update($cronJob->user, CronJob::crontab($server, $cronJob->user));
|
|
$cronJob->status = CronjobStatus::READY;
|
|
$cronJob->save();
|
|
|
|
return $cronJob;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $input
|
|
* @return array<string, array<int, mixed>>
|
|
*/
|
|
public static function rules(array $input, Server $server): array
|
|
{
|
|
$rules = [
|
|
'command' => [
|
|
'required',
|
|
],
|
|
'user' => [
|
|
'required',
|
|
Rule::in($server->getSshUsers()),
|
|
],
|
|
'frequency' => [
|
|
'required',
|
|
new CronRule(acceptCustom: true),
|
|
],
|
|
];
|
|
|
|
if (isset($input['frequency']) && $input['frequency'] == 'custom') {
|
|
$rules['custom'] = [
|
|
'required',
|
|
new CronRule,
|
|
];
|
|
}
|
|
|
|
return $rules;
|
|
}
|
|
}
|