From 36c73b5336c49ac7ee1a042aa468757ffb3b2a52 Mon Sep 17 00:00:00 2001 From: Benjamin Vison Date: Sat, 5 May 2018 20:48:37 -0400 Subject: [PATCH] Adding improved synchronization process --- Annotations/SyncParent.php | 17 +++++ EventListener/DoctrineEventSubscriber.php | 93 ++++++++++------------- Interfaces/SyncEntityInterface.php | 6 ++ Service/SyncService.php | 2 +- 4 files changed, 63 insertions(+), 55 deletions(-) create mode 100755 Annotations/SyncParent.php create mode 100755 Interfaces/SyncEntityInterface.php diff --git a/Annotations/SyncParent.php b/Annotations/SyncParent.php new file mode 100755 index 0000000..1886d34 --- /dev/null +++ b/Annotations/SyncParent.php @@ -0,0 +1,17 @@ +container = $container; $this->syncService = $this->container->get('nti.sync'); } @@ -28,72 +34,51 @@ public function getSubscribedEvents() public function onFlush(OnFlushEventArgs $args) { + $em = $args->getEntityManager(); $uow = $em->getUnitOfWork(); - $somethingChanged = false; - - $identityMap = $uow->getIdentityMap(); - - // ehhehehehee LOL - $deletedEntities = count($uow->getScheduledEntityDeletions()) > 0; - $insertedEntities = count($uow->getScheduledEntityInsertions()) > 0; - - foreach($identityMap as $map) { - foreach($map as $object) { - $changes = $uow->getEntityChangeSet($object); - if(count($changes) > 1 || (count($changes) > 0 && !isset($changes["lastTimestamp"]) && !isset($changes["lastLogin"]))) { - $somethingChanged = true; - break; - } - } - - if($somethingChanged) { - break; + foreach ($uow->getScheduledEntityUpdates() as $entity) { + // Check if the entity should be synchronized + if (!($entity instanceof SyncEntityInterface)) { + continue; } + $this->processEntity($em, $entity); } - if($deletedEntities || $insertedEntities) - $somethingChanged = true; - - foreach ($uow->getScheduledEntityUpdates() as $keyEntity => $entity) { - $changes = $uow->getEntityChangeSet($entity); - - if(count($changes) == 1 && isset($changes["lastTimestamp"]) && !isset($changes["lastLogin"]) && !$somethingChanged) { - $oid = spl_object_hash($entity); - $uow->clearEntityChangeSet($oid); - } else { - $this->handleEntityChange($em, $entity); - } - } + } - foreach ($uow->getScheduledEntityInsertions() as $keyEntity => $entity) { - $this->handleEntityChange($em, $entity); - } + public function preRemove(LifecycleEventArgs $args) + { } - public function preRemove(LifecycleEventArgs $args) { - $entity = $args->getEntity(); - $class = get_class($entity); - $id = null; + private function processEntity(EntityManagerInterface $em, $entity) + { + $uow = $em->getUnitOfWork(); - if(method_exists($entity, 'getId')) { - $id = $entity->getId(); - } + $timestamp = time(); - $this->syncService->addToDeleteSyncState($class, $id); - } + // Check if this class itself has a lastTimestamp + if(method_exists($entity, 'setLastTimestamp')) { + $entity->setLastTimestamp($timestamp); + $uow->computeChangeSet($em->getClassMetadata(get_class($entity)), $entity); + } - private function handleEntityChange(EntityManagerInterface $em, $entity) { - if(method_exists($entity, 'getLastTimestamp')) { - $timestamp = $entity->getLastTimestamp() ?? time(); - } else { - $timestamp = time(); + // Check if there are any relationships that should notified + $annotationReader = new AnnotationReader(); + $reflection = new \ReflectionClass(get_class($entity)); + + /** @var \ReflectionProperty $property */ + foreach ($reflection->getProperties() as $property) { + /** @var SyncParent $annotation */ + if (null !== ($annotation = $annotationReader->getPropertyAnnotation($property, SyncParent::class))) { + $getter = $annotation->getter; + $parent = $entity->$getter(); + if($parent instanceof SyncEntityInterface) { + $this->processEntity($em, $parent); + } + } } - $class = get_class($entity); - $this->syncService->updateSyncState($em, $class, $timestamp); } - - } diff --git a/Interfaces/SyncEntityInterface.php b/Interfaces/SyncEntityInterface.php new file mode 100755 index 0000000..3b54861 --- /dev/null +++ b/Interfaces/SyncEntityInterface.php @@ -0,0 +1,6 @@ + $result["data"], 'deletes' => json_decode($this->container->get('jms_serializer')->serialize($deletes, 'json'), true), 'newItems' => json_decode($this->container->get('jms_serializer')->serialize($newItems, 'json'), true), - 'failedItems' => json_decode($this->container->get('jms_serializer')->serialize($failedItems, 'json'), true), + 'failedItems' => json_decode($this->container->get('jms_serializer')->serialize($failedItems, 'json'), true), SyncState::REAL_LAST_TIMESTAMP => $result[SyncState::REAL_LAST_TIMESTAMP], ); }