getStatus() !== ImageStatus::SUCCESS) { return new BadRequestHttpException('Image is not ready to be deployed'); } $this->validator->validate($input); $clientJobs = []; if ($input->type === 'monolithic') { $clientJobs = $this->handleMonolithicDeployment($input, $image); } return new JsonResponse(data: $clientJobs, status: Response::HTTP_OK); } private function handleMonolithicDeployment(DeployImageInput $input, ImageImageRepository $image): array { $clientJobs = []; switch ($input->method) { case DeployMethodTypes::UNICAST: case DeployMethodTypes::UNICAST_DIRECT: $clientJobs = $this->handleUnicastDeployment($input, $image); break; case DeployMethodTypes::MULTICAST_UFTP: case DeployMethodTypes::MULTICAST_UFTP_DIRECT: case DeployMethodTypes::MULTICAST_UDPCAST: case DeployMethodTypes::MULTICAST_UDPCAST_DIRECT: $clientJobs = $this->handleMulticastDeployment($input, $image); break; case DeployMethodTypes::TORRENT: $clientJobs = $this->handleTorrentDeployment($input, $image); break; } return $clientJobs; } private function handleUnicastDeployment(DeployImageInput $input, ImageImageRepository $image): array { $clientJobs = []; foreach ($input->clients as $client) { $inputData = $this->createInputData($input, $image, $client->getEntity()); $jobId = $this->processDeployment($client->getEntity(), $input, $image, $inputData, DeployMethodTypes::UNICAST); if ($jobId) { $clientJobs[(string) '/clients/' . $client->getEntity()->getUuid()] = $jobId; } } return $clientJobs; } private function handleMulticastDeployment(DeployImageInput $input, ImageImageRepository $image): array { $clientJobs = []; foreach ($input->clients as $client) { $inputData = $this->createMulticastInputData($input, $image, $client->getEntity()); try { $this->deployImageOgRepositoryAction->__invoke($input, $image, $client->getEntity()); } catch (\Exception $e) { continue; } $jobId = $this->processDeployment($client->getEntity(), $input, $image, $inputData, DeployMethodTypes::MULTICAST); if ($jobId) { $clientJobs[(string) '/clients/' . $client->getEntity()->getUuid()] = $jobId; } } return $clientJobs; } private function handleTorrentDeployment(DeployImageInput $input, ImageImageRepository $image): array { $clientJobs = []; foreach ($input->clients as $client) { $inputData = $this->createTorrentInputData($input, $image, $client->getEntity()); try { $response = $this->deployImageOgRepositoryAction->__invoke($input, $image, $client->getEntity(), $this->httpClient); if (is_array($response) && isset($response['code']) && $response['code'] === 500) { throw new \Exception('Error del servidor OgRepository: ' . ($response['error'] ?? 'Error desconocido') . ' - ' . ($response['details'] ?? '')); } } catch (\Exception $e) { throw $e; } $jobId = $this->processDeployment($client->getEntity(), $input, $image, $inputData, DeployMethodTypes::TORRENT); if ($jobId) { $clientJobs[(string) '/clients/' . $client->getEntity()->getUuid()] = $jobId; } } return $clientJobs; } private function processDeployment($client, DeployImageInput $input, ImageImageRepository $image, array $inputData, string $deployType): ?string { $agentJobId = $this->deployImageOgAgentAction->__invoke($image, $input, $client, $deployType); if (!$agentJobId) { if ($input->queue) { $this->createService->__invoke($client, CommandTypes::DEPLOY_IMAGE, TraceStatus::PENDING, null, $inputData); } return null; } $this->createService->__invoke($client, CommandTypes::DEPLOY_IMAGE, TraceStatus::IN_PROGRESS, $agentJobId, $inputData); return $agentJobId; } private function createInputData(DeployImageInput $input, ImageImageRepository $image, $client): array { return [ 'method' => $input->method, 'client' => $client->getUuid(), 'image' => $image->getUuid(), 'imageName' => $image->getName(), 'numDisk' => (string) $input->diskNumber, 'numPartition' => (string) $input->partitionNumber, 'type' => $input->type, ]; } private function createMulticastInputData(DeployImageInput $input, ImageImageRepository $image, $client): array { return array_merge($this->createInputData($input, $image, $client), [ 'mcastIp' => $input->mcastIp, 'mcastPort' => $input->mcastPort, 'mcastSpeed' => $input->mcastSpeed, 'mcastMode' => $input->mcastMode, ]); } private function createTorrentInputData(DeployImageInput $input, ImageImageRepository $image, $client): array { return array_merge($this->createInputData($input, $image, $client), [ 'p2pMode' => $input->p2pMode, 'p2pTime' => $input->p2pTime, ]); } }