|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Extensions\Tasks\Schemas; |
| 4 | + |
| 5 | +use App\Models\Task; |
| 6 | +use App\Repositories\Daemon\DaemonServerRepository; |
| 7 | +use Filament\Forms\Components\Select; |
| 8 | +use Filament\Schemas\Components\Component; |
| 9 | +use Illuminate\Support\Str; |
| 10 | + |
| 11 | +final class PowerActionSchema extends TaskSchema |
| 12 | +{ |
| 13 | + public function __construct(private DaemonServerRepository $serverRepository) {} |
| 14 | + |
| 15 | + public function getId(): string |
| 16 | + { |
| 17 | + return 'power'; |
| 18 | + } |
| 19 | + |
| 20 | + public function runTask(Task $task): void |
| 21 | + { |
| 22 | + $this->serverRepository->setServer($task->server)->power($task->payload); |
| 23 | + } |
| 24 | + |
| 25 | + public function getDefaultPayload(): string |
| 26 | + { |
| 27 | + return 'restart'; |
| 28 | + } |
| 29 | + |
| 30 | + public function getPayloadLabel(): string |
| 31 | + { |
| 32 | + return trans('server/schedule.tasks.actions.power.action'); |
| 33 | + } |
| 34 | + |
| 35 | + public function formatPayload(string $payload): string |
| 36 | + { |
| 37 | + return Str::ucfirst($payload); |
| 38 | + } |
| 39 | + |
| 40 | + /** @return Component[] */ |
| 41 | + public function getPayloadForm(): array |
| 42 | + { |
| 43 | + return [ |
| 44 | + Select::make('payload') |
| 45 | + ->label($this->getPayloadLabel()) |
| 46 | + ->required() |
| 47 | + ->options([ |
| 48 | + 'start' => trans('server/schedule.tasks.actions.power.start'), |
| 49 | + 'restart' => trans('server/schedule.tasks.actions.power.restart'), |
| 50 | + 'stop' => trans('server/schedule.tasks.actions.power.stop'), |
| 51 | + 'kill' => trans('server/schedule.tasks.actions.power.kill'), |
| 52 | + ]) |
| 53 | + ->selectablePlaceholder(false) |
| 54 | + ->default($this->getDefaultPayload()), |
| 55 | + ]; |
| 56 | + } |
| 57 | +} |
0 commit comments