Skip to content

Commit

Permalink
Changed the event subscriber to work on OnFlush to prevent exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Vison committed Feb 9, 2018
1 parent 8962dce commit be6ee9e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 44 deletions.
2 changes: 0 additions & 2 deletions Controller/SyncController.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ public function pushAction(Request $request) {

$syncClass = $mapping->getSyncService();

#if(!class_exists($syncClass)) { continue; }

/** @var SyncServiceInterface $service */
$service = $this->get($syncClass);

Expand Down
Empty file modified Entity/SyncState.php
100644 → 100755
Empty file.
43 changes: 21 additions & 22 deletions EventListener/DoctrineEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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);
}

}
33 changes: 13 additions & 20 deletions Service/SyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use NTI\SyncBundle\Entity\SyncDeleteState;
use NTI\SyncBundle\Entity\SyncMapping;
use NTI\SyncBundle\Entity\SyncState;
use NTI\SyncBundle\Interfaces\SyncRepositoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand All @@ -27,6 +28,7 @@ class SyncService {
*/
public function __construct(ContainerInterface $container) {
$this->container = $container;

}

/**
Expand Down Expand Up @@ -60,11 +62,6 @@ public function getFromMappings($mappings) {
$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);

/** @var SyncRepositoryInterface $repository */
$repository = $this->em->getRepository($syncMapping->getClass());
if(!($repository instanceof SyncRepositoryInterface)) {
Expand All @@ -78,39 +75,35 @@ 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],
);
}

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());
}
}

/**
Expand Down

0 comments on commit be6ee9e

Please sign in to comment.