security->isGranted('ROLE_ADMIN')) { throw new AccessDeniedException('Access denied: ROLE_ADMIN required.'); } $parents = array_filter([$clientId, $prospectId, $prestataireId], static fn ($v) => null !== $v); if (1 !== count($parents)) { throw new InvalidArgumentException('Exactly one of clientId, prospectId or prestataireId must be provided.'); } $address = new Address(); if (null !== $clientId) { $client = $this->clientRepository->findById($clientId); if (null === $client) { throw new InvalidArgumentException(sprintf('Client with ID %d not found.', $clientId)); } $address->setClient($client); } if (null !== $prospectId) { $prospect = $this->prospectRepository->findById($prospectId); if (null === $prospect) { throw new InvalidArgumentException(sprintf('Prospect with ID %d not found.', $prospectId)); } $address->setProspect($prospect); } if (null !== $prestataireId) { $prestataire = $this->prestataireRepository->findById($prestataireId); if (null === $prestataire) { throw new InvalidArgumentException(sprintf('Prestataire with ID %d not found.', $prestataireId)); } $address->setPrestataire($prestataire); } $address->setLabel($label); $address->setStreet($street); $address->setStreetComplement($streetComplement); $address->setPostalCode($postalCode); $address->setCity($city); if (null !== $country) { if (2 !== strlen($country)) { throw new InvalidArgumentException('country must be a 2-letter ISO 3166 alpha-2 code (e.g., FR, BE).'); } $address->setCountry(strtoupper($country)); } $this->entityManager->persist($address); $this->entityManager->flush(); return json_encode(Serializer::address($address)); } }