diff --git a/composer.json b/composer.json index d9c7a51db..175fbc512 100644 --- a/composer.json +++ b/composer.json @@ -98,7 +98,7 @@ "require-dev": { "doctrine/doctrine-fixtures-bundle": "^3.3", "fakerphp/faker": "^1.13", - "friendsofphp/php-cs-fixer": "3.93.0", + "friendsofphp/php-cs-fixer": "^3.94", "jangregor/phpstan-prophecy": "^1.0", "nelmio/alice": "^3.8", "nikic/php-parser": "^4.15", @@ -113,13 +113,13 @@ "symfony/debug-bundle": "^5.4", "symfony/dotenv": "^5.4", "symfony/flex": "^2.4", + "symfony/loco-translation-provider": "^6.0", "symfony/maker-bundle": "^1.20", "symfony/phpunit-bridge": "^7.1", "symfony/runtime": "^5.4", "symfony/stopwatch": "^5.4", "symfony/var-dumper": "^5.4", - "symfony/web-profiler-bundle": "^5.4", - "symfony/loco-translation-provider": "^6.0" + "symfony/web-profiler-bundle": "^5.4" }, "conflict": { "symfony/symfony": "*" diff --git a/package.json b/package.json index 12a5fee26..cd4f23a07 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "@hotwired/stimulus": "^3.0.0", "@luminateone/eslint-baseline": "^1.0.9", "@symfony/stimulus-bridge": "^3.2.0", + "@symfony/ux-translator": "file:vendor/symfony/ux-translator/assets", "@symfony/webpack-encore": "^4.1.0", "@tsconfig/node20": "^20.1.4", "@types/dompurify": "^3.0.5", diff --git a/src/Bundle/ChillPersonBundle/Entity/Person/Upsert/Handler/PersonUpsertHandler.php b/src/Bundle/ChillPersonBundle/Entity/Person/Upsert/Handler/PersonUpsertHandler.php new file mode 100644 index 000000000..e5ab106e7 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Entity/Person/Upsert/Handler/PersonUpsertHandler.php @@ -0,0 +1,83 @@ +personIdentifierDefinitionRepository->find($message->personIdentifierDefinitionId); + if (!$definition) { + throw new \RuntimeException('PersonIdentifierDefinition non trouvée pour l\'id '.$message->personIdentifierDefinitionId); + } + + // 2. Chercher les identifiants + $identifiers = $this->personIdentifierRepository->findByDefinitionAndCanonical($definition, $message->externalId); + + if (count($identifiers) > 1) { + throw new \RuntimeException('Plus d\'un identifiant trouvé pour la définition et la valeur donnée.'); + } + + if (0 === count($identifiers)) { + // 3. Créer une nouvelle entité Person + $person = new Person(); + if (null !== $message->firstName) { + $person->setFirstName($message->firstName); + } + if (null !== $message->lastName) { + $person->setLastName($message->lastName); + } + $this->entityManager->persist($person); + + // Créer l'identifiant et l'associer + $identifier = new PersonIdentifier($definition); + $identifier->setPerson($person); + $identifier->setValue(['content' => $message->externalId]); + $this->entityManager->persist($identifier); + } else { + // 4. Mettre à jour le Person existant + /** @var PersonIdentifier $identifier */ + $identifier = $identifiers[0]; + $person = $identifier->getPerson(); + if (null === $person) { + throw new \RuntimeException('L\'identifiant trouvé n\'est lié à aucun Person.'); + } + if (null !== $message->firstName) { + $person->setFirstName($message->firstName); + } + if (null !== $message->lastName) { + $person->setLastName($message->lastName); + } + } + + $this->entityManager->flush(); + } +} diff --git a/src/Bundle/ChillPersonBundle/Entity/Person/Upsert/UpsertMessage.php b/src/Bundle/ChillPersonBundle/Entity/Person/Upsert/UpsertMessage.php new file mode 100644 index 000000000..81e9d6eb2 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Entity/Person/Upsert/UpsertMessage.php @@ -0,0 +1,20 @@ +repository->findBy(['id' => $ids]); } + public function findByExternalId(string $externalId): ?Person + { + return $this->repository->findOneBy(['externalId' => $externalId]); + } + /** * @throws \Exception *