mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-02-27 18:49:41 +00:00
Ajoute le gestionnaire PersonUpsertHandler et le message UpsertMessage, ainsi que des tests pour la création et la mise à jour des utilisateurs.
This commit is contained in:
@@ -11,43 +11,40 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\PersonBundle\Actions\Upsert\Handler;
|
||||
|
||||
use Chill\PersonBundle\Repository\PersonRepository;
|
||||
use Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository;
|
||||
use Chill\PersonBundle\Repository\Identifier\PersonIdentifierRepository;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Entity\Identifier\PersonIdentifier;
|
||||
use Chill\PersonBundle\Entity\Person\Upsert\UpsertMessage;
|
||||
use Chill\PersonBundle\Actions\Upsert\UpsertMessage;
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
#[AsMessageHandler]
|
||||
class PersonUpsertHandler
|
||||
readonly class PersonUpsertHandler
|
||||
{
|
||||
public function __construct(
|
||||
// TODO: Utiliser findByExternalId à la place de findByDefinitionAndCanonical ?
|
||||
private readonly PersonRepository $personRepository,
|
||||
private readonly PersonIdentifierDefinitionRepository $personIdentifierDefinitionRepository,
|
||||
private readonly PersonIdentifierRepository $personIdentifierRepository,
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private PersonIdentifierDefinitionRepository $personIdentifierDefinitionRepository,
|
||||
private PersonIdentifierRepository $personIdentifierRepository,
|
||||
private EntityManagerInterface $entityManager,
|
||||
) {}
|
||||
|
||||
public function __invoke(UpsertMessage $message): void
|
||||
{
|
||||
// 1. Récupérer la définition
|
||||
// 1. Retrieve definition
|
||||
$definition = $this->personIdentifierDefinitionRepository->find($message->personIdentifierDefinitionId);
|
||||
if (!$definition) {
|
||||
throw new \RuntimeException('PersonIdentifierDefinition non trouvée pour l\'id '.$message->personIdentifierDefinitionId);
|
||||
throw new \RuntimeException('PersonIdentifierDefinition not found for id '.$message->personIdentifierDefinitionId);
|
||||
}
|
||||
|
||||
// 2. Chercher les identifiants
|
||||
// 2. Search identifiers
|
||||
$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.');
|
||||
throw new \RuntimeException('More than one identifier found for definition.');
|
||||
}
|
||||
|
||||
if (0 === count($identifiers)) {
|
||||
// 3. Créer une nouvelle entité Person
|
||||
// 3. Create new entity Person
|
||||
$person = new Person();
|
||||
if (null !== $message->firstName) {
|
||||
$person->setFirstName($message->firstName);
|
||||
@@ -57,19 +54,16 @@ class PersonUpsertHandler
|
||||
}
|
||||
$this->entityManager->persist($person);
|
||||
|
||||
// Créer l'identifiant et l'associer
|
||||
// Create and bound identifier
|
||||
$identifier = new PersonIdentifier($definition);
|
||||
$identifier->setPerson($person);
|
||||
$identifier->setValue(['content' => $message->externalId]);
|
||||
$this->entityManager->persist($identifier);
|
||||
} else {
|
||||
// 4. Mettre à jour le Person existant
|
||||
// 4. Update existing person
|
||||
/** @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);
|
||||
}
|
||||
|
||||
@@ -9,12 +9,12 @@ declare(strict_types=1);
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\PersonBundle\Tests\Entity\Person\Upsert;
|
||||
namespace Chill\PersonBundle\Tests\Action\Upsert\Handler;
|
||||
|
||||
use Chill\PersonBundle\Entity\Person\Upsert\Handler\PersonUpsertHandler;
|
||||
use Chill\PersonBundle\Actions\Upsert\Handler\PersonUpsertHandler;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Entity\Identifier\PersonIdentifier;
|
||||
use Chill\PersonBundle\Entity\Person\Upsert\UpsertMessage;
|
||||
use Chill\PersonBundle\Actions\Upsert\UpsertMessage;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
@@ -27,7 +27,7 @@ class PersonUpsertHandlerTest extends TestCase
|
||||
{
|
||||
public function testInvokeCreatesNewPersonAndIdentifier(): void
|
||||
{
|
||||
$personRepository = $this->createMock(\Chill\PersonBundle\Repository\PersonRepository::class);
|
||||
$personIdentifierDefinitionRepository = $this->createMock(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository::class);
|
||||
$definitionRepository = $this->createMock(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository::class);
|
||||
$identifierRepository = $this->createMock(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierRepository::class);
|
||||
$entityManager = $this->createMock(EntityManagerInterface::class);
|
||||
@@ -43,8 +43,7 @@ class PersonUpsertHandlerTest extends TestCase
|
||||
$entityManager->expects($this->once())->method('flush');
|
||||
|
||||
$handler = new PersonUpsertHandler(
|
||||
$personRepository,
|
||||
$definitionRepository,
|
||||
$personIdentifierDefinitionRepository,
|
||||
$identifierRepository,
|
||||
$entityManager
|
||||
);
|
||||
@@ -56,12 +55,12 @@ class PersonUpsertHandlerTest extends TestCase
|
||||
$message->lastName = 'Doe';
|
||||
|
||||
$handler->__invoke($message);
|
||||
$this->assertTrue(true); // Pour éviter le test risky
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
public function testInvokeUpdatesExistingPerson(): void
|
||||
{
|
||||
$personRepository = $this->createMock(\Chill\PersonBundle\Repository\PersonRepository::class);
|
||||
$personIdentifierDefinitionRepository = $this->createMock(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository::class);
|
||||
$definitionRepository = $this->createMock(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository::class);
|
||||
$identifierRepository = $this->createMock(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierRepository::class);
|
||||
$entityManager = $this->createMock(EntityManagerInterface::class);
|
||||
@@ -81,8 +80,7 @@ class PersonUpsertHandlerTest extends TestCase
|
||||
$entityManager->expects($this->once())->method('flush');
|
||||
|
||||
$handler = new PersonUpsertHandler(
|
||||
$personRepository,
|
||||
$definitionRepository,
|
||||
$personIdentifierDefinitionRepository,
|
||||
$identifierRepository,
|
||||
$entityManager
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user