Skip to content

Commit

Permalink
Allow reverse setter if index is in autoRefresh and property is writable
Browse files Browse the repository at this point in the history
  • Loading branch information
maxhelias committed Nov 21, 2023
1 parent 878f887 commit c7484ed
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
3 changes: 2 additions & 1 deletion config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@
->args([
service('annotation_reader')->nullOnInvalid(), // @deprecated
service(EntityManagerInterface::class),
service('encryption.indexes_generator')
service('encryption.indexes_generator'),
service('property_accessor')
])
->alias(IndexableFieldsService::class, 'encryption.indexable_field')

Expand Down
25 changes: 19 additions & 6 deletions src/Services/IndexableFieldsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Doctrine\Common\Annotations\Reader;
use Doctrine\ORM\EntityManagerInterface;
use Odandb\DoctrineCiphersweetEncryptionBundle\Exception\UndefinedGeneratorException;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;

class IndexableFieldsService
{
Expand All @@ -19,12 +20,14 @@ class IndexableFieldsService
private ?Reader $annReader;
private EntityManagerInterface $em;
private IndexesGenerator $indexesGenerator;
private PropertyAccessorInterface $propertyAccessor;

public function __construct(?Reader $annReader, EntityManagerInterface $em, IndexesGenerator $generator)
public function __construct(?Reader $annReader, EntityManagerInterface $em, IndexesGenerator $generator, PropertyAccessorInterface $propertyAccessor)
{
$this->annReader = $annReader;
$this->em = $em;
$this->indexesGenerator = $generator;
$this->propertyAccessor = $propertyAccessor;
}

/**
Expand Down Expand Up @@ -122,11 +125,11 @@ public function purgeFiltersForContextAndIds(array $fieldsContexts, ?array $ids)
* @param null|array<int, string> $ids
* @param array<int, array{refProperty: \ReflectionProperty, indexableConfig: IndexableField}> $fieldsContexts
*/
public function handleFilterableFieldsForChunck(string $className, ?array $ids, array $fieldsContexts, bool $needsToComputeChangeset = false): void
public function handleFilterableFieldsForChunck(string $className, ?array $ids, array $fieldsContexts, bool $runtimeMode = false): void
{
$chunck = $this->em->getRepository($className)->findBy(!empty($ids) ? ['id' => $ids] : []);
foreach ($chunck as $entity) {
$this->handleIndexableFieldsForEntity($entity, $fieldsContexts, $needsToComputeChangeset);
$this->handleIndexableFieldsForEntity($entity, $fieldsContexts, $runtimeMode);
$this->em->flush();
}
}
Expand All @@ -138,7 +141,7 @@ public function handleFilterableFieldsForChunck(string $className, ?array $ids,
*
* @throws UndefinedGeneratorException|\ReflectionException
*/
public function handleIndexableFieldsForEntity(object $entity, array $fieldsContexts, bool $needsToComputeChangeset = false): void
public function handleIndexableFieldsForEntity(object $entity, array $fieldsContexts, bool $runtimeMode = false): void
{
$className = get_class($entity);
$searchIndexes = $this->generateIndexableValuesForEntity($entity, $fieldsContexts);
Expand All @@ -152,10 +155,14 @@ public function handleIndexableFieldsForEntity(object $entity, array $fieldsCont

$indexes = $this->indexesGenerator->generateBlindIndexesFromPossibleValues($className, $refProperty->getName(), $indexesToEncrypt, $indexableAnnotationConfig->fastIndexing);

// We create the filter object instances and associate them to the parent entity
// We create the filter object instances and associate them to the parent entity if is needed
$indexEntities = [];
$indexEntityClass = $indexableAnnotationConfig->indexesEntityClass;

// If we are in runtime with autoRefresh indexes, we need to compute the change set and set the inverse property to overwrite the existing one. With the orphanRemoval option, the old collection will be deleted.
// In other cases, the old indexes remain and must be purged by you.
$needToCompute = $runtimeMode && $indexableAnnotationConfig->autoRefresh;

$refClass = new \ReflectionClass($indexEntityClass);
$classMetadata = $this->em->getClassMetadata($refClass->getName());
foreach ($indexes as $index) {
Expand All @@ -168,11 +175,17 @@ public function handleIndexableFieldsForEntity(object $entity, array $fieldsCont

$this->em->persist($indexEntity);

if ($needsToComputeChangeset) {
if ($needToCompute) {
$this->em->getUnitOfWork()->computeChangeSet($classMetadata, $indexEntity);
}
}
}

if ($needToCompute) {
if ($this->propertyAccessor->isWritable($entity, $refClass->getShortName())) {
$this->propertyAccessor->setValue($entity, $refClass->getShortName(), $indexEntities);
}
}
}
}

Expand Down

0 comments on commit c7484ed

Please sign in to comment.