Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correction sur la détection des changements pour UnitOfWork #8

Merged
merged 1 commit into from
Feb 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions src/Subscribers/DoctrineCiphersweetSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class DoctrineCiphersweetSubscriber implements EventSubscriber
*/
private array $postFlushDecryptQueue = [];

private array $entitiesToEncrypt = [];

private IndexableFieldsService $indexableFieldsService;

private PropertyHydratorService $propertyHydratorService;
Expand Down Expand Up @@ -84,6 +86,12 @@ public function onFlush(OnFlushEventArgs $args): void
foreach ($unitOfWork->getScheduledEntityUpdates() as $entity) {
$this->entityOnFlush($entity, $em);
$unitOfWork->recomputeSingleEntityChangeSet($em->getClassMetadata(\get_class($entity)), $entity);
unset($this->entitiesToEncrypt[spl_object_id($entity)]);
}

foreach ($this->entitiesToEncrypt as $entity) {
$this->entityOnFlush($entity, $em);
$unitOfWork->recomputeSingleEntityChangeSet($em->getClassMetadata(\get_class($entity)), $entity);
}
}

Expand All @@ -105,7 +113,7 @@ public function onClear(OnClearEventArgs $args): void
*/
private function entityOnFlush(object $entity, EntityManagerInterface $em): void
{
$objId = spl_object_hash($entity);
$objId = spl_object_id($entity);

$fields = [];

Expand Down Expand Up @@ -161,7 +169,7 @@ public function processFields(object $entity, EntityManagerInterface $em, $isEnc
$properties = $this->getEncryptedFields($entity, $em);
$unitOfWork = $em->getUnitOfWork();

$oid = spl_object_hash($entity);
$oid = spl_object_id($entity);

$entityClassName = $em->getClassMetadata(get_class($entity))->getName();

Expand All @@ -175,8 +183,12 @@ public function processFields(object $entity, EntityManagerInterface $em, $isEnc
$context = $this->buildContext($entityClassName, $refProperty);

if ($isEncryptOperation) {
$value = $this->handleEncryptOperation($entity, $oid, $value, $refProperty, $em, $context, $force);
$value = $this->handleEncryptOperation($entity, $oid, $value, $refProperty, $context, $force);
} else {
$oldValue = $value;
if (!$this->isValueEncrypted($oldValue)) {
$this->entitiesToEncrypt[$oid] = $entity;
}
$value = $this->handleDecryptOperation($oid, $value, $refProperty, $context);
}

Expand All @@ -189,7 +201,7 @@ public function processFields(object $entity, EntityManagerInterface $em, $isEnc

if (!$isEncryptOperation && !\defined('_DONOTENCRYPT')) {
//we don't want the object to be dirty immediately after reading
$unitOfWork->setOriginalEntityProperty($oid, $refProperty->getName(), $value);
$unitOfWork->setOriginalEntityProperty(spl_object_id($entity), $refProperty->getName(), $value);
}
}

Expand Down Expand Up @@ -224,18 +236,17 @@ private function buildContext(string $entityClassName, \ReflectionProperty $refP

/**
* @param object $entity
* @param string $oid
* @param int $oid
* @param mixed $value
* @param \ReflectionProperty $refProperty
* @param EntityManagerInterface $em
* @param array $context
* @param string|null $force
* @return mixed|string|null
*
* @throws \Odandb\DoctrineCiphersweetEncryptionBundle\Exception\UndefinedGeneratorException
* @throws \ReflectionException
*/
private function handleEncryptOperation(object $entity, string $oid, $value, \ReflectionProperty $refProperty, EntityManagerInterface $em, array $context, ?string $force = null)
private function handleEncryptOperation(object $entity, int $oid, $value, \ReflectionProperty $refProperty, array $context, ?string $force = null)
{
/**
* @var IndexableField $indexableAnnotationConfig
Expand Down Expand Up @@ -280,13 +291,13 @@ private function handleEncryptOperation(object $entity, string $oid, $value, \Re
}

/**
* @param string $oid
* @param int $oid
* @param mixed $value
* @param \ReflectionProperty $refProperty
* @param array $context
* @return string
*/
private function handleDecryptOperation(string $oid, $value, \ReflectionProperty $refProperty, array $context): string
private function handleDecryptOperation(int $oid, $value, \ReflectionProperty $refProperty, array $context): string
{
/**
* @var IndexableField $indexableAnnotationConfig
Expand Down Expand Up @@ -376,12 +387,12 @@ private function storeIndexes(object $entity, \ReflectionProperty $refProperty,
*/
public function postFlush(PostFlushEventArgs $args): void
{
$unitOfWork = $args->getEntityManager()->getUnitOfWork();
$unitOfWork = $args->getObjectManager()->getUnitOfWork();

foreach ($this->postFlushDecryptQueue as $pair) {
$fieldPairs = $pair['fields'];
$entity = $pair['entity'];
$oid = spl_object_hash($entity);
$oid = spl_object_id($entity);

foreach ($fieldPairs as $fieldPair) {
/** @var \ReflectionProperty $field */
Expand All @@ -402,7 +413,7 @@ public function postFlush(PostFlushEventArgs $args): void
*/
private function addToDecodedRegistry($entity): void
{
$this->decodedRegistry[spl_object_hash($entity)] = true;
$this->decodedRegistry[spl_object_id($entity)] = true;
}

/**
Expand All @@ -411,8 +422,8 @@ private function addToDecodedRegistry($entity): void
*/
public function postLoad(LifecycleEventArgs $args): void
{
$entity = $args->getEntity();
if (!$this->hasInDecodedRegistry($entity) && $this->processFields($entity, $args->getEntityManager(), false)) {
$entity = $args->getObject();
if (!$this->hasInDecodedRegistry($entity) && $this->processFields($entity, $args->getObjectManager(), false)) {
$this->addToDecodedRegistry($entity);
}
}
Expand All @@ -424,7 +435,7 @@ public function postLoad(LifecycleEventArgs $args): void
*/
private function hasInDecodedRegistry(object $entity): bool
{
return isset($this->decodedRegistry[spl_object_hash($entity)]);
return isset($this->decodedRegistry[spl_object_id($entity)]);
}

/**
Expand Down