toArray(); $requiredFields = ['nfn', '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']]); /** @var ImageImageRepository $imageImageRepository */ $imageImageRepository = $this->entityManager->getRepository(ImageImageRepository::class)->findOneBy(['uuid' => $data['idi']]); if (!$imageImageRepository) { $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()); $imageImageRepository->setStatus(ImageStatus::AUX_FILES_PENDING); $imageImageRepository->setCreated(true); $this->entityManager->persist($imageImageRepository); $this->logger->info('Start partition creation. ', ['image' => (string) $imageImageRepository->getUuid()]); if (isset($data['cfg'])) { $this->createPartitionService->__invoke($data, $imageImageRepository->getImage()->getClient()); } $this->logger->info('Starting software profile creation. ', ['image' => (string) $imageImageRepository->getUuid()]); $this->createSoftwareProfile($data['inv_sft'], $imageImageRepository); $this->logger->info('Start aux files ogrepo API ', ['image' => (string) $imageImageRepository->getUuid()]); try { $this->createAuxFilesAction->__invoke($imageImageRepository); } catch (\Exception $e) { $this->logger->error('Error creating aux files', ['image' => (string) $imageImageRepository->getUuid(), 'error' => $e->getMessage()]); } $this->logger->info('End aux files ogrepo API ', ['image' => (string) $imageImageRepository->getUuid()]); } else { $trace->setStatus(TraceStatus::FAILED); $trace->setFinishedAt(new \DateTime()); $trace->setOutput($data['der']); $imageImageRepository->setCreated(false); $imageImageRepository->setStatus(ImageStatus::FAILED); $this->logger->error('Image updated failed', $data); } $this->entityManager->persist($imageImageRepository); $client = $trace->getClient(); $client->setStatus(ClientStatus::OG_LIVE); $this->entityManager->persist($client); $this->entityManager->persist($trace); $this->entityManager->flush(); $this->logger->info('Image updated. Success.', ['image' => (string) $imageImageRepository->getUuid()]); } if ($data['nfn'] === 'RESPUESTA_RestaurarImagen'|| $data['nfn'] === 'RESPUESTA_Configurar') { $trace = $this->entityManager->getRepository(Trace::class)->findOneBy(['jobId' => $data['job_id']]); $client = $trace->getClient(); if ($data['res'] === 1) { $trace->setStatus(TraceStatus::SUCCESS); $trace->setFinishedAt(new \DateTime()); if (isset($data['cfg'])) { $this->createPartitionService->__invoke($data,$client); } } else { $trace->setStatus(TraceStatus::FAILED); $trace->setFinishedAt(new \DateTime()); $trace->setOutput($data['der']); } $client->setStatus(ClientStatus::OG_LIVE); $this->entityManager->persist($client); $this->entityManager->persist($trace); $this->entityManager->flush(); } return new JsonResponse(data: 'Webhook finished', status: Response::HTTP_OK); } public function createSoftwareProfile(string $base64Data, ImageImageRepository $imageImageRepository): void { $decodedData = base64_decode($base64Data); $this->logger->info('Software profile decoded', ['data' => '']); $softwareList = array_map('trim', explode("\n", $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); } $image = $imageImageRepository->getImage(); $softwareProfile = new SoftwareProfile(); $softwareProfile->setDescription('Perfil software: ' . $image->getClient()->getName()); $softwareProfile->setOrganizationalUnit($image->getClient()->getOrganizationalUnit()); foreach ($existingSoftware as $softwareEntity) { $softwareEntity->addSoftwareProfile($softwareProfile); } $image->setSoftwareProfile($softwareProfile); $this->entityManager->persist($image); $this->entityManager->persist($imageImageRepository); $this->entityManager->persist($softwareProfile); $this->entityManager->flush(); } }