Skip to content

Commit 14b8dfe

Browse files
jdecoolmogilvie
andauthored
Use event's ObjectManager instead of injected EntityManager in DoctrineEncryptListener (#68)
* Use ObjectManager from event args instead of injected EntityManager in DoctrineEncryptListener * Update DoctrineEncryptListener.php Move deprecation from constructor to doc block for compatibility with other php and symfony versions. * Update DoctrineEncryptListenerInterface.php --------- Co-authored-by: Mark Ogilvie <mogilvie@users.noreply.github.com>
1 parent 267b23f commit 14b8dfe

2 files changed

Lines changed: 24 additions & 14 deletions

File tree

src/EventListener/DoctrineEncryptListener.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Doctrine\ORM\Event\OnFlushEventArgs;
77
use Doctrine\ORM\Events;
88
use Doctrine\Persistence\Event\LifecycleEventArgs;
9+
use Doctrine\Persistence\ObjectManager;
910
use ReflectionProperty;
1011
use SpecShaper\EncryptBundle\Encryptors\EncryptorInterface;
1112
use SpecShaper\EncryptBundle\Exception\EncryptException;
@@ -40,6 +41,9 @@ class DoctrineEncryptListener implements DoctrineEncryptListenerInterface
4041

4142
private bool $isDisabled;
4243

44+
/**
45+
* @param EntityManagerInterface $em Deprecated in favour of fetching object manager from event args.
46+
*/
4347
public function __construct(
4448
private readonly EncryptorInterface $encryptor,
4549
private readonly EntityManagerInterface $em,
@@ -77,14 +81,15 @@ public function onFlush(OnFlushEventArgs $args): void
7781
return;
7882
}
7983

80-
$unitOfWork = $this->em->getUnitOfWork();
84+
$objectManager = $args->getObjectManager();
85+
$unitOfWork = $objectManager->getUnitOfWork();
8186

8287
foreach ($unitOfWork->getScheduledEntityInsertions() as $entity) {
83-
$this->processFields($entity, true, true);
88+
$this->processFields($objectManager, $entity, true, true);
8489
}
8590

8691
foreach ($unitOfWork->getScheduledEntityUpdates() as $entity) {
87-
$this->processFields($entity, true, false);
92+
$this->processFields($objectManager, $entity, true, false);
8893
}
8994
}
9095

@@ -99,7 +104,7 @@ public function postLoad(LifecycleEventArgs $args): void
99104
$entity = $args->getObject();
100105

101106
// Decrypt the entity fields.
102-
$this->processFields($entity, false, false);
107+
$this->processFields($args->getObjectManager(), $entity, false, false);
103108
}
104109

105110
/**
@@ -130,19 +135,19 @@ public function getEncryptionableProperties(array $allProperties): array
130135
/**
131136
* Process (encrypt/decrypt) entities fields.
132137
*/
133-
protected function processFields(object $entity, bool $isEncryptOperation, bool $isInsert): bool
138+
protected function processFields(ObjectManager $objectManager,object $entity, bool $isEncryptOperation, bool $isInsert): bool
134139
{
135140
// Get the encrypted properties in the entity.
136-
$properties = $this->getEncryptedFields($entity);
141+
$properties = $this->getEncryptedFields($objectManager, $entity);
137142

138143
// If no encrypted properties, return false.
139144
if (empty($properties)) {
140145
return false;
141146
}
142147

143-
$unitOfWork = $this->em->getUnitOfWork();
148+
$unitOfWork = $objectManager->getUnitOfWork();
144149
$oid = spl_object_id($entity);
145-
$meta = $this->em->getClassMetadata(get_class($entity));
150+
$meta = $objectManager->getClassMetadata(get_class($entity));
146151

147152
foreach ($properties as $refProperty) {
148153

@@ -201,9 +206,11 @@ public function postUpdate(LifecycleEventArgs $args): void
201206
$entity = $args->getObject();
202207
$oid = spl_object_id($entity);
203208

209+
$objectManager = $args->getObjectManager();
210+
204211
if (isset($this->rawValues[$oid])) {
205212
$className = get_class($entity);
206-
$meta = $this->em->getClassMetadata($className);
213+
$meta = $objectManager->getClassMetadata($className);
207214
foreach ($this->rawValues[$oid] as $prop => $rawValue) {
208215
$refProperty = $meta->getReflectionProperty($prop);
209216
$refProperty->setValue($entity, $rawValue);
@@ -216,9 +223,9 @@ public function postUpdate(LifecycleEventArgs $args): void
216223
/**
217224
* @return array<string, ReflectionProperty>
218225
*/
219-
protected function getEncryptedFields(object $entity): array
226+
protected function getEncryptedFields(ObjectManager $objectManager, object $entity): array
220227
{
221-
$reflectionClass = $this->getOriginalEntityReflection($entity);
228+
$reflectionClass = $this->getOriginalEntityReflection($objectManager, $entity);
222229

223230
$className = $reflectionClass->getName();
224231

@@ -257,9 +264,9 @@ private function isEncryptedProperty(ReflectionProperty $refProperty)
257264
return false;
258265
}
259266

260-
protected function getOriginalEntityReflection($entity): \ReflectionClass
267+
protected function getOriginalEntityReflection(ObjectManager $objectManager, $entity): \ReflectionClass
261268
{
262-
$realClassName = $this->em->getClassMetadata(get_class($entity))->getName();
269+
$realClassName = $objectManager->getClassMetadata(get_class($entity))->getName();
263270
return new \ReflectionClass($realClassName);
264271
}
265272
}

src/EventListener/DoctrineEncryptListenerInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
interface DoctrineEncryptListenerInterface
1414
{
1515
public const ENCRYPTED_SUFFIX = '<ENC>';
16-
16+
17+
/**
18+
* @param EntityManagerInterface $em Deprecated in favour of getting object manager from event args.
19+
*/
1720
public function __construct(
1821
EncryptorInterface $encryptor,
1922
EntityManagerInterface $em,

0 commit comments

Comments
 (0)