/**
  * Add context in version manager
  *
  * @param JobExecutionEvent $event
  */
 public function addContext(JobExecutionEvent $event)
 {
     $jobInstance = $event->getJobExecution()->getJobInstance();
     if ($jobInstance->getType() === JobInstance::TYPE_IMPORT) {
         $this->versionContext->addContextInfo(sprintf('%s "%s"', JobInstance::TYPE_IMPORT, $jobInstance->getCode()));
     }
 }
 /**
  * Bulk generates and inserts full version records for the provided versionable entities
  * in MongoDB.
  * Return an array of ids of documents that have really changed since the last version.
  *
  * @param array $versionables
  *
  * @return array
  */
 public function bulkPersist(array $versionables)
 {
     $versions = [];
     $changedDocIds = [];
     $author = VersionManager::DEFAULT_SYSTEM_USER;
     $event = $this->eventDispatcher->dispatch(BuildVersionEvents::PRE_BUILD, new BuildVersionEvent());
     if (null !== $event && null !== $event->getUsername()) {
         $author = $event->getUsername();
     }
     foreach ($versionables as $versionable) {
         $previousVersion = $this->getPreviousVersion($versionable);
         $context = $this->versionContext->getContextInfo(get_class($versionable));
         $newVersion = $this->versionBuilder->buildVersion($versionable, $author, $previousVersion, $context);
         if (count($newVersion->getChangeSet()) > 0) {
             $versions[] = $newVersion;
             $changedDocIds[] = $versionable->getId();
         }
         if (null !== $previousVersion) {
             $this->documentManager->detach($previousVersion);
         }
     }
     $mongodbVersions = [];
     foreach ($versions as $version) {
         $mongodbVersions[] = $this->normalizer->normalize($version, VersionNormalizer::FORMAT);
     }
     if (count($mongodbVersions) > 0) {
         $collection = $this->documentManager->getDocumentCollection($this->versionClass);
         $collection->batchInsert($mongodbVersions);
     }
     return $changedDocIds;
 }
 /**
  * {@inheritdoc}
  */
 public function save($group, array $options = [])
 {
     /* @var GroupInterface */
     if (!$group instanceof GroupInterface) {
         throw new \InvalidArgumentException(sprintf('Expects a "Pim\\Bundle\\CatalogBundle\\Model\\GroupInterface", "%s" provided.', ClassUtils::getClass($group)));
     }
     $this->eventDispatcher->dispatch(GroupEvents::PRE_SAVE, new GenericEvent($group));
     $options = $this->optionsResolver->resolveSaveOptions($options);
     $this->versionContext->addContextInfo(sprintf('Comes from variant group %s', $group->getCode()), $this->productClassName);
     if ($group->getType()->isVariant()) {
         $template = $group->getProductTemplate();
         if (null !== $template) {
             $this->templateMediaManager->handleProductTemplateMedia($template);
         }
     }
     $this->objectManager->persist($group);
     if (true === $options['flush']) {
         $this->objectManager->flush();
     }
     if (count($options['add_products']) > 0) {
         $this->addProducts($options['add_products']);
     }
     if (count($options['remove_products']) > 0) {
         $this->removeProducts($options['remove_products']);
     }
     if ($group->getType()->isVariant() && true === $options['copy_values_to_products']) {
         $this->copyVariantGroupValues($group);
     }
     $this->eventDispatcher->dispatch(GroupEvents::POST_SAVE, new GenericEvent($group));
 }
 /**
  * Create the pending versions for the versionable provided
  *
  * @param VersionableInterface[] $versionables
  */
 public function persistPendingVersions(array $versionables)
 {
     $author = $this->versionManager->getUsername();
     $context = $this->versionContext->getContextInfo();
     $pendingVersions = [];
     foreach ($versionables as $versionable) {
         $changeset = $this->normalizer->normalize($versionable, 'csv', ['versioning' => true]);
         $pendingVersions[] = $this->versionBuilder->createPendingVersion($versionable, $author, $changeset, $context);
     }
     if (count($pendingVersions) > 0) {
         $this->batchInsertPendingVersions($pendingVersions);
     }
 }
 /**
  * Build a version from a versionable entity
  *
  * @param object $versionable
  * @param array  $changeset
  *
  * @return Version[]
  */
 public function buildVersion($versionable, array $changeset = [])
 {
     $createdVersions = [];
     $event = $this->eventDispatcher->dispatch(BuildVersionEvents::PRE_BUILD, new BuildVersionEvent());
     if (null !== $event && null !== $event->getUsername()) {
         $this->username = $event->getUsername();
     }
     if ($this->realTimeVersioning) {
         $manager = $this->registry->getManagerForClass(ClassUtils::getClass($versionable));
         $manager->refresh($versionable);
         $createdVersions = $this->buildPendingVersions($versionable);
         $builtVersions = array_filter($createdVersions, function ($version) {
             return count($version->getChangeset()) > 0;
         });
         if (!empty($builtVersions)) {
             $previousVersion = end($builtVersions);
         } else {
             $previousVersion = $this->getNewestLogEntry($versionable);
         }
         $createdVersions[] = $this->versionBuilder->buildVersion($versionable, $this->username, $previousVersion, $this->versionContext->getContextInfo(ClassUtils::getClass($versionable)));
         if (null !== $previousVersion) {
             $manager->detach($previousVersion);
         }
     } else {
         $createdVersions[] = $this->versionBuilder->createPendingVersion($versionable, $this->username, $changeset, $this->versionContext->getContextInfo(ClassUtils::getClass($versionable)));
     }
     return $createdVersions;
 }
 /**
  * {@inheritdoc}
  *
  * If a versionable contains no change (i.e. has nothing in its changeset) no version will be built for it.
  */
 public function buildVersions(array $versionables)
 {
     $author = VersionManager::DEFAULT_SYSTEM_USER;
     $event = $this->eventDispatcher->dispatch(BuildVersionEvents::PRE_BUILD, new BuildVersionEvent());
     if (null !== $event && null !== $event->getUsername()) {
         $author = $event->getUsername();
     }
     $versions = [];
     foreach ($versionables as $versionable) {
         $context = $this->versionContext->getContextInfo(ClassUtils::getClass($versionable));
         $previousVersion = $this->getPreviousVersion($versionable);
         $newVersion = $this->versionBuilder->buildVersion($versionable, $author, $previousVersion, $context);
         if (0 < count($newVersion->getChangeSet())) {
             $versions[] = $newVersion;
         }
         if (null !== $previousVersion) {
             $this->documentManager->detach($previousVersion);
         }
     }
     return $versions;
 }
 /**
  * @param       $group
  * @param array $options
  */
 protected function persistGroup($group, array $options)
 {
     $context = $this->productClassName;
     $this->versionContext->addContextInfo(sprintf('Comes from variant group %s', $group->getCode()), $context);
     if ($group->getType()->isVariant()) {
         $template = $group->getProductTemplate();
         if (null !== $template) {
             $this->templateMediaManager->handleProductTemplateMedia($template);
         }
     }
     $this->objectManager->persist($group);
     $this->saveAssociatedProducts($group);
     if ($group->getType()->isVariant() && true === $options['copy_values_to_products']) {
         $this->copyVariantGroupValues($group);
         $this->detacher->detachAll($group->getProducts()->toArray());
     }
     $this->versionContext->unsetContextInfo($context);
 }
 /**
  * Build a version from a versionable entity
  *
  * @param object $versionable
  * @param array  $changeset
  *
  * @return Version[]
  */
 public function buildVersion($versionable, array $changeset = [])
 {
     $createdVersions = [];
     if ($this->realTimeVersioning) {
         $this->registry->getManagerForClass(ClassUtils::getClass($versionable))->refresh($versionable);
         $createdVersions = $this->buildPendingVersions($versionable);
         $builtVersions = array_filter($createdVersions, function ($version) {
             return count($version->getChangeset()) > 0;
         });
         if (!empty($builtVersions)) {
             $previousVersion = end($builtVersions);
         } else {
             $previousVersion = $this->getNewestLogEntry($versionable);
         }
         $createdVersions[] = $this->versionBuilder->buildVersion($versionable, $this->username, $previousVersion, $this->versionContext->getContextInfo(ClassUtils::getClass($versionable)));
     } else {
         $createdVersions[] = $this->versionBuilder->createPendingVersion($versionable, $this->username, $changeset, $this->versionContext->getContextInfo(ClassUtils::getClass($versionable)));
     }
     return $createdVersions;
 }
 /**
  * Get an object hash, provides different hashes depending on version manager context to allows to log different
  * versions of a same object during a request
  *
  * @param object $object
  *
  * @return string
  */
 protected function getObjectHash($object)
 {
     return sprintf('%s#%s', spl_object_hash($object), sha1($this->versionContext->getContextInfo()));
 }