service = $service; $this->action = $action; $this->successStatus = $successStatus; $this->failStatus = $failStatus; $this->failMessage = $failMessage; } /** * @throws Throwable */ public function handle(): void { $command = match ($this->action) { 'start' => new StartServiceCommand($this->service->unit), 'stop' => new StopServiceCommand($this->service->unit), 'restart' => new RestartServiceCommand($this->service->unit), default => throw new Exception('Invalid action'), }; $this->service->server->ssh()->exec( $command, $this->action.'-'.$this->service->name ); $this->service->status = $this->successStatus; $this->service->save(); event( new Broadcast('update-service-finished', [ 'service' => $this->service, ]) ); } public function failed(): void { $this->service->status = $this->failStatus; $this->service->save(); event( new Broadcast('update-service-failed', [ 'message' => $this->service->name.' '.$this->failMessage, 'service' => $this->service, ]) ); } }