diff --git a/Controller/SyncController.php b/Controller/SyncController.php index 70747b9..2989afd 100755 --- a/Controller/SyncController.php +++ b/Controller/SyncController.php @@ -98,8 +98,6 @@ public function pushAction(Request $request) { $syncClass = $mapping->getSyncService(); - #if(!class_exists($syncClass)) { continue; } - /** @var SyncServiceInterface $service */ $service = $this->get($syncClass); diff --git a/Entity/SyncState.php b/Entity/SyncState.php old mode 100644 new mode 100755 diff --git a/EventListener/DoctrineEventSubscriber.php b/EventListener/DoctrineEventSubscriber.php index 1fabc89..8700e95 100755 --- a/EventListener/DoctrineEventSubscriber.php +++ b/EventListener/DoctrineEventSubscriber.php @@ -3,7 +3,9 @@ namespace NTI\SyncBundle\EventListener; use Doctrine\Common\EventSubscriber; +use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Event\LifecycleEventArgs; +use Doctrine\ORM\Event\OnFlushEventArgs; use Symfony\Component\DependencyInjection\ContainerInterface; class DoctrineEventSubscriber implements EventSubscriber @@ -19,36 +21,23 @@ public function __construct(ContainerInterface $container) { public function getSubscribedEvents() { return array( - 'postPersist', - 'postUpdate', + 'onFlush', 'preRemove', ); } - public function postPersist(LifecycleEventArgs $args) + public function onFlush(OnFlushEventArgs $args) { - $entity = $args->getEntity(); - if(method_exists($entity, 'getLastTimestamp')) { - $timestamp = $entity->getLastTimestamp() ?? time(); - } else { - $timestamp = time(); - } + $em = $args->getEntityManager(); + $uow = $em->getUnitOfWork(); - $class = get_class($args->getEntity()); - $this->syncService->updateSyncState($class, $timestamp); - } - - public function postUpdate(LifecycleEventArgs $args) - { - $entity = $args->getEntity(); - if(method_exists($entity, 'getLastTimestamp')) { - $timestamp = $entity->getLastTimestamp() ?? time(); - } else { - $timestamp = time(); + foreach ($uow->getScheduledEntityUpdates() as $keyEntity => $entity) { + $this->handleEntityChange($em, $entity); } - $class = get_class($args->getEntity()); - $this->syncService->updateSyncState($class, $timestamp); + foreach ($uow->getScheduledEntityInsertions() as $keyEntity => $entity) { + $this->handleEntityChange($em, $entity); + } } public function preRemove(LifecycleEventArgs $args) @@ -65,4 +54,14 @@ public function preRemove(LifecycleEventArgs $args) $this->syncService->addToDeleteSyncState($class, $id); } + private function handleEntityChange(EntityManagerInterface $em, $entity) { + if(method_exists($entity, 'getLastTimestamp')) { + $timestamp = $entity->getLastTimestamp() ?? time(); + } else { + $timestamp = time(); + } + $class = get_class($entity); + $this->syncService->updateSyncState($em, $class, $timestamp); + } + } \ No newline at end of file diff --git a/Resources/config/services.yml b/Resources/config/services.yml index ca743e4..212e162 100755 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -6,4 +6,5 @@ services: - { name: doctrine.event_subscriber, connection: default } nti.sync: class: NTI\SyncBundle\Service\SyncService - arguments: ["@service_container"] \ No newline at end of file + arguments: ["@service_container"] + public: true \ No newline at end of file diff --git a/Service/SyncService.php b/Service/SyncService.php index b628773..6f6d685 100755 --- a/Service/SyncService.php +++ b/Service/SyncService.php @@ -5,6 +5,8 @@ use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManagerInterface; use NTI\SyncBundle\Entity\SyncDeleteState; +use NTI\SyncBundle\Entity\SyncMapping; +use NTI\SyncBundle\Entity\SyncNewItemState; use NTI\SyncBundle\Entity\SyncState; use NTI\SyncBundle\Interfaces\SyncRepositoryInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -27,6 +29,7 @@ class SyncService { */ public function __construct(ContainerInterface $container) { $this->container = $container; + } /** @@ -51,19 +54,14 @@ public function getFromMappings($mappings) { $mappingName = $mapping["mapping"]; $serializationGroup = (isset($mapping["serializer"])) ? $mapping["serializer"] : "sync_basic"; - $syncMapping = $this->em->getRepository('NTISyncBundle:SyncMapping')->findOneBy(array("name" => $mappingName)); + $syncMapping = $this->em->getRepository(SyncMapping::class)->findOneBy(array("name" => $mappingName)); if(!$syncMapping) { continue; } - $deletes = $this->em->getRepository('NTISyncBundle:SyncDeleteState')->findFromTimestamp($mappingName, $timestamp); - $newItems = $this->em->getRepository('NTISyncBundle:SyncNewItemState')->findFromTimestampAndMapping($mappingName, $timestamp); - - /** - * Failed Items Synchronization - */ - $failedItems = $this->em->getRepository('NTISyncBundle:SyncFailedItemState')->findFromTimestampAndMapping($mappingName, $timestamp); + $deletes = $this->em->getRepository(SyncDeleteState::class)->findFromTimestamp($mappingName, $timestamp); + $newItems = $this->em->getRepository(SyncNewItemState::class)->findFromTimestampAndMapping($mappingName, $timestamp); /** @var SyncRepositoryInterface $repository */ $repository = $this->em->getRepository($syncMapping->getClass()); @@ -78,7 +76,6 @@ public function getFromMappings($mappings) { 'changes' => $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), SyncState::REAL_LAST_TIMESTAMP => $result[SyncState::REAL_LAST_TIMESTAMP], ); } @@ -86,31 +83,28 @@ public function getFromMappings($mappings) { return $changes; } - public function updateSyncState($class, $timestamp) { - - $this->em = $this->container->get('doctrine')->getManager(); + public function updateSyncState(EntityManagerInterface $em, $class, $timestamp) { - $mapping = $this->em->getRepository('NTISyncBundle:SyncMapping')->findOneBy(array("class" => $class)); + $mapping = $em->getRepository(SyncMapping::class)->findOneBy(array("class" => $class)); if(!$mapping) { return; } - $syncState = $this->em->getRepository('NTISyncBundle:SyncState')->findOneBy(array("mapping" => $mapping)); + $syncState = $em->getRepository(SyncState::class)->findOneBy(array("mapping" => $mapping)); + + $uow = $em->getUnitOfWork(); if(!$syncState) { $syncState = new SyncState(); $syncState->setMapping($mapping); - $this->em->persist($syncState); + $syncState->setTimestamp($timestamp); + $em->persist($syncState); + $uow->computeChangeSet($em->getClassMetadata(SyncState::class), $syncState); + } else { + $syncState->setTimestamp($timestamp); + $uow->recomputeSingleEntityChangeSet($em->getClassMetadata(SyncState::class), $syncState); } - $syncState->setTimestamp($timestamp); - - try { - $this->em->flush(); - } catch (\Exception $ex) { - error_log("Unable to register sync state change for object: " . $class); - error_log($ex->getMessage()); - } } /** @@ -123,7 +117,7 @@ public function addToDeleteSyncState($class, $id) { $this->em = $this->container->get('doctrine')->getManager(); - $mapping = $this->em->getRepository('NTISyncBundle:SyncMapping')->findOneBy(array("class" => $class)); + $mapping = $this->em->getRepository(SyncMapping::class)->findOneBy(array("class" => $class)); if(!$mapping) { return; }