toArray(); $requiredFields = ['nfn', 'idi', 'dsk', 'par', 'ids', 'res', 'der', 'job_id']; foreach ($requiredFields as $field) { if (!isset($data[$field])) { return new JsonResponse(['message' => "Missing parameter: $field"], Response::HTTP_BAD_REQUEST); } } $this->logger->info('Webhook data received', $data); if ($data['nfn'] === 'RESPUESTA_CrearImagen') { $trace = $this->entityManager->getRepository(Trace::class)->findOneBy(['jobId' => $data['job_id']]); $image = $this->entityManager->getRepository(Image::class)->findOneBy(['uuid' => $data['idi']]); if (!$image) { $this->logger->error('Image not found', $data); return new JsonResponse(['message' => 'Image not found'], Response::HTTP_NOT_FOUND); } if (!$trace) { $this->logger->error('Trace not found', $data); return new JsonResponse(['message' => 'Trace not found'], Response::HTTP_NOT_FOUND); } if ($data['res'] === 1) { $trace->setStatus(TraceStatus::SUCCESS); $trace->setFinishedAt(new \DateTime()); $image->setStatus(ImageStatus::AUX_FILES_PENDING); $image->setCreated(true); $this->logger->info('Start partition creation. ', ['image' => (string) $image->getUuid()]); if (isset($data['cfg'])) { $this->createPartitionService->__invoke($data, $image->getClient()); } $this->logger->info('Starting software profile creation. ', ['image' => (string) $image->getUuid()]); $this->createSoftwareProfile($data['inv_sft'], $image); $this->logger->info('Start aux files ogrepo API ', ['image' => (string) $image->getUuid()]); $this->createAuxFilesAction->__invoke($image); $this->logger->info('End aux files ogrepo API ', ['image' => (string) $image->getUuid()]); } else { $trace->setStatus(TraceStatus::FAILED); $trace->setFinishedAt(new \DateTime()); $trace->setOutput($data['der']); $image->setCreated(false); $image->setStatus(ImageStatus::FAILED); $this->logger->error('Image updated failed', $data); } $this->entityManager->persist($image); $client = $trace->getClient(); $client->setStatus(ClientStatus::OG_LIVE); $this->entityManager->persist($trace); $this->entityManager->flush(); $this->logger->info('Image updated. Success.', ['image' => (string) $image->getUuid()]); } if ($data['nfn'] === 'RESPUESTA_RestaurarImagen') { $trace = $this->entityManager->getRepository(Trace::class)->findOneBy(['jobId' => $data['job_id']]); $image = $this->entityManager->getRepository(Image::class)->findOneBy(['uuid' => $data['idi']]); if ($data['res'] === 1) { $trace->setStatus(TraceStatus::SUCCESS); $trace->setFinishedAt(new \DateTime()); $image->setStatus(ImageStatus::PENDING); } else { $trace->setStatus(TraceStatus::FAILED); $trace->setFinishedAt(new \DateTime()); $trace->setOutput($data['der']); } $this->entityManager->persist($image); $this->entityManager->persist($trace); $this->entityManager->flush(); } return new JsonResponse([], Response::HTTP_OK); } public function createSoftwareProfile(string $base64Data, Image $image): void { $decodedData = base64_decode($base64Data); $this->logger->info('Software profile decoded', ['data' => '']); $softwareList = array_map('trim', explode(",", $decodedData)); $softwareList = array_filter($softwareList); $existingSoftware = $this->entityManager->getRepository(Software::class)->findBy(['name' => $softwareList]); $existingNames = array_map(fn($software) => $software->getName(), $existingSoftware); $newSoftwareNames = array_diff($softwareList, $existingNames); foreach ($newSoftwareNames as $software) { $softwareEntity = new Software(); $softwareEntity->setName($software); $softwareEntity->setType(SoftwareTypes::APPLICATION); $this->entityManager->persist($softwareEntity); } $softwareProfile = new SoftwareProfile(); $softwareProfile->setDescription('Perfil: ' . $image->getName()); $softwareProfile->setOrganizationalUnit($image->getClient()->getOrganizationalUnit()); foreach ($existingSoftware as $softwareEntity) { $softwareEntity->addSoftwareProfile($softwareProfile); } $this->entityManager->persist($softwareProfile); $this->entityManager->flush(); } }