Skip to content

Commit

Permalink
Merge pull request #2 from ntidev/master
Browse files Browse the repository at this point in the history
Resolving UnitOfWork Exception
  • Loading branch information
ealcantara22 authored Mar 13, 2018
2 parents abbaf38 + f19cba1 commit 70b09e9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 49 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);
}

}
3 changes: 2 additions & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ services:
- { name: doctrine.event_subscriber, connection: default }
nti.sync:
class: NTI\SyncBundle\Service\SyncService
arguments: ["@service_container"]
arguments: ["@service_container"]
public: true
42 changes: 18 additions & 24 deletions Service/SyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,6 +29,7 @@ class SyncService {
*/
public function __construct(ContainerInterface $container) {
$this->container = $container;

}

/**
Expand All @@ -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());
Expand All @@ -78,39 +76,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 All @@ -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;
}
Expand Down

0 comments on commit 70b09e9

Please sign in to comment.